(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Warray-bounds-89.c
       1  /* Verify warnings and notes for MIN_EXPRs involving either pointers
       2     to distinct objects or one to a known object and the other to
       3     an unknown one.  The relational expressions are strictly invalid
       4     but that should be diagnosed by a separate warning.
       5     { dg-do compile }
       6     { dg-options "-O2 -Warray-bounds -Wno-stringop-overflow" } */
       7  
       8  /* Verify the note points to the larger of the two objects and mentions
       9     the offset into it (alhough the offset would ideally be a part of
      10     the warning).  */
      11  extern char a3[3];
      12  extern char a5[5];  // { dg-message "at offset 5 into object 'a5' of size 5" "note" }
      13  
      14  void min_a3_a5 (int i)
      15  {
      16    char *p = a3 + i;
      17    char *q = a5 + i;
      18  
      19    /* The relational expression below is invalid and should be diagnosed
      20       by its own warning independently of -Warray-bounds.  */
      21    char *d = p < q ? p : q;
      22  
      23    d[4] = 0;
      24  
      25    /* Verify the type in the warning corresponds to the larger of the two
      26       objects.  */
      27    d[5] = 0;         // { dg-warning "subscript 5 is outside array bounds of 'char\\\[5]'" }
      28  }
      29  
      30  
      31  // Same as above but with the larger array as the first MIN_EXPR operand.
      32  extern char b4[4];
      33  extern char b6[6];  // { dg-message "at offset 6 into object 'b6' of size 6" "note" }
      34  
      35  void min_b6_b4 (int i)
      36  {
      37    char *p = b6 + i;
      38    char *q = b4 + i;
      39    char *d = p < q ? p : q;
      40  
      41    d[5] = 0;
      42    d[6] = 0;         // { dg-warning "subscript 6 is outside array bounds of 'char\\\[6]'" }
      43  }
      44  
      45  
      46  /* Same as above but with the first MIN_EXPR operand pointing to an unknown
      47     object.  */
      48  extern char c7[7];  // { dg-message "at offset 7 into object 'c7' of size 7" "note" }
      49  
      50  void min_p_c7 (char *p, int i)
      51  {
      52    char *q = c7 + i;
      53    char *d = p < q ? p : q;
      54  
      55    d[6] = 0;
      56    d[7] = 0;         // { dg-warning "subscript 7 is outside array bounds of 'char\\\[7]'" }
      57  }
      58  
      59  
      60  /* Same as above but with the second MIN_EXPR operand pointing to an unknown
      61     object.  */
      62  extern char d8[8];  // { dg-message "at offset 8 into object 'd8' of size 8" "note" }
      63  
      64  void min_d8_p (char *q, int i)
      65  {
      66    char *p = d8 + i;
      67    char *d = p < q ? p : q;
      68  
      69    d[7] = 0;
      70    d[8] = 0;         // { dg-warning "subscript 8 is outside array bounds of 'char\\\[8]'" }
      71  }
      72  
      73  
      74  /* The following are diagnosed by -Wstringop-overflow but, as a result
      75     of PR 101374, not by -Warray-bounds.  */
      76  
      77  struct A3_5
      78  {
      79    char a3[3];
      80    char a5[5];  // { dg-message "at offset 5 into object 'a5' of size 5" "note" { xfail *-*-* } }
      81  };
      82  
      83  void min_A3_A5 (int i, struct A3_5 *pa3_5)
      84  {
      85    char *p = pa3_5->a3 + i;
      86    char *q = pa3_5->a5 + i;
      87  
      88    char *d = p < q ? p : q;
      89  
      90    // d[4] = 0;
      91    d[5] = 0;         // { dg-warning "subscript 5 is outside array bounds of 'char\\\[5]'" "pr??????" { xfail *-*-* } }
      92  }
      93  
      94  
      95  struct B4_B6
      96  {
      97    char b4[4];
      98    char b6[6];       // { dg-message "at offset 6 into object 'b6' of size 6" "note" { xfail *-*-* } }
      99  };
     100  
     101  void min_B6_B4 (int i, struct B4_B6 *pb4_b6)
     102  {
     103    char *p = pb4_b6->b6 + i;
     104    char *q = pb4_b6->b4 + i;
     105    char *d = p < q ? p : q;
     106  
     107    d[5] = 0;
     108    d[6] = 0;         // { dg-warning "subscript 6 is outside array bounds of 'char\\\[6]'" "pr??????" { xfail *-*-* } }
     109  }
     110  
     111  
     112  struct C7
     113  {
     114    char c7[7];       // { dg-message "at offset 7 into object 'c7' of size 7" "note" { xfail *-*-* } }
     115  };
     116  
     117  void min_p_C7 (char *p, int i, struct C7 *pc7)
     118  {
     119    char *q = pc7->c7 + i;
     120    char *d = p < q ? p : q;
     121  
     122    d[6] = 0;
     123    d[7] = 0;         // { dg-warning "subscript 7 is outside array bounds of 'char\\\[7]'" "pr??????" { xfail *-*-* } }
     124  }
     125  
     126  
     127  struct D8
     128  {
     129    char d8[8];       // { dg-message "at offset 8 into object 'd8' of size 8" "note" { xfail *-*-* } }
     130  };
     131  
     132  void min_D8_p (char *q, int i, struct D8 *pd8)
     133  {
     134    char *p = pd8->d8 + i;
     135    char *d = p < q ? p : q;
     136  
     137    d[7] = 0;
     138    d[8] = 0;         // { dg-warning "subscript 8 is outside array bounds of 'char\\\[8]'" "pr??????" { xfail *-*-* } }
     139  }