https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94226

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |84774
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
                 CC|                            |msebor at gcc dot gnu.org
           Keywords|                            |diagnostic
   Last reconfirmed|                            |2020-03-19

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
$ cat pr94226.c && gcc -O2 -S -Wall -fdump-tree-strlen=/dev/stdout pr94226.c
extern char a2[2][22];

int f (void)
{
  return __builtin_sprintf (a2[1], "%s", a2[1] + 1);   // expect -Wrestrict
}


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

;; 1 loops found
;;
;; Loop 0
;;  header 0, latch 1
;;  depth 0, outer -1
;;  nodes: 0 1 2
;; 2 succs { 1 }
pr94226.c:5: __builtin_sprintf: objsize = 22, fmtstr = "%s"
  Directive 1 at offset 0: "%s"
    Result: 0, 20, 20, 20 (0, 20, 20, 20)
  Directive 2 at offset 2: "", length = 1
  Setting in-bounds return value range [0, 20].

f ()
{
  int _3;

  <bb 2> [local count: 1073741824]:
  _3 = __builtin_sprintf (&a2[1], "%s", &MEM <char[22]> [(void *)&a2 + 23B]);
  return _3;

}


Prior to the change GCC issued the warning below:

a.c: In function ‘f’:
a.c:7:10: warning: ‘__builtin_sprintf’ argument 3 may overlap destination
object ‘a2’ [-Wrestrict]
    7 |   return __builtin_sprintf (a2[1], "%s", a2[1] + 1);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.c:3:13: note: destination object referenced by ‘restrict’-qualified argument
1 was declared here
    3 | extern char a2[2][22];
      |             ^~

The warning code in gimple-ssa-sprintf.c most likely needs adjusting.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84774
[Bug 84774] [meta-bug] bogus/missing -Wrestrict

Reply via email to