File: Wstringop-overread-2.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 (117 lines) | stat: -rw-r--r-- 3,661 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Verify -Wstringop-overread is issued for reading more than the maximum
   object size but not for writing.
  { dg-do compile }
  { dg-options "-O2 -Wno-stringop-overflow -ftrack-macro-expansion=0" } */

#define PTRDIFF_MAX   __PTRDIFF_MAX__
#define SIZE_MAX      __SIZE_MAX__

#define NOIPA         __attribute__ ((noipa))

typedef __SIZE_TYPE__ size_t;

void* memchr (const void*, int, size_t);
int memcmp (const void*, const void*, size_t);
void* memcpy (const void*, const void*, size_t);

int strncmp (const char*, const char*, size_t);
char* strncat (char*, const char*, size_t);
char* strncpy (char*, const char*, size_t);
size_t strnlen (const char*, size_t);

void sink (int, ...);
#define sink(...) sink (0, __VA_ARGS__)
#define T(exp)   sink (exp)

NOIPA void test_memchr (const void *p, int x)
{
  size_t dmax = PTRDIFF_MAX;
  size_t smax = SIZE_MAX;

  T (memchr (p, x, dmax));

  T (memchr (p, x, dmax + 1));     // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
  T (memchr (p, x, dmax * 2));     // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
  T (memchr (p, x, smax));         // { dg-warning "\\\[-Wstringop-overread" }
}


NOIPA void test_memcmp (const void *p, const void *q)
{
  size_t dmax = PTRDIFF_MAX;
  size_t smax = SIZE_MAX;

  T (memcmp (p, q, dmax));

  T (memcmp (p, q, dmax + 1));     // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
  T (memcmp (p, q, dmax * 2));     // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
  T (memcmp (p, q, smax));         // { dg-warning "\\\[-Wstringop-overread" }
}


NOIPA void test_memcpy (void *p, const void *q)
{
  size_t dmax = PTRDIFF_MAX;
  size_t smax = SIZE_MAX;

  T (memcpy (p, q, dmax));

  T (memcpy (p, q, dmax + 1));    // -Wstringop-overflow disabled
  T (memcpy (p, q, dmax * 2));    // ditto
  T (memcpy (p, q, smax));        // ditto
}


NOIPA void test_strncmp (const char *p, const char *q)
{
  size_t dmax = PTRDIFF_MAX;
  size_t smax = SIZE_MAX;

  T (strncmp (p, q, dmax));

  T (strncmp (p, q, dmax + 1));   // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" "strncmp" }
  T (strncmp (p, q, dmax * 2));   // { dg-warning "\\\[-Wstringop-overread" "strncmp" }
  T (strncmp (p, q, smax));       // { dg-warning "\\\[-Wstringop-overread" "strncmp" }
}

NOIPA void test_strncat (char *p, const char *q)
{
  size_t dmax = PTRDIFF_MAX;
  size_t smax = SIZE_MAX;

  T (strncat (p, q, dmax));

  T (strncat (p, q, dmax + 1));   // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
  T (strncat (p, q, dmax * 2));   // { dg-warning "\\\[-Wstringop-overread" }
  T (strncat (p, q, smax));       // { dg-warning "\\\[-Wstringop-overread" }
}

NOIPA void test_strncpy (char *p, const char *q)
{
#if 0
  /* Disabled: strncpy calls with an excissve bound trigger both
     -Wstringop-overflow and, when the former option is disabled,
     -Wstringop-overread.  The latter should probably not trigger.  */

  size_t dmax = PTRDIFF_MAX;
  size_t smax = SIZE_MAX;

  T (strncpy (p, q, dmax));

  T (strncpy (p, q, dmax + 1));    // -Wstringop-overflow disabled
  T (strncpy (p, q, dmax * 2));    // ditto
  T (strncpy (p, q, smax));        // ditto
#endif
}

NOIPA void test_strnlen (const char *p)
{
  size_t dmax = PTRDIFF_MAX;
  size_t smax = SIZE_MAX;

  T (strnlen (p, dmax));

  T (strnlen (p, dmax + 1));      // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
  T (strnlen (p, dmax * 2));      // { dg-warning "\\\[-Wstringop-overread" }
  T (strnlen (p, smax));          // { dg-warning "\\\[-Wstringop-overread" }
}