(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
strcmpopt_6.c
       1  /* Verify that strcmp and strncmp calls with mixed constant and
       2     non-constant strings are evaluated correctly.
       3     { dg-do run }
       4     { dg-options "-O2" } */
       5  
       6  #include "strlenopt.h"
       7  
       8  #define A(expr)                                                 \
       9    ((expr)                                                       \
      10     ? (void)0                                                    \
      11     : (__builtin_printf ("assertion failed on line %i: %s\n",    \
      12                          __LINE__, #expr),                       \
      13        __builtin_abort ()))
      14  
      15  __attribute__ ((noclone, noinline)) int
      16  test_strlen_gt2_strcmp_abcd (const char *s)
      17  {
      18    if (strlen (s) < 3)
      19      return -1;
      20  
      21    return strcmp (s, "abcd") == 0;
      22  }
      23  
      24  __attribute__ ((noclone, noinline)) int
      25  test_strlen_lt6_strcmp_abcd (const char *s)
      26  {
      27    if (strlen (s) > 5)
      28      return -1;
      29  
      30    return strcmp (s, "abcd") == 0;
      31  }
      32  
      33  __attribute__ ((noclone, noinline)) int
      34  test_strcpy_strcmp_abc (const char *s)
      35  {
      36    char a[5];
      37    strcpy (a, s);
      38    return strcmp (a, "abc") == 0;
      39  }
      40  
      41  __attribute__ ((noclone, noinline)) int
      42  test_strcpy_abc_strcmp (const char *s)
      43  {
      44    char a[4], b[6];
      45    strcpy (a, "abc");
      46    strcpy (b, s);
      47    return strcmp (a, b) == 0;
      48  }
      49  
      50  /* Exercise strcmp of two strings between 1 and 3 characters long
      51     stored in arrays of the same known size.  */
      52  char ga4[4], gb4[4];
      53  
      54  __attribute__ ((noclone, noinline)) int
      55  test_store_0_nulterm_strcmp_same_size_arrays (void)
      56  {
      57    ga4[0] = gb4[0] = 'x';
      58    ga4[3] = gb4[3] = '\0';
      59    return strcmp (ga4, gb4) == 0;
      60  }
      61  
      62  __attribute__ ((noclone, noinline)) int
      63  test_store_0_nulterm_strncmp_bound_2_same_size_arrays (void)
      64  {
      65    ga4[0] = gb4[0] = 'x';
      66    ga4[3] = gb4[3] = '\0';
      67    return strncmp (ga4, gb4, 2) == 0;
      68  }
      69  
      70  __attribute__ ((noclone, noinline)) int
      71  test_store_0_nulterm_strncmp_bound_equal_same_size_arrays (void)
      72  {
      73    ga4[0] = gb4[0] = 'x';
      74    ga4[3] = gb4[3] = '\0';
      75    return strncmp (ga4, gb4, 4) == 0;
      76  }
      77  
      78  /* Exercise strcmp of two strings between 0 and 3 characters long
      79     stored in arrays of the same known size.  */
      80  
      81  __attribute__ ((noclone, noinline)) int
      82  test_nulterm_strcmp_same_size_arrays (void)
      83  {
      84    ga4[3] = gb4[3] = '\0';
      85    return strcmp (ga4, gb4) == 0;
      86  }
      87  
      88  /* Exercise strcmp of two strings between 1 and 3 and 1 and 4 characters
      89     long, respectively, stored in arrays of known but different sizes.  */
      90  char gc5[5];
      91  
      92  __attribute__ ((noclone, noinline)) int
      93  test_store_0_nulterm_strcmp_arrays (void)
      94  {
      95    ga4[0] = gc5[0] = 'x';
      96    ga4[3] = gc5[4] = '\0';
      97    return strcmp (ga4, gc5) == 0;
      98  }
      99  
     100  /* Exercise strcmp of two strings between 0 and 3 and 1 and 4 characters
     101     long, respectively, stored in arrays of known but different sizes.  */
     102  
     103  __attribute__ ((noclone, noinline)) int
     104  test_nulterm_strcmp_arrays (void)
     105  {
     106    ga4[3] = gc5[4] = '\0';
     107    return strcmp (ga4, gc5) == 0;
     108  }
     109  
     110  
     111  __attribute__ ((noclone, noinline)) int
     112  test_strcpy_strncmp_abcd (const char *s)
     113  {
     114    char a[6];
     115    strcpy (a, s);
     116    return strcmp (a, "abcd") == 0;
     117  }
     118  
     119  __attribute__ ((noclone, noinline)) int
     120  test_strcpy_abcd_strncmp_3 (const char *s)
     121  {
     122    char a[6], b[8];
     123    strcpy (a, "abcd");
     124    strcpy (b, s);
     125    return strncmp (a, b, 3) == 0;
     126  }
     127  
     128  __attribute__ ((noclone, noinline)) int
     129  test_strcpy_abcd_strncmp_4 (const char *s)
     130  {
     131    char a[6], b[8];
     132    strcpy (a, "abcd");
     133    strcpy (b, s);
     134    return strncmp (a, b, 4) == 0;
     135  }
     136  
     137  
     138  int main (void)
     139  {
     140    test_strlen_gt2_strcmp_abcd ("abcd");
     141    test_strlen_lt6_strcmp_abcd ("abcd");
     142  
     143    A (0 == test_strcpy_strcmp_abc ("ab"));
     144    A (0 != test_strcpy_strcmp_abc ("abc"));
     145    A (0 == test_strcpy_strcmp_abc ("abcd"));
     146  
     147    A (0 == test_strcpy_abc_strcmp ("ab"));
     148    A (0 != test_strcpy_abc_strcmp ("abc"));
     149    A (0 == test_strcpy_abc_strcmp ("abcd"));
     150  
     151    strcpy (ga4, "abc"); strcpy (gb4, "abd");
     152    A (0 == test_store_0_nulterm_strcmp_same_size_arrays ());
     153    strcpy (ga4, "abd"); strcpy (gb4, "abc");
     154    A (0 == test_store_0_nulterm_strcmp_same_size_arrays ());
     155    strcpy (ga4, "abc"); strcpy (gb4, "abc");
     156    A (0 != test_store_0_nulterm_strcmp_same_size_arrays ());
     157  
     158    strcpy (ga4, "abc"); strcpy (gb4, "acd");
     159    A (0 == test_store_0_nulterm_strncmp_bound_2_same_size_arrays ());
     160    strcpy (ga4, "acd"); strcpy (gb4, "abc");
     161    A (0 == test_store_0_nulterm_strncmp_bound_2_same_size_arrays ());
     162    strcpy (ga4, "abc"); strcpy (gb4, "abc");
     163    A (0 != test_store_0_nulterm_strncmp_bound_2_same_size_arrays ());
     164  
     165    strcpy (ga4, "abc"); strcpy (gb4, "abd");
     166    A (0 == test_store_0_nulterm_strncmp_bound_equal_same_size_arrays ());
     167    strcpy (ga4, "abd"); strcpy (gb4, "abc");
     168    A (0 == test_store_0_nulterm_strncmp_bound_equal_same_size_arrays ());
     169    strcpy (ga4, "abc"); strcpy (gb4, "abc");
     170    A (0 != test_store_0_nulterm_strncmp_bound_equal_same_size_arrays ());
     171  
     172    strcpy (ga4, "abc"); strcpy (gb4, "abd");
     173    A (0 == test_nulterm_strcmp_same_size_arrays ());
     174    strcpy (ga4, "abd"); strcpy (gb4, "abc");
     175    A (0 == test_nulterm_strcmp_same_size_arrays ());
     176    strcpy (ga4, "abc"); strcpy (gb4, "abc");
     177    A (0 != test_nulterm_strcmp_same_size_arrays ());
     178  
     179    strcpy (ga4, "abc"); strcpy (gc5, "abcd");
     180    A (0 == test_store_0_nulterm_strcmp_arrays ());
     181    strcpy (ga4, "abd"); strcpy (gc5, "abcd");
     182    A (0 == test_store_0_nulterm_strcmp_arrays ());
     183    strcpy (ga4, "abc"); strcpy (gc5, "abc");
     184    A (0 != test_store_0_nulterm_strcmp_arrays ());
     185  
     186    strcpy (ga4, "abc"); strcpy (gc5, "abcd");
     187    A (0 == test_nulterm_strcmp_arrays ());
     188    strcpy (ga4, "abd"); strcpy (gc5, "abc");
     189    A (0 == test_nulterm_strcmp_arrays ());
     190    strcpy (ga4, "abc"); strcpy (gc5, "abc");
     191    A (0 != test_nulterm_strcmp_arrays ());
     192  
     193    A (0 == test_strcpy_strncmp_abcd ("ab"));
     194    A (0 == test_strcpy_strncmp_abcd ("abc"));
     195    A (0 != test_strcpy_strncmp_abcd ("abcd"));
     196    A (0 == test_strcpy_strncmp_abcd ("abcde"));
     197  
     198    A (0 == test_strcpy_abcd_strncmp_3 ("ab"));
     199    A (0 != test_strcpy_abcd_strncmp_3 ("abc"));
     200    A (0 != test_strcpy_abcd_strncmp_3 ("abcd"));
     201    A (0 != test_strcpy_abcd_strncmp_3 ("abcde"));
     202  
     203    A (0 == test_strcpy_abcd_strncmp_4 ("ab"));
     204    A (0 == test_strcpy_abcd_strncmp_4 ("abc"));
     205    A (0 != test_strcpy_abcd_strncmp_4 ("abcd"));
     206    A (0 != test_strcpy_abcd_strncmp_4 ("abcde"));
     207  }