(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Warray-bounds-46.c
       1  /* PR middle-end/91631 - buffer overflow into an array member of a declared
       2     object not detected
       3     Test to verify that past-the-end accesses by string functions to member
       4     arrays by-reference objects are diagnosed.
       5     { dg-do compile }
       6     { dg-options "-O2 -Wall -Wno-unused-local-typedefs -Wno-stringop-overflow -ftrack-macro-expansion=0" }
       7     { dg-require-effective-target alloca } */
       8  
       9  #define SA(expr) typedef int StaticAssert [2 * !!(expr) - 1]
      10  
      11  typedef __SIZE_TYPE__ size_t;
      12  
      13  extern char* strcpy (char*, const char*);
      14  extern char* strncpy (char*, const char*, size_t);
      15  
      16  void sink (void*);
      17  
      18  struct MA17
      19  {
      20    char pad[4];
      21    char a1[1], a2[2], a3[3], a4[4], a5[5], a6[6], a7[7], a8[8], a9[9], a10[10];
      22    char a11[11], a12[12], a13[13], a14[14], a15[15], a16[16], a17[17], ax[];
      23  };
      24  
      25  extern struct MA17 gma;
      26  extern struct MA17 gma2[2];
      27  
      28  struct MA17 igma_3 = { .ax = { 1, 2, 3 } };
      29  struct MA17 igma2_[2];
      30  
      31  #define S36 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      32  
      33  #define S(N)   (S36 + sizeof (S36) - N - 1)
      34  
      35  /* In the test macro, prevent the strcpy to memcpy transformation
      36     by using a local array initialized with the string literal.  Without
      37     it, GCC transforms the strcpy call with memcpy which (unfortunately)
      38     permits accesses that cross subobject boundaries.  */
      39  #define T(dst, n)				\
      40    do {						\
      41      const char a[] = S36;			\
      42      strcpy (dst, a + sizeof a - n - 1);		\
      43      sink (dst);					\
      44    } while (0)
      45  
      46  void strcpy_global (void)
      47  {
      48    T (gma.a1, 0);
      49    T (gma.a1, 1);          // { dg-warning "'strcpy' offset 5 from the object at 'gma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" }
      50    T (gma.a1, 4);          // { dg-warning "'strcpy' offset \\\[5, 8] from the object at 'gma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" }
      51  
      52    T (gma.a2, 1);
      53    T (gma.a2, 2);          // { dg-warning "'strcpy' offset 7 from the object at 'gma' is out of the bounds of referenced subobject 'a2' with type 'char\\\[2]' at offset 5" }
      54  
      55    T (gma.a3, 2);
      56    T (gma.a3, 3);          // { dg-warning "'strcpy' offset 10 from the object at 'gma' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset 7" }
      57  
      58    T (gma.a4, 3);
      59    T (gma.a4, 4);          // { dg-warning "'strcpy' offset 14 from the object at 'gma' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset 10" }
      60  
      61    T (gma.a5, 4);
      62    T (gma.a5, 5);          // { dg-warning "'strcpy' offset 19 from the object at 'gma' is out of the bounds of referenced subobject 'a5' with type 'char\\\[5]' at offset 14" }
      63  
      64    SA (__builtin_offsetof (struct MA17, a17) == 140);
      65  
      66    T (gma.a17, 16);
      67    T (gma.a17, 17);        // { dg-warning "'strcpy' offset 157 from the object at 'gma' is out of the bounds of referenced subobject 'a17' with type 'char\\\[17]' at offset 140" }
      68  
      69    SA (__builtin_offsetof (struct MA17, ax) == 157);
      70    // GCC allows static initialization of flexible array members of
      71    // non-local objects.  Verify that writing into one that may be
      72    // initialized in another translation unit isn't diagnosed.  */
      73    T (gma.ax, 0);          // { dg-bogus "\\\[-Warray-bounds" }
      74  }
      75  
      76  
      77  void strcpy_global_array (void)
      78  {
      79    T (gma2[0].a1, 0);
      80    T (gma2[0].a1, 1);      // { dg-warning "'strcpy' offset 5 from the object at 'gma2' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" }
      81    T (gma2[0].a1, 4);      // { dg-warning "'strcpy' offset \\\[5, 8] from the object at 'gma2' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" }
      82  
      83    T (gma2[0].a2, 1);
      84    T (gma2[0].a2, 2);      // { dg-warning "'strcpy' offset 7 from the object at 'gma2' is out of the bounds of referenced subobject 'a2' with type 'char\\\[2]' at offset 5" }
      85  
      86    T (gma2[0].a3, 2);
      87    T (gma2[0].a3, 3);      // { dg-warning "'strcpy' offset 10 from the object at 'gma2' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset 7" }
      88  
      89    T (gma2[0].a4, 3);
      90    T (gma2[0].a4, 4);      // { dg-warning "'strcpy' offset 14 from the object at 'gma2' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset 10" }
      91  
      92    T (gma2[0].a5, 4);
      93    T (gma2[0].a5, 5);      // { dg-warning "'strcpy' offset 19 from the object at 'gma2' is out of the bounds of referenced subobject 'a5' with type 'char\\\[5]' at offset 14" }
      94  
      95    T (gma2[0].a17, 16);
      96    T (gma2[0].a17, 17);    // { dg-warning "'strcpy' offset 157 from the object at 'gma2' is out of the bounds of referenced subobject 'a17' with type 'char\\\[17]' at offset 140" }
      97  
      98    /* GMA2 is external but because it's an array its definition in another
      99       translation unit may not provide an initializer for the flexible array
     100       member.  Verify that a warning is issued for access to it.  */
     101    T (gma2[0].ax, 1);      // { dg-warning "'strcpy' offset \\\[157, 158] from the object at 'gma2' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 157" }
     102    T (gma2[0].ax, 7);      // { dg-warning "'strcpy' offset \\\[157, 164] from the object at 'gma2' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 157" }
     103  
     104    /* IGMA2_ is internal and provides no definition for the flexible array
     105       member.  Verify that a warning is issued for out-of-bounds accesses
     106       to it.  */
     107    T (igma2_[0].ax, 1);    // { dg-warning "'strcpy' offset \\\[157, 158] from the object at 'igma2_' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 157" }
     108  
     109    T (igma_3.ax, 0);
     110    T (igma_3.ax, 1);
     111    T (igma_3.ax, 1);
     112    T (igma_3.ax, 3);       // { dg-warning " offset 160 " }
     113    T (igma_3.ax, 9);       // { dg-warning " offset \\\[160, 166] " }
     114  }
     115  
     116  
     117  void strcpy_local (void)
     118  {
     119    struct MA17 lma;
     120  
     121    T (lma.a1, 0);
     122    T (lma.a1, 1);          // { dg-warning "'strcpy' offset 5 from the object at 'lma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" }
     123    T (lma.a1, 4);          // { dg-warning "'strcpy' offset \\\[5, 8] from the object at 'lma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" }
     124  
     125    T (lma.a2, 1);
     126    T (lma.a2, 2);          // { dg-warning "'strcpy' offset 7 from the object at 'lma' is out of the bounds of referenced subobject 'a2' with type 'char\\\[2]' at offset 5" }
     127  
     128    T (lma.a3, 2);
     129    T (lma.a3, 3);          // { dg-warning "'strcpy' offset 10 from the object at 'lma' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset 7" }
     130  
     131    T (lma.a4, 3);
     132    T (lma.a4, 4);          // { dg-warning "'strcpy' offset 14 from the object at 'lma' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset 10" }
     133  
     134    T (lma.a5, 4);
     135    T (lma.a5, 5);          // { dg-warning "'strcpy' offset 19 from the object at 'lma' is out of the bounds of referenced subobject 'a5' with type 'char\\\[5]' at offset 14" }
     136  
     137    T (lma.a17, 16);
     138    T (lma.a17, 17);        // { dg-warning "'strcpy' offset 157 from the object at 'lma' is out of the bounds of referenced subobject 'a17' with type 'char\\\[17]' at offset 140" }
     139  
     140    T (lma.ax, 0);          // { dg-warning "'strcpy' offset 157 from the object at 'lma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 157" }
     141  }
     142  
     143  
     144  void strcpy_ref (struct MA17 *pma)
     145  {
     146    T (pma->a1, 0);
     147    T (pma->a1, 1);         // { dg-warning "'strcpy' offset 5 from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" }
     148    T (pma->a1, 4);         // { dg-warning "'strcpy' offset \\\[5, 8] from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" }
     149  
     150    T (pma->a2, 1);
     151    T (pma->a2, 2);         // { dg-warning "'strcpy' offset 7 from the object at 'pma' is out of the bounds of referenced subobject 'a2' with type 'char\\\[2]' at offset 5" }
     152  
     153    T (pma->a3, 2);
     154    T (pma->a3, 3);         // { dg-warning "'strcpy' offset 10 from the object at 'pma' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset 7" }
     155  
     156    T (pma->a4, 3);
     157    T (pma->a4, 4);         // { dg-warning "'strcpy' offset 14 from the object at 'pma' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset 10" }
     158  
     159    T (pma->a5, 4);
     160    T (pma->a5, 5);         // { dg-warning "'strcpy' offset 19 from the object at 'pma' is out of the bounds of referenced subobject 'a5' with type 'char\\\[5]' at offset 14" }
     161  
     162    T (pma->a17, 16);
     163    T (pma->a17, 17);       // { dg-warning "'strcpy' offset 157 from the object at 'pma' is out of the bounds of referenced subobject 'a17' with type 'char\\\[17]' at offset 140" }
     164  
     165    T (pma->ax, 0);
     166    T ((*pma).ax, 8);
     167    T (pma[0].ax, 9);
     168  
     169    SA (__builtin_offsetof (struct MA17, a1) == 4
     170        && sizeof (struct MA17) == 157);
     171  
     172    T (pma[1].a1, 0);
     173    T (pma[1].a1, 1);       // { dg-warning "'strcpy' offset 162 from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 161" }
     174    T (pma[1].a1, 4);       // { dg-warning "'strcpy' offset \\\[162, 165] from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 161" }
     175  
     176    T (pma[1].a2, 1);
     177    T (pma[1].a2, 2);       // { dg-warning "'strcpy' offset 164 from the object at 'pma' is out of the bounds of referenced subobject 'a2' with type 'char\\\[2]' at offset 162" }
     178  
     179    T (pma[1].a3, 2);
     180    T (pma[1].a3, 3);       // { dg-warning "'strcpy' offset 167 from the object at 'pma' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset 164" }
     181  
     182    T (pma[1].a4, 3);
     183    T (pma[1].a4, 4);       // { dg-warning "'strcpy' offset 171 from the object at 'pma' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset 167" }
     184  
     185    T (pma[1].a5, 4);
     186    T (pma[1].a5, 5);       // { dg-warning "'strcpy' offset 176 from the object at 'pma' is out of the bounds of referenced subobject 'a5' with type 'char\\\[5]' at offset 171" }
     187  
     188    T (pma[1].a17, 16);
     189    T (pma[1].a17, 17);     // { dg-warning "'strcpy' offset 314 from the object at 'pma' is out of the bounds of referenced subobject 'a17' with type 'char\\\[17]' at offset 297" }
     190  
     191    /* Since PMA points to an array of structs, accessing the flexible
     192       member of any of the elements of the array except for the last one
     193       would necessarily access a part of the next element of the enclosing
     194       array.  The warning assumes that PMA doesn't point to the last element
     195       of the array which could in theory have nonzero elements without
     196       overlapping other objects.  */
     197    T (pma[1].ax, 0);       // { dg-warning "'strcpy' offset 314 from the object at 'pma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 314" }
     198    T ((pma + 1)->ax, 1);   // { dg-warning "'strcpy' offset \\\[314, 315] from the object at 'pma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 314" }
     199    T ((pma + 1)[1].ax, 2); // { dg-warning "'strcpy' offset \\\[471, 473] from the object at 'pma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 471" }
     200    T ((*(pma + 2)).ax, 2); // { dg-warning "'strcpy' offset \\\[471, 473] from the object at 'pma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 471" }
     201    T (pma[3].ax, 9);       // { dg-warning "'strcpy' offset \\\[628, 637] from the object at 'pma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 628" }
     202  
     203    T (pma[-1].a1, 0);
     204    T (pma[-1].a1, 1);      // { dg-warning "'strcpy' offset -152 from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset -153" }
     205    T (pma[-1].a1, 4);      // { dg-warning "'strcpy' offset \\\[-152, -149] from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset -153" }
     206  }
     207  
     208  struct MA3
     209  {
     210    char a4[4];             // { dg-message "'a4' declared here" }
     211    char a3[3];             // { dg-message "'a3' declared here" }
     212    char c;
     213  };
     214  
     215  void strcpy_ref_note (struct MA17 *pma, struct MA3 *pma3)
     216  {
     217    T (pma3[-1].a4, 0);
     218    T (pma3[-1].a4, 1);
     219    T (pma3[-1].a4, 2);
     220    T (pma3[-1].a4, 3);
     221    T (pma3[-1].a4, 4);     // { dg-warning "'strcpy' offset -4 from the object at 'pma3' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset -8" }
     222    T (pma3[-1].a4, 5);     // { dg-warning "'strcpy' offset \\\[-4, -3] from the object at 'pma3' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset -8" }
     223  
     224    T (pma3[-1].a3, 0);
     225    T (pma3[-1].a3, 1);
     226    T (pma3[-1].a3, 2);
     227    T (pma3[-1].a3, 3);     // { dg-warning "'strcpy' offset -1 from the object at 'pma3' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset -4" }
     228    T (pma3[-1].a3, 4);     // { dg-warning "'strcpy' offset \\\[-1, 0] from the object at 'pma3' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset -4" }
     229  }
     230  
     231  
     232  void strncpy_vla_member (unsigned n)
     233  {
     234    struct VarLenStruct {
     235      char a4[4], an[n], bn[n];
     236    } x;
     237  
     238    sink (&x);
     239  
     240    strncpy (x.bn, x.a4, sizeof x.bn);
     241    sink (&x);
     242  
     243    strncpy (x.a4, x.bn, sizeof x.a4);
     244    x.a4[sizeof x.a4 - 1] = '\0';
     245    sink (&x);
     246  
     247    strncpy (x.a4, x.bn, n);
     248    sink (&x);
     249  
     250    strncpy (x.an, x.bn, sizeof x.bn);    /* { dg-bogus "\\\[-Warray-bounds" } */
     251    sink (&x);
     252  }