public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/107754] New: Confusing -Warray-bounds warning with strcpy
@ 2022-11-18 21:19 nightstrike at gmail dot com
2022-11-18 21:28 ` [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer pinskia at gcc dot gnu.org
2022-11-18 21:29 ` [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer and non-zero offset for struct array pinskia at gcc dot gnu.org
0 siblings, 2 replies; 3+ messages in thread
From: nightstrike at gmail dot com @ 2022-11-18 21:19 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107754
Bug ID: 107754
Summary: Confusing -Warray-bounds warning with strcpy
Product: gcc
Version: 12.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: nightstrike at gmail dot com
Target Milestone: ---
Metabug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=warray-bounds
// Compile with GCC 12.2.0 with -O2:
// warning: ‘strcpy’ offset 0 is out of the bounds [0, 0] [-Warray-bounds]
// no warning without -O2
struct Inst;
struct Class { int offset; };
static struct Class * classFoo; // No problem without static here
struct Foo
{
unsigned int a; // no problem if commented out
char bar[1024];
};
void setFoo(struct Inst * this, const char * value)
{
struct Foo * ptr = (struct Foo *)(this ? (((char *)this) + classFoo->offset)
: 0);
__builtin_strcpy(ptr->bar, value);
}
$ gcc-12 -c -O2 -Warray-bounds a.c -o /dev/null
a.c: In function 'setFoo':
a.c:19:4: warning: '__builtin_strcpy' offset 0 is out of the bounds [0, 0]
[-Warray-bounds]
19 | __builtin_strcpy(ptr->bar, value);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer
2022-11-18 21:19 [Bug c/107754] New: Confusing -Warray-bounds warning with strcpy nightstrike at gmail dot com
@ 2022-11-18 21:28 ` pinskia at gcc dot gnu.org
2022-11-18 21:29 ` [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer and non-zero offset for struct array pinskia at gcc dot gnu.org
1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-18 21:28 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107754
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Last reconfirmed| |2022-11-18
Ever confirmed|0 |1
Summary|Confusing -Warray-bounds |Confusing -Warray-bounds
|warning with strcpy |warning with strcpy with a
| |null pointer
Status|UNCONFIRMED |NEW
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. Note the warning message is correct; just confusing and does not
mention a null pointer here.
Reduced further:
struct Foo
{
unsigned int a;
char bar[1024];
};
void setFoo(const char * value)
{
struct Foo * ptr = 0;
__builtin_strcpy(ptr->bar, value);
}
---- CUT ---
Since the offset for the character array is non-zero, we see a non-zero
constant and (based on other settings) assume it is the null pointer page and
we get a size of 0 (which is ok) but don't mention a null pointer.
There might be other dups of this but I am not going to search for it right
now.
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer and non-zero offset for struct array
2022-11-18 21:19 [Bug c/107754] New: Confusing -Warray-bounds warning with strcpy nightstrike at gmail dot com
2022-11-18 21:28 ` [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer pinskia at gcc dot gnu.org
@ 2022-11-18 21:29 ` pinskia at gcc dot gnu.org
1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-18 21:29 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107754
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note in the origin "reduced" testcase, we had a conditional null pointer which
was exposing the null pointer at -O2 due to optimizations.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-11-18 21:29 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-18 21:19 [Bug c/107754] New: Confusing -Warray-bounds warning with strcpy nightstrike at gmail dot com
2022-11-18 21:28 ` [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer pinskia at gcc dot gnu.org
2022-11-18 21:29 ` [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer and non-zero offset for struct array pinskia 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).