(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
attr-nonstring-4.c
       1  /* PR middle-end/81384 - built-in form of strnlen missing
       2  
       3     Verify that a strnlen bound in excess of the maximum object size
       4     is diagnosed regardless of attribute nonstring.  Also verify that
       5     a bound that's greater than the size of a non-string array is
       6     diagnosed, even if it's not in excess of the maximum object size.
       7  
       8     { dg-do compile }
       9     { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
      10  
      11  #include "range.h"
      12  
      13  extern size_t strnlen (const char*, size_t);
      14  
      15  #define STR   /* not non-string */
      16  #define NS    __attribute__ ((nonstring))
      17  
      18  #define _CAT(s, n)   s ## n
      19  #define CAT(s, n)    _CAT (s, n)
      20  #define UNIQ(n)      CAT (n, __LINE__)
      21  
      22  void sink (size_t);
      23  
      24  #define T(attr, N, bound)			\
      25    do {						\
      26      extern attr char UNIQ (a)[N];		\
      27      sink (strnlen (UNIQ (a), bound));		\
      28    } while (0)
      29  
      30  void strnlen_cst (void)
      31  {
      32    size_t n = DIFF_MAX;
      33  
      34    T (STR, /* [] */, n);
      35    T (STR, /* [] */, n + 1);    /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
      36  
      37    T (STR, 1, n);
      38    T (STR, 2, n + 1);           /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
      39  
      40    T (NS, /* [] */, n);
      41    T (NS, /* [] */, n + 1);     /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
      42  
      43    T (NS, 9, n);                /* { dg-warning "argument 1 declared attribute 'nonstring' is smaller than the specified bound \\d+|specified bound \\d+ exceeds source size 9" } */
      44    T (NS, 10, n + 1);           /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
      45  }
      46  
      47  
      48  void strnlen_range (void)
      49  {
      50    size_t n = DIFF_MAX;
      51    n = UR (n, n + 1);
      52  
      53    T (STR, /* [] */, n);
      54    T (STR, /* [] */, n + 1);    /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
      55  
      56    T (STR, 1, n);               /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds source size 1" } */
      57    T (STR, 2, n + 1);           /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
      58  
      59    T (NS, /* [] */, n);
      60    T (NS, /* [] */, n + 1);     /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
      61  
      62    T (NS, 9, n);                /* { dg-warning "argument 1 declared attribute 'nonstring' is smaller than the specified bound \\\[\\d+, \\d+]|specified bound \\\[\\d+, \\d+] exceeds source size 9" } */
      63    T (NS, 10, n + 1);           /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
      64  }