public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/102919] New: spurious -Wrestrict warning for sprintf into the same member array as argument plus offset
@ 2021-10-24 22:12 msebor at gcc dot gnu.org
  2021-10-24 22:12 ` [Bug tree-optimization/102919] " msebor at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-10-24 22:12 UTC (permalink / raw)
  To: gcc-bugs

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)
      |         ~~~~~~~~~~^

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-10-26 23:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-24 22:12 [Bug tree-optimization/102919] New: spurious -Wrestrict warning for sprintf into the same member array as argument plus offset msebor at gcc dot gnu.org
2021-10-24 22:12 ` [Bug tree-optimization/102919] " msebor at gcc dot gnu.org
2021-10-24 23:44 ` msebor at gcc dot gnu.org
2021-10-26 22:54 ` cvs-commit at gcc dot gnu.org
2021-10-26 23:03 ` msebor at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).