diff --git a/gcc/builtins.c b/gcc/builtins.c index 0db4090c434eeeb371d980d15d8eda72a8aa4a8b..6f67eb7eeb5588e99d1a149ccc8e5b8cf6e767a3 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -5447,8 +5447,10 @@ compute_objsize_r (tree ptr, int ostype, access_ref *pref, if (!addr && POINTER_TYPE_P (TREE_TYPE (ptr))) { /* Set the maximum size if the reference is to the pointer - itself (as opposed to what it points to). */ + itself (as opposed to what it points to), and clear + BASE0 since the offset isn't necessarily zero-based. */ pref->set_max_size_range (); + pref->base0 = false; return true; } diff --git a/gcc/testsuite/g++.dg/warn/Wplacement-new-size-9.C b/gcc/testsuite/g++.dg/warn/Wplacement-new-size-9.C new file mode 100644 index 0000000000000000000000000000000000000000..a6b5870be64514c720c5c25c4a79f7867a721cef --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wplacement-new-size-9.C @@ -0,0 +1,39 @@ +/* PR middle-end/100307 - spurious -Wplacement-new with negative pointer + offset + { dg-do compile } + { dg-options "-O0 -Wall" } */ + +void* operator new (__SIZE_TYPE__, void *p) { return p; } +void* operator new[] (__SIZE_TYPE__, void *p) { return p; } + +static char a[2]; + +void* nowarn_scalar () +{ + char* p = a + 1; + char *q = new (p - 1) char (); // { dg-bogus "-Wplacement-new" } + return q; +} + + +void* nowarn_array () +{ + char* p = a + 1; + char *q = new (p - 1) char[2]; // { dg-bogus "-Wplacement-new" } + return q; +} + +void* warn_scalar () +{ + char* p = a + 1; + char *q = new (p - 2) char (); // { dg-warning "-Wplacement-new" "pr100325" { xfail *-*-* } } + return q; +} + + +void* warn_array () +{ + char* p = a + 1; + char *q = new (p - 1) char[2]; // { dg-warning "-Wplacement-new" "pr100325" { xfail *-*-* } } + return q; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-26.c b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-26.c new file mode 100644 index 0000000000000000000000000000000000000000..16a551d9c8da00ab3ce83af6417fa5146dab8a5c --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-26.c @@ -0,0 +1,38 @@ +/* PR middle-end/100307 - spurious -Wplacement-new with negative pointer + offset + { dg-do compile } + { dg-options "-O0 -Wall" } */ + +extern int sprintf (char*, const char*, ...); + +char a[4]; + +void nowarn_1m1 () +{ + char *p = a + 1; + sprintf (p - 1, "%i", 123); // { dg-bogus "-Wformat-overflow" } +} + +void nowarn_4m3 () +{ + char *p = a + 4; + sprintf (p - 3, "%i", 12); // { dg-bogus "-Wformat-overflow" } +} + +void warn_2m1 () +{ + char *p = a + 2; + sprintf (p - 1, "%i", 123); // { dg-warning "-Wformat-overflow" "pr100325" { xfail *-*-* } } +} + +void warn_3m1 () +{ + char *p = a + 3; + sprintf (p - 1, "%i", 12); // { dg-warning "-Wformat-overflow" "pr100325" { xfail *-*-* } } +} + +void warn_4m1 () +{ + char *p = a + 4; + sprintf (p - 1, "%i", 1); // { dg-warning "-Wformat-overflow" "pr100325" { xfail *-*-* } } +}