1  /* { dg-additional-options "-Wno-stringop-overflow -Wno-stringop-truncation" } */
       2  #include <string.h>
       3  
       4  /* Wanalyzer-out-of-bounds tests for strpy-related overflows.
       5    
       6     The intra-procedural tests are all caught by Wstringop-overflow.
       7     The inter-procedural out-of-bounds are only found by the analyzer.  */
       8  
       9  void test1 (void)
      10  {
      11    char dst[5];
      12    strcpy (dst, "Hello"); /* { dg-line test1 } */
      13  
      14    /* { dg-warning "stack-based buffer overflow" "warning" { target *-*-* } test1 } */
      15    /* { dg-message "write of 1 byte to beyond the end of 'dst'" "num bad bytes note" { target *-*-* } test1 } */
      16    /* { dg-message "valid subscripts for 'dst' are '\\\[0\\\]' to '\\\[4\\\]'" "valid subscript note" { target *-*-* } test1 } */
      17  }
      18  
      19  void test2 (void)
      20  {
      21    char dst[6];
      22    strcpy (dst, "Hello");
      23  }
      24  
      25  void test3 (void)
      26  {
      27    char *src = "Hello";
      28    char dst[5];
      29    strcpy (dst, src); /* { dg-line test3 } */
      30  
      31    /* { dg-warning "stack-based buffer overflow" "warning" { target *-*-* } test3 } */
      32    /* { dg-message "write of 1 byte to beyond the end of 'dst'" "num bad bytes note" { target *-*-* } test3 } */
      33    /* { dg-message "valid subscripts for 'dst' are '\\\[0\\\]' to '\\\[4\\\]'" "valid subscript note" { target *-*-* } test3 } */
      34  }
      35  
      36  void test4 (void)
      37  {
      38    char *src = "Hello";
      39    char dst[6];
      40    strcpy (dst, src);
      41  }
      42  
      43  const char *return_hello (void)
      44  {
      45    return "hello";
      46  }
      47  
      48  void test5 (void)
      49  {
      50    const char *str = return_hello ();
      51    if (!str)
      52      return;
      53    char dst[5];
      54    strcpy (dst, str); /* { dg-line test5 } */
      55  
      56    /* { dg-warning "stack-based buffer overflow" "warning" { target *-*-* } test5 } */
      57    /* { dg-message "write of 1 byte to beyond the end of 'dst'" "num bad bytes note" { target *-*-* } test5 } */
      58    /* { dg-message "valid subscripts for 'dst' are '\\\[0\\\]' to '\\\[4\\\]'" "valid subscript note" { target *-*-* } test5 } */
      59  }
      60  
      61  void test6 (void)
      62  {
      63    const char *str = return_hello ();
      64    if (!str)
      65      return;
      66    char dst[6];
      67    strcpy (dst, str);
      68  }