(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
tree-ssa/
builtin-sprintf-warn-29.c
       1  /* PR tree-optimization/104119 - unexpected -Wformat-overflow after strlen
       2     in ILP32 since Ranger integration
       3     { dg-do compile }
       4     { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
       5  
       6  typedef __SIZE_TYPE__ size_t;
       7  
       8  void* malloc (size_t);
       9  int sprintf (char*, const char*, ...);
      10  size_t strlen (const char*);
      11  
      12  void sink (void*, ...);
      13  
      14  struct __attribute__ ((packed)) S
      15  {
      16    char a3[3], a4[4], a5[5], a6[6], a7[7], a8[8], a9[9], ax[];
      17  };
      18  
      19  extern struct S s;
      20  extern char a4[4], a7[7], a8[8];
      21  
      22  
      23  void test_decl (void)
      24  {
      25    struct S *p = &s;
      26  
      27    {
      28      size_t n = strlen (p->a3);
      29      sprintf (a4, "%s", p->a3);    // { dg-bogus "-Wformat-overflow" }
      30      sink (a4, n);
      31    }
      32  
      33    {
      34      size_t n = strlen (p->a4);
      35      sprintf (a4, "%s", p->a4);    // { dg-bogus "-Wformat-overflow" }
      36      sink (a4, n);
      37    }
      38  
      39    {
      40      size_t n = strlen (p->a5);
      41      sprintf (a4, "%s", p->a5);    // { dg-warning "may write a terminating nul past the end" }
      42      sink (a4, n);
      43    }
      44  
      45    {
      46      size_t n = strlen (p->a7);
      47      sprintf (a8, "%s", p->a7);    // { dg-bogus "-Wformat-overflow" }
      48      sink (a8, n);
      49    }
      50  
      51    {
      52      size_t n = strlen (p->a8);
      53      sprintf (a8, "%s", p->a8);    // { dg-bogus "-Wformat-overflow" }
      54      sink (a8, n);
      55    }
      56  
      57    {
      58      size_t n = strlen (p->a9);
      59      sprintf (a8, "%s", p->a9);    // { dg-warning "may write a terminating nul past the end " }
      60      sink (a8, n);
      61    }
      62  
      63    {
      64      size_t n = strlen (p->ax);
      65      sprintf (a7, "%s", p->ax);    // { dg-bogus "-Wformat-overflow" "pr??????" { xfail ilp32 } }
      66      sink (a7, n);
      67    }
      68  }
      69  
      70  
      71  /* Verify the warning with a pointer to an allocated object with nonstant
      72     size in known range.  */
      73  
      74  void test_alloc_5_8 (int n)
      75  {
      76    if (n < 5 || 8 < n)
      77      n = 5;
      78  
      79    struct S *p = (struct S*)malloc (sizeof *p + n);
      80    sink (p);   // initialize *p
      81  
      82    {
      83      size_t n = strlen (p->a3);
      84      sprintf (a4, "%s", p->a3);    // { dg-bogus "-Wformat-overflow" }
      85      sink (a4, n);
      86    }
      87  
      88    {
      89      size_t n = strlen (p->a4);
      90      sprintf (a4, "%s", p->a4);    // { dg-bogus "-Wformat-overflow" }
      91      sink (a4, n);
      92    }
      93  
      94    {
      95      size_t n = strlen (p->a5);
      96      sprintf (a4, "%s", p->a5);    // { dg-warning "may write a terminating nul past the end" }
      97      sink (a4, n);
      98    }
      99  
     100    {
     101      size_t n = strlen (p->a7);
     102      sprintf (a8, "%s", p->a7);    // { dg-bogus "-Wformat-overflow" }
     103      sink (a8, n);
     104    }
     105  
     106    {
     107      size_t n = strlen (p->a8);
     108      sprintf (a8, "%s", p->a8);    // { dg-bogus "-Wformat-overflow" }
     109      sink (a8, n);
     110    }
     111  
     112    {
     113      size_t n = strlen (p->a9);
     114      sprintf (a8, "%s", p->a9);    // { dg-warning "may write a terminating nul past the end " }
     115      sink (a8, n);
     116    }
     117  
     118    {
     119      /* The size of the flexible array member p->ax is between 5 and 8
     120         bytes so the length of the string stored in it is at most 7.
     121         Verify the warning triggers based on its size and also gets
     122         the length right.  */
     123      size_t n = strlen (p->ax);
     124      sprintf (a4, "%s", p->ax);    // { dg-warning "writing up to 7 bytes " }
     125      sink (a4, n);
     126    }
     127  
     128    {
     129      size_t n = strlen (p->ax);
     130      sprintf (a8, "%s", p->ax);
     131      sink (a8, n);
     132    }
     133  }
     134  
     135  
     136  void test_ptr (struct S *p)
     137  {
     138    {
     139      size_t n = strlen (p->a3);
     140      sprintf (a4, "%s", p->a3);    // { dg-bogus "-Wformat-overflow" }
     141      sink (a4, n);
     142    }
     143  
     144    {
     145      size_t n = strlen (p->a4);
     146      sprintf (a4, "%s", p->a4);    // { dg-bogus "-Wformat-overflow" }
     147      sink (a4, n);
     148    }
     149  
     150    {
     151      size_t n = strlen (p->a5);
     152      sprintf (a4, "%s", p->a5);    // { dg-warning "may write a terminating nul past the end" }
     153      sink (a4, n);
     154    }
     155  
     156    {
     157      size_t n = strlen (p->a7);
     158      sprintf (a8, "%s", p->a7);    // { dg-bogus "-Wformat-overflow" }
     159      sink (a8, n);
     160    }
     161  
     162    {
     163      size_t n = strlen (p->a8);
     164      sprintf (a8, "%s", p->a8);    // { dg-bogus "-Wformat-overflow" }
     165      sink (a8, n);
     166    }
     167  
     168    {
     169      size_t n = strlen (p->a9);
     170      sprintf (a8, "%s", p->a9);    // { dg-warning "may write a terminating nul past the end " }
     171      sink (a8, n);
     172    }
     173  
     174    {
     175      size_t n = strlen (p->ax);
     176      sprintf (a8, "%s", p->ax);    // { dg-bogus "-Wformat-overflow" "pr??????" { xfail ilp32 } }
     177      sink (a8, n);
     178    }
     179  }