1  /* PR tree-optimization/83431 -Wformat-truncation may incorrectly report
       2     truncation
       3     { dg-do compile }
       4     { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
       5  
       6  typedef __SIZE_TYPE__ size_t;
       7  
       8  extern int snprintf (char*, size_t, const char*, ...);
       9  extern char* strcpy (char*, const char*);
      10  
      11  struct S
      12  {
      13    char a9[9];
      14    char a5[5];
      15    int x;
      16  };
      17  
      18  
      19  void test_assign_nowarn (struct S* s)
      20  {
      21    int i = 0;
      22  
      23    {
      24      char a9[9] = "1234";
      25      snprintf (s[i].a5, sizeof (s[i].a5), "%s", a9);         /* { dg-bogus "\\\[-Wformat-truncation]" } */
      26    }
      27  
      28    {
      29      ++i;
      30      char a8[8] = "123";
      31      snprintf (s[i].a5, sizeof (s[i].a5), "%s\n", a8);       /* { dg-bogus "\\\[-Wformat-truncation]" } */
      32    }
      33  
      34    {
      35      ++i;
      36      char a7[7] = "12";
      37      snprintf (s[i].a5, sizeof (s[i].a5), "[%s]", a7);       /* { dg-bogus "\\\[-Wformat-truncation]" } */
      38    }
      39  
      40    {
      41      ++i;
      42      char a6[6] = "1";
      43      snprintf (s[i].a5, sizeof (s[i].a5), "[%s]\n", a6);     /* { dg-bogus "\\\[-Wformat-truncation]" } */
      44    }
      45  }
      46  
      47  
      48  void test_strcpy_nowarn (struct S* s)
      49  {
      50    int i = 0;
      51  
      52    strcpy (s[i].a9, "1234");
      53    snprintf (s[i].a5, sizeof (s[i].a5), "%s", s[i].a9);
      54  
      55    ++i;
      56    strcpy (s[i].a9, "123");
      57    snprintf (s[i].a5, sizeof (s[i].a5), "%s\n", s[i].a9);    /* { dg-bogus "\\\[-Wformat-truncation]" } */
      58  
      59    ++i;
      60    strcpy (s[i].a9, "12");
      61    snprintf (s[i].a5, sizeof (s[i].a5), "[%s]", s[i].a9);    /* { dg-bogus "\\\[-Wformat-truncation]" } */
      62  
      63    ++i;
      64    strcpy (s[i].a9, "1");
      65    snprintf (s[i].a5, sizeof (s[i].a5), "[%s]\n", s[i].a9);  /* { dg-bogus "\\\[-Wformat-truncation]" } */
      66  }
      67  
      68  
      69  void test_warn (struct S* s)
      70  {
      71    int i = 0;
      72    strcpy (s[i].a9, "12345678");
      73    snprintf (s[i].a5, sizeof (s[i].a5), "%s", s[i].a9);      /* { dg-warning "'%s' directive output truncated writing 8 bytes into a region of size 5" } */
      74  
      75    ++i;
      76    strcpy (s[i].a9, "1234567");
      77    snprintf (s[i].a5, sizeof (s[i].a5), "%s", s[i].a9);      /* { dg-warning "'%s' directive output truncated writing 7 bytes into a region of size 5" } */
      78  
      79    ++i;
      80    strcpy (s[i].a9, "123456");
      81    snprintf (s[i].a5, sizeof (s[i].a5), "%s", s[i].a9);      /* { dg-warning "'%s' directive output truncated writing 6 bytes into a region of size 5" } */
      82  
      83    ++i;
      84    strcpy (s[i].a9, "12345");
      85    snprintf (s[i].a5, sizeof (s[i].a5), "%s", s[i].a9);      /* { dg-warning "'snprintf' output truncated before the last format character" } */
      86  
      87    ++i;
      88    strcpy (s[i].a9, "1234");
      89    snprintf (s[i].a5, sizeof (s[i].a5), "%s\n", s[i].a9);    /* { dg-warning "output truncated before the last format character" } */
      90  
      91    ++i;
      92    strcpy (s[i].a9, "123");
      93    snprintf (s[i].a5, sizeof (s[i].a5), ">%s<", s[i].a9);    /* { dg-warning "output truncated before the last format character" } */
      94  }