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

            Bug ID: 102919
           Summary: spurious -Wrestrict warning for sprintf into the same
                    member array as argument plus offset
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The write below by sprintf into p->a cannot overlap with the read from p->a + 2
because the length of the string at p->a + 2 is at most 1.  The only it might
overlap is if the string was longer than fits in the array which would make the
sprintf call invalid for another reason: reading past the end.  The -Wrestrict
instance should be avoided here.

$ cat z.c && gcc -O2 -S -Wall z.c
struct A { char a[4]; int i; };

void f (struct A *p)
{ 
  __builtin_sprintf (p->a, "%s", p->a + 2);
}
z.c: In function ‘void f(A*)’:
z.c:5:21: warning: ‘__builtin_sprintf’ argument 3 may overlap destination
object ‘p’ [-Wrestrict]
    5 |   __builtin_sprintf (p->a, "%s", p->a + 2);
      |   ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
z.c:3:19: note: destination object referenced by ‘restrict’-qualified argument
1 was declared here
    3 | void f (struct A *p)
      |         ~~~~~~~~~~^

Reply via email to