File: Wstringop-truncation-3.c

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (60 lines) | stat: -rw-r--r-- 1,863 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* PR c/85931 - -Wsizeof-pointer-memaccess for strncpy with size of source
   { dg-do compile }
   { dg-require-effective-target alloca }
   { dg-options "-O2 -Wall -Wstringop-truncation -ftrack-macro-expansion=0" } */

typedef __SIZE_TYPE__ size_t;

extern char* strncpy (char*, const char*, size_t);

extern char a3[3], b3[3];
extern char a5[5], b5[5];
extern char ax[], bx[];

struct SA
{
  char a3[3], b3[3];
  char a5[5], b5[5];
  char ax[];
};

void sink (void*, ...);

#define T(d, s, n)   sink (strncpy (d, s, n))

void test_array (unsigned n)
{
  T (a3, b3, 3);
  /* For the following statemenmt, GCC 8.1 issues warning:

       argument to ‘sizeof’ in ‘strncpy’ call is the same expression
       as the source; did you mean to use the size of the destination?

     Since the size of both the source and destination the warning
     isn't helpful.  Verify that it isn't issued.  */
  T (a3, b3, sizeof b3);    /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */

  T (a3, ax, sizeof a3);    /* { dg-warning "\\\[-Wstringop-truncation" } */
  T (ax, a3, sizeof a3);    /* { dg-warning "argument to .sizeof. in .strncpy. call is the same expression as the source" } */

  char an[n], bn[n];
  sink (an, bn);

  T (an, bn, sizeof bn);    /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */
}

void test_member_array (struct SA *sa, unsigned n)
{
  T (sa->a3, sa->b3, 3);
  T (sa->a3, sa->b3, sizeof sa->b3);  /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */

  T (sa->a3, sa->ax, sizeof sa->a3);  /* { dg-warning "\\\[-Wstringop-truncation" } */
  T (sa->ax, sa->a3, sizeof sa->a3);  /* { dg-warning "argument to .sizeof. in .strncpy. call is the same expression as the source" } */

  struct VarLenStruct {
    char an[n], bn[n];
  } x;

  sink (&x);
  T (x.an, x.bn, sizeof x.bn);        /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */
}