(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Warray-bounds-32.c
       1  /* PR tree-optimization/83776: missing -Warray-bounds indexing past the end
       2     of a string literal
       3     Test to exercise indices into wide string literals.
       4     { dg-do compile }
       5     { dg-options "-O2 -Warray-bounds -ftrack-macro-expansion=0" } */
       6  
       7  #include "range.h"
       8  
       9  #define MAX DIFF_MAX
      10  #define MIN DIFF_MIN
      11  
      12  typedef __WCHAR_TYPE__ wchar_t;
      13  
      14  #define W2 L"12"
      15  #define W3 L"123"
      16  #define W4 L"1234"
      17  #define W7 L"1234567"
      18  #define W8 L"12345678"
      19  #define W9 L"123456789"
      20  
      21  void sink (int);
      22  
      23  #define T(expr)   sink (expr)
      24  
      25  
      26  void wide_direct_cst (void)
      27  {
      28    T (W9[MIN]);                /* { dg-warning "array subscript -\[0-9\]+ is below array bounds of .\[a-z \]+\\\[10]" "bug 86611" { xfail ilp32 } } */
      29    T (W9[-1]);                 /* { dg-warning "array subscript -1 is below array bounds of .\[a-z \]+\\\[10]" } */
      30    T (W9[11]);                 /* { dg-warning "array subscript 11 is above array bounds of .\[a-z \]+\\\[10]" } */
      31    T (W9[MAX]);                /* { dg-warning "array subscript \[0-9\]+ is above array bounds of .\[a-z \]+\\\[10]" } */
      32  }
      33  
      34  void wide_ptr_deref_cst (void)
      35  {
      36    const wchar_t *p = W8 + 9;
      37    T (*p);                     /* { dg-warning "array subscript 9 is outside array bounds of .\[a-z \]+\\\[9]." } */
      38    T (p[1]);                   /* { dg-warning "array subscript 10 is outside array bounds of .\[a-z \]+\\\[9]." } */
      39    T (p[99]);                  /* { dg-warning "array subscript 108 is outside array bounds of .\[a-z \]+\\\[9]." } */
      40  }
      41  
      42  void wide_ptr_index_cst (void)
      43  {
      44    const wchar_t *p = W7;
      45  
      46    T (p[1]);
      47    T (p[8]);                   /* { dg-warning "array subscript 8 is outside array bounds of .\[a-z \]+\\\[8]." } */
      48    T (p[99]);                  /* { dg-warning "array subscript 99 is outside array bounds of .\[a-z \]+\\\[8]." } */
      49    T (p[MAX]);                 /* { dg-warning "array subscript -?\[0-9\]+ is outside array bounds of .\[a-z \]+\\\[8]." } */
      50  }
      51  
      52  
      53  void wide_direct_range (ptrdiff_t i, size_t j)
      54  {
      55    T (W9[i]);
      56    T (W9[j]);
      57  
      58    T (W9[SR (MIN, -9)]);       /* { dg-warning "array subscript -9 is below array bounds of .\[a-z \]+\\\[10]" } */
      59    T (W9[SR (MIN, -1)]);       /* { dg-warning "array subscript -1 is below array bounds of .\[a-z \]+\\\[10]" } */
      60    T (W9[SR (MIN, 0)]);
      61    T (W9[SR (-2, -1)]);        /* { dg-warning "array subscript -1 is below array bounds of .\[a-z \]+\\\[10]" } */
      62    T (W9[SR (1, 2)]);
      63    T (W9[SR (1, 9)]);
      64    T (W9[SR (1, 999)]);
      65    T (W9[SR (9, 999)]);
      66    T (W9[SR (10, 999)]);       /* { dg-warning "array subscript 10 is above array bounds of .\[a-z \]+\\\[10]" } */
      67    T (W9[SR (99, MAX)]);       /* { dg-warning "array subscript 99 is above array bounds of .\[a-z \]+\\\[10]" } */
      68  }
      69  
      70  void wide_ptr_deref_range (ptrdiff_t i, size_t j)
      71  {
      72    const wchar_t *p;
      73  
      74    p = W8 + i;
      75    T (*p);
      76  
      77    p = W8 + j;
      78    T (*p);
      79  
      80    p = W8 + SR (-9, -1);
      81    T (*p);                     /* { dg-warning "array subscript \\\[-9, -1] is outside array bounds of .\[a-z \]+\\\[9]." } */
      82  
      83    p = W8 + SR (-9, 0);
      84    T (*p);
      85  
      86    p = W8 + SR (-9, 9);
      87    T (*p);
      88  
      89    p = W8 + SR (9, 123);
      90    T (*p);                     /* { dg-warning "array subscript 9 is outside array bounds of .\[a-z \]+\\\[9]." } */
      91  }
      92  
      93  void wide_ptr_index_range (void)
      94  {
      95    const wchar_t *p;
      96  
      97    p = W7;
      98    T (p[SR (-9, -1)]);         /* { dg-warning "array subscript \\\[-9, -1] is outside array bounds of .\[a-z \]+\\\[8]." } */
      99    T (p[SR (-8, 0)]);
     100    T (p[SR (0, MAX)]);
     101    T (p[SR (1, 9)]);
     102    T (p[SR (8, 9)]);           /* { dg-warning "array subscript 8 is outside array bounds of .\[a-z \]+\\\[8]." } */
     103  
     104    p = W7 + SR (4, 6);
     105    T (p[5]);                   /* { dg-warning "array subscript \\\[9, 11] is outside array bounds of .\[a-z \]+\\\[8]." } */
     106  }
     107  
     108  void wide_ptr_index_range_1 (void)
     109  {
     110    {
     111      int i = SR (1, 2);
     112      const wchar_t *p1 = W2 + i;
     113  
     114      T (p1[0]);
     115    }
     116    {
     117      int i = SR (1, 2);
     118      const wchar_t *p1 = W2 + i;
     119  
     120      T (p1[1]);
     121    }
     122    {
     123      int i = SR (1, 2);
     124      const wchar_t *p1 = W2 + i;
     125  
     126      T (p1[2]);                /* { dg-warning "array subscript 3 is outside array bounds of .\[a-z \]+\\\[3]." } */
     127    }
     128  }
     129  
     130  void wide_ptr_index_range_chain (void)
     131  {
     132    int i = SR (1, 2);
     133    {
     134      const wchar_t *p1 = W2 + i;
     135      const wchar_t *p2 = p1 + i;
     136      const wchar_t *p3 = p2 + i;
     137  
     138      T (p1[-3]);               /* { dg-warning "array subscript \\\[-2, -1] is outside array bounds of .\[a-z \]+\\\[3]." } */
     139      T (p1[-2]);
     140      T (p1[-1]);
     141      T (p1[0]);
     142      T (p1[1]);
     143      T (p1[2]);                /* { dg-warning "array subscript 3 is outside array bounds of .\[a-z \]+\\\[3]." } */
     144  
     145      T (p2[-5]);               /* { dg-warning "array subscript \\\[-3, -2] is outside array bounds of .\[a-z \]+\\\[3]." } */
     146      T (p2[-4]);               /* { dg-warning "array subscript \\\[-2, -1] is outside array bounds of .\[a-z \]+\\\[3]." } */
     147      T (p2[-1]);
     148      T (p2[0]);
     149      T (p2[1]);                /* { dg-warning "array subscript 3 is outside array bounds of .\[a-z \]+\\\[3]." } */
     150  
     151      T (p3[0]);                /* { dg-warning "array subscript 3 is outside array bounds of .\[a-z \]+\\\[3]." } */
     152      T (p3[1]);                /* { dg-warning "array subscript 4 is outside array bounds of .\[a-z \]+\\\[3]." } */
     153      T (p3[9999]);             /* { dg-warning "array subscript 10002 is outside array bounds of .\[a-z \]+\\\[3]." "" { target size20plus} } */
     154      /* { dg-warning "array subscript \\\[-6382, -6379] is outside array bounds of .\[a-z \]+\\\[3]." "" { target { ! size20plus } } .-1 } */
     155      /* Large offsets are indistinguishable from negative values.  */
     156      T (p3[DIFF_MAX]);         /* { dg-warning "array subscript" "bug" { xfail *-*-* } } */
     157    }
     158  
     159    {
     160      const wchar_t *p1 = W3 + i;
     161      const wchar_t *p2 = p1 + i;
     162      const wchar_t *p3 = p2 + i;
     163      const wchar_t *p4 = p3 + i;
     164  
     165      T (p1[-3]);               /* { dg-warning "array subscript \\\[-2, -1] is outside array bounds of .\[a-z \]+\\\[4]." } */
     166      T (p1[-2]);
     167      T (p1[1]);
     168      T (p1[2]);
     169      T (p1[3]);                /* { dg-warning "array subscript 4 is outside array bounds of .\[a-z \]+\\\[4]." } */
     170  
     171      T (p3[1]);                /* { dg-warning "array subscript 4 is outside array bounds of .\[a-z \]+\\\[4]." } */
     172    }
     173  }
     174  
     175  void wide_ptr_index_range_4 (void)
     176  {
     177    int i = SR (1, 2);
     178    const wchar_t *p1 = W4 + i;
     179    const wchar_t *p2 = p1 + i;
     180    const wchar_t *p3 = p2 + i;
     181    const wchar_t *p4 = p3 + i;
     182  
     183    T (p4[1]);                  /* { dg-warning "array subscript 5 is outside array bounds of .\[a-z \]+\\\[5]." } */
     184  }