Bug 84754 - missing -Wrestrict on a possible strcpy overlap with constant offset
Summary: missing -Wrestrict on a possible strcpy overlap with constant offset
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wrestrict
  Show dependency treegraph
 
Reported: 2018-03-08 00:46 UTC by Martin Sebor
Modified: 2023-04-24 21:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2018-03-08 00:46:27 UTC
-Wrestrict triggers for a strcpy call from a string of unknown length and unknown offset but not the same call with a constant offset, even though the latter seems more likely to overlap than the former (the smaller the offset the more likely the cooy is to overlap).  In any case, unlike for memcpy, for string functions -Wrestrict is specifically meant to warn even for possible overlaps (i.e., when copying into the same array) so this is a bug, not just a missing feature or design choice.

$ cat z.c && gcc -O2 -S -Wall -fdump-tree-wrestrict=/dev/stdout z.c
char a[32];

void f (int i)
{
  __builtin_strcpy (a, a + i);   // -Wrestrict (good)
}

void g (void)
{
  __builtin_strcpy (a, a + 1);   // missing -Wrestrict
}


;; Function f (f, funcdef_no=0, decl_uid=1959, cgraph_uid=0, symbol_order=1)

z.c: In function ‘f’:
z.c:5:3: warning: ‘__builtin_strcpy’ accessing 1 byte at offsets 0 and [0, 32] may overlap 1 byte at offset 0 [-Wrestrict]
   __builtin_strcpy (a, a + i);   // -Wrestrict (good)
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
f (int i)
{
  sizetype _1;
  const char * _2;

  <bb 2> [local count: 1073741825]:
  _1 = (sizetype) i_3(D);
  _2 = &a + _1;
  __builtin_strcpy (&a, _2);
  return;

}



;; Function g (g, funcdef_no=1, decl_uid=1962, cgraph_uid=1, symbol_order=2)

g ()
{
  <bb 2> [local count: 1073741825]:
  __builtin_strcpy (&a, &MEM[(void *)&a + 1B]);
  return;

}