[committed] ubsan: Add another testcase for [0] array in the middle of struct [PR108894]
Commit Message
Hi!
On Tue, Feb 28, 2023 at 10:59:03PM +0100, Jakub Jelinek via Gcc-patches wrote:
> On Tue, Feb 28, 2023 at 07:19:40PM +0000, Qing Zhao wrote:
> > Understood.
> > So, your patch fixed this bug, and then [0] arrays are instrumented by default with this patch.
> >
> > > Well, it would complain about
> > > struct S { int a; int b[0]; int c; } s;
> > > ... &s.b[1] ...
> > > for C++, but not for C.
> >
> > A little confused here: [0] arrays were instrumented by default for C++ if it’s not a trailing array, but not for C?
>
> Given say
> struct S { int a; int b[0]; int c; } s;
>
> int
> main ()
> {
> int *volatile p = &s.b[0];
> p = &s.b[1];
> int volatile q = s.b[0];
> }
And, when I wrote such a testcase, I thought it would be worth it to have it
in the testsuite too.
Tested on x86_64-linux -m32/-m64, committed to trunk as obvious:
2023-03-01 Jakub Jelinek <jakub@redhat.com>
PR sanitizer/108894
* c-c++-common/ubsan/bounds-16.c: New test.
Jakub
@@ -0,0 +1,15 @@
+/* PR sanitizer/108894 */
+/* { dg-do run } */
+/* { dg-options "-fsanitize=bounds -fsanitize-recover=bounds" } */
+/* { dg-output "index 1 out of bounds for type 'int \\\[\[*0-9x]*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*index 0 out of bounds for type 'int \\\[\[*0-9x]*\\\]'" } */
+
+struct S { int a; int b[0]; int c; } s;
+
+int
+main ()
+{
+ int *volatile p = &s.b[0];
+ p = &s.b[1];
+ int volatile q = s.b[0];
+}