| 1 |
/* PR middle-end/91582 - missing heap overflow detection for strcpy |
| 2 |
PR middle-end/85484 - missing -Wstringop-overflow for strcpy with |
| 3 |
a string of non-const length |
| 4 |
{ dg-do compile } |
| 5 |
{ dg-options "-O2 -Wall -Wno-array-bounds" } */ |
| 6 |
|
| 7 |
typedef __SIZE_TYPE__ size_t; |
| 8 |
|
| 9 |
extern void* calloc (size_t, size_t); |
| 10 |
extern void* malloc (size_t); |
| 11 |
extern void* memcpy (void*, const void*, size_t); |
| 12 |
extern void* memset (void*, int, size_t); |
| 13 |
extern char* strcpy (char*, const char*); |
| 14 |
extern size_t strlen (const char*); |
| 15 |
|
| 16 |
void sink (void*); |
| 17 |
|
| 18 |
|
| 19 |
void test_memcpy_nowarn (const void *s, int i, size_t n) |
| 20 |
{ |
| 21 |
sink (memcpy (calloc (1, 1), s, 1)); |
| 22 |
sink (memcpy (calloc (1, 2), s, 1)); |
| 23 |
sink (memcpy (calloc (2, 1), s, 1)); |
| 24 |
sink (memcpy (calloc (3, 1), s, 2)); |
| 25 |
sink (memcpy (calloc (3, 1), "12", 2)); |
| 26 |
sink (memcpy (calloc (3, 1), s, 3)); |
| 27 |
sink (memcpy (calloc (3, 1), "12", 3)); |
| 28 |
sink (memcpy (calloc (i, 1), s, 1)); |
| 29 |
sink (memcpy (calloc (n, 1), s, 1)); |
| 30 |
sink (memcpy (calloc (1, n), "", 1)); |
| 31 |
sink (memcpy (calloc (1, i), "", 1)); |
| 32 |
sink (memcpy (calloc (i, 1), "123", 3)); |
| 33 |
sink (memcpy (calloc (n, 1), "123", 3)); |
| 34 |
sink (memcpy (calloc (1, i), "123456", 7)); |
| 35 |
sink (memcpy (calloc (1, n), "123456", 7)); |
| 36 |
sink (memcpy (calloc (n, 1), s, 12345)); |
| 37 |
sink (memcpy (calloc (1, n), s, n - 1)); |
| 38 |
sink (memcpy (calloc (n, 1), s, n)); |
| 39 |
|
| 40 |
sink (memcpy ((char*)calloc (1, 1) + i, "123", 1)); |
| 41 |
sink (memcpy ((char*)calloc (n, 1) + i, "123", n)); |
| 42 |
|
| 43 |
sink (memcpy ((char*)calloc (1, 1) + i, s, 1)); |
| 44 |
sink (memcpy ((char*)calloc (n, 1) + i, s, n)); |
| 45 |
|
| 46 |
sink (memcpy (malloc (1), s, 1)); |
| 47 |
sink (memcpy (malloc (2), s, 1)); |
| 48 |
sink (memcpy (malloc (3), s, 2)); |
| 49 |
sink (memcpy (malloc (3), "12", 2)); |
| 50 |
sink (memcpy (malloc (3), s, 3)); |
| 51 |
sink (memcpy (malloc (3), "12", 3)); |
| 52 |
sink (memcpy (malloc (n), s, 1)); |
| 53 |
sink (memcpy (malloc (n), "", 1)); |
| 54 |
sink (memcpy (malloc (n), "123", 3)); |
| 55 |
sink (memcpy (malloc (n), "123456", 7)); |
| 56 |
sink (memcpy (malloc (n), s, 12345)); |
| 57 |
sink (memcpy (malloc (n), s, n - 1)); |
| 58 |
sink (memcpy (malloc (n), s, n)); |
| 59 |
|
| 60 |
{ |
| 61 |
const int a[] = { 1, 2, 3, 4 }; |
| 62 |
void *p = (char*)malloc (sizeof a); |
| 63 |
memcpy (p, a, sizeof a); |
| 64 |
sink (p); |
| 65 |
} |
| 66 |
|
| 67 |
{ |
| 68 |
const int a[] = { 1, 2, 3, 4, 5 }; |
| 69 |
size_t nelts = sizeof a / sizeof *a; |
| 70 |
int vla[nelts]; |
| 71 |
memcpy (vla, a, nelts * sizeof *vla); |
| 72 |
sink (vla); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
void test_memcpy_warn (const int *s, size_t n) |
| 78 |
{ |
| 79 |
{ |
| 80 |
void *p = (char*)malloc (0); |
| 81 |
memcpy (p, s, 1); // { dg-warning "writing 1 byte into a region of size 0" } |
| 82 |
sink (p); |
| 83 |
} |
| 84 |
|
| 85 |
{ |
| 86 |
void *p = (char*)malloc (1); |
| 87 |
memcpy (p, s, 2); // { dg-warning "writing 2 bytes into a region of size 1" } |
| 88 |
sink (p); |
| 89 |
} |
| 90 |
|
| 91 |
{ |
| 92 |
void *p = (char*)malloc (2); |
| 93 |
memcpy (p, s, 3); // { dg-warning "writing 3 bytes into a region of size 2" } |
| 94 |
sink (p); |
| 95 |
} |
| 96 |
|
| 97 |
{ |
| 98 |
void *p = (char*)malloc (3); |
| 99 |
memcpy (p, s, 4); // { dg-warning "writing 4 bytes into a region of size 3" } |
| 100 |
sink (p); |
| 101 |
} |
| 102 |
|
| 103 |
{ |
| 104 |
const int a[] = { 1, 2, 3, 4 }; |
| 105 |
void *p = (char*)malloc (sizeof *a); |
| 106 |
memcpy (p, a, sizeof a); // { dg-warning "" } |
| 107 |
sink (p); |
| 108 |
} |
| 109 |
|
| 110 |
{ |
| 111 |
const int a[] = { 1, 2, 3, 4, 5 }; |
| 112 |
size_t nelts = sizeof a / sizeof *a; |
| 113 |
char vla[nelts]; |
| 114 |
memcpy (vla, a, nelts * sizeof *a); // { dg-warning "" } |
| 115 |
sink (vla); |
| 116 |
} |
| 117 |
|
| 118 |
{ |
| 119 |
void *p = malloc (n); |
| 120 |
memcpy (p, s, n * sizeof *s); // { dg-warning "\\\[-Wstringop-overflow" "" { xfail *-*-* } } |
| 121 |
sink (p); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void test_memset_nowarn (int x, size_t n) |
| 126 |
{ |
| 127 |
sink (memset (calloc (1, 1), x, 1)); |
| 128 |
sink (memset (calloc (1, 2), x, 1)); |
| 129 |
sink (memset (calloc (2, 1), x, 1)); |
| 130 |
sink (memset (calloc (3, 1), x, 2)); |
| 131 |
sink (memset (calloc (3, 1), x, 3)); |
| 132 |
sink (memset (calloc (n, 1), x, 1)); |
| 133 |
sink (memset (calloc (n, 1), x, 12345)); |
| 134 |
sink (memset (calloc (1, n), x, n - 1)); |
| 135 |
sink (memset (calloc (n, 1), x, n)); |
| 136 |
|
| 137 |
sink (memset (malloc (1), x, 1)); |
| 138 |
sink (memset (malloc (2), x, 1)); |
| 139 |
sink (memset (malloc (3), x, 2)); |
| 140 |
sink (memset (malloc (3), x, 3)); |
| 141 |
sink (memset (malloc (n), x, 1)); |
| 142 |
sink (memset (malloc (n), x, 12345)); |
| 143 |
sink (memset (malloc (n), x, n - 1)); |
| 144 |
sink (memset (malloc (n), x, n)); |
| 145 |
|
| 146 |
{ |
| 147 |
const int a[] = { 1, 2, 3, 4 }; |
| 148 |
void *p = (char*)malloc (sizeof a); |
| 149 |
memset (p, x, sizeof a); |
| 150 |
sink (p); |
| 151 |
} |
| 152 |
|
| 153 |
{ |
| 154 |
const int a[] = { 1, 2, 3, 4, 5 }; |
| 155 |
size_t nelts = sizeof a / sizeof *a; |
| 156 |
int vla[nelts]; |
| 157 |
memset (vla, x, nelts * sizeof *vla); |
| 158 |
sink (vla); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
void test_memset_warn (int x, size_t n) |
| 164 |
{ |
| 165 |
{ |
| 166 |
void *p = (char*)malloc (0); |
| 167 |
memset (p, x, 1); // { dg-warning "writing 1 byte into a region of size 0" } |
| 168 |
sink (p); |
| 169 |
} |
| 170 |
|
| 171 |
{ |
| 172 |
void *p = (char*)malloc (1); |
| 173 |
memset (p, x, 2); // { dg-warning "writing 2 bytes into a region of size 1" } |
| 174 |
sink (p); |
| 175 |
} |
| 176 |
|
| 177 |
{ |
| 178 |
void *p = (char*)malloc (2); |
| 179 |
memset (p, x, 3); // { dg-warning "writing 3 bytes into a region of size 2" } |
| 180 |
sink (p); |
| 181 |
} |
| 182 |
|
| 183 |
{ |
| 184 |
void *p = (char*)malloc (3); |
| 185 |
memset (p, x, 4); // { dg-warning "writing 4 bytes into a region of size 3" } |
| 186 |
sink (p); |
| 187 |
} |
| 188 |
|
| 189 |
{ |
| 190 |
const int a[] = { 1, 2, 3, 4 }; |
| 191 |
void *p = (char*)malloc (sizeof *a); |
| 192 |
memset (p, 0, sizeof a); // { dg-warning "" } |
| 193 |
sink (p); |
| 194 |
} |
| 195 |
|
| 196 |
{ |
| 197 |
const int a[] = { 1, 2, 3, 4, 5 }; |
| 198 |
size_t nelts = sizeof a / sizeof *a; |
| 199 |
char vla[nelts]; |
| 200 |
memset (vla, 0, nelts * sizeof *a); // { dg-warning "" } |
| 201 |
sink (vla); |
| 202 |
} |
| 203 |
|
| 204 |
{ |
| 205 |
void *p = malloc (n); |
| 206 |
memset (p, x, n * sizeof (int)); // { dg-warning "\\\[-Wstringop-overflow" "" { xfail *-*-* } } |
| 207 |
sink (p); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
void test_strcpy_nowarn (const char *s) |
| 213 |
{ |
| 214 |
{ |
| 215 |
const char a[] = "12"; |
| 216 |
int n = strlen (a); |
| 217 |
char *t = (char*)calloc (2, n); |
| 218 |
strcpy (t, a); |
| 219 |
sink (t); |
| 220 |
} |
| 221 |
|
| 222 |
{ |
| 223 |
const char a[] = "123"; |
| 224 |
unsigned n = strlen (a) + 1; |
| 225 |
char *t = (char*)calloc (n, 1); |
| 226 |
strcpy (t, a); |
| 227 |
sink (t); |
| 228 |
} |
| 229 |
|
| 230 |
{ |
| 231 |
const char a[] = "1234"; |
| 232 |
size_t n = strlen (a) * 2; |
| 233 |
char *t = (char*)malloc (n); |
| 234 |
strcpy (t, a); |
| 235 |
sink (t); |
| 236 |
} |
| 237 |
|
| 238 |
{ |
| 239 |
const char a[] = "1234"; |
| 240 |
size_t len = strlen (a) + 1; |
| 241 |
char vla[len]; |
| 242 |
strcpy (vla, a); |
| 243 |
sink (vla); |
| 244 |
} |
| 245 |
|
| 246 |
{ |
| 247 |
size_t n = strlen (s) + 1; |
| 248 |
char *t = (char*)malloc (n); |
| 249 |
strcpy (t, s); |
| 250 |
sink (t); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
void test_strcpy_warn (const char *s) |
| 256 |
{ |
| 257 |
{ |
| 258 |
const char a[] = "123"; |
| 259 |
/* Verify that using signed int for the strlen result works (i.e., |
| 260 |
that the conversion from signed int to size_t doesn't prevent |
| 261 |
the detection. */ |
| 262 |
int n = strlen (a); |
| 263 |
char *t = (char*)calloc (n, 1); // { dg-message "destination object of size 3 allocated by 'calloc'" "note" } |
| 264 |
strcpy (t, a); // { dg-warning "writing 4 bytes into a region of size (between 0 and )?3 " } |
| 265 |
|
| 266 |
sink (t); |
| 267 |
} |
| 268 |
|
| 269 |
{ |
| 270 |
const char a[] = "1234"; |
| 271 |
size_t n = strlen (a); |
| 272 |
char *t = (char*)malloc (n); // { dg-message "destination object of size 4 allocated by 'malloc'" "note" } |
| 273 |
strcpy (t, a); // { dg-warning "writing 5 bytes into a region of size (between 0 and )?4 " } |
| 274 |
sink (t); |
| 275 |
} |
| 276 |
|
| 277 |
// Exercise PR middle-end/85484. |
| 278 |
{ |
| 279 |
size_t len = strlen (s); |
| 280 |
char vla[len]; // { dg-message "destination object 'vla'" "vla note" } |
| 281 |
strcpy (vla, s); // { dg-warning "writing one too many bytes into a region of a size that depends on 'strlen'" } |
| 282 |
sink (vla); |
| 283 |
} |
| 284 |
|
| 285 |
{ |
| 286 |
size_t n = strlen (s); |
| 287 |
char *t = (char*)malloc (n); // { dg-message "allocated by 'malloc'" "malloc note" } |
| 288 |
strcpy (t, s); // { dg-warning "writing one too many bytes into a region of a size that depends on 'strlen'" } |
| 289 |
sink (t); |
| 290 |
} |
| 291 |
} |