Bug 122073 - Bogus -Wstringop-overread with [[gnu::nonstring]] and type punning
Summary: Bogus -Wstringop-overread with [[gnu::nonstring]] and type punning
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 16.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL: https://lore.kernel.org/899f2dec-e8b9...
Keywords: diagnostic
Depends on:
Blocks: Wstringop-overread
  Show dependency treegraph
 
Reported: 2025-09-26 16:12 UTC by Xi Ruoyao
Modified: 2025-10-17 21:05 UTC (History)
0 users

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 Xi Ruoyao 2025-09-26 16:12:26 UTC
struct [[gnu::may_alias]] A
{
  [[gnu::nonstring]] char x[4];
};

struct B
{
  [[gnu::nonstring]] char x[2];
};

void
f (struct B *p)
{
  if (!__builtin_strncmp (p->x, "AA", 2))
    ;
  else if (!__builtin_strncmp (((struct A *)p)->x, "BBCC", 4))
    asm ("");
}

results in a warning which does not make any sense (to me):

t1.c: In function 'f':
t1.c:16:13: warning: '__builtin_strncmp' argument 1 declared attribute 'nonstring' is smaller than the specified bound 4 [-Wstringop-overread]
   16 |   else if (!__builtin_strncmp (((struct A *)p)->x, "BBCC", 4))
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t1.c:8:27: note: argument 'x' declared here
    8 |   [[gnu::nonstring]] char x[2];
      |                           ^
Comment 1 Andrew Pinski 2025-09-26 17:01:07 UTC
This is 99% a dup of a different bug dealing with addr_expr being merged to be the same since they point to the same location but this warning requires the non merging of the addr_expr.
Comment 2 Andrew Pinski 2025-09-26 17:02:40 UTC
Yes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118232#c4