| 1 |
/* { dg-additional-options "-fdiagnostics-text-art-charset=unicode" } */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <stdint.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
void test6 (size_t size) |
| 8 |
{ |
| 9 |
int32_t *buf = (int32_t *) __builtin_alloca (4 * size); |
| 10 |
memset (buf, 0, 4 * size); |
| 11 |
int32_t last = *(buf + 4 * size); /* { dg-warning "stack-based buffer over-read" } */ |
| 12 |
} |
| 13 |
|
| 14 |
/* (size * 16) - (size * 4) leads to a gap of (size * 12). */ |
| 15 |
|
| 16 |
/* { dg-begin-multiline-output "" } |
| 17 |
|
| 18 |
┌─────────────────┐ |
| 19 |
│ read of 4 bytes │ |
| 20 |
└─────────────────┘ |
| 21 |
^ |
| 22 |
│ |
| 23 |
│ |
| 24 |
┌────────────────────────────────┐ ┌─────────────────┐ |
| 25 |
│buffer allocated on stack at (1)│ │after valid range│ |
| 26 |
└────────────────────────────────┘ └─────────────────┘ |
| 27 |
├───────────────┬────────────────┤├────────┬────────┤├────────┬────────┤ |
| 28 |
│ │ │ |
| 29 |
│ │ ╭──────────┴─────────╮ |
| 30 |
│ │ │over-read of 4 bytes│ |
| 31 |
│ │ ╰────────────────────╯ |
| 32 |
│ ╭────────────────┴───────────────╮ |
| 33 |
│ │'(size * 16) - (size * 4)' bytes│ |
| 34 |
│ ╰────────────────────────────────╯ |
| 35 |
╭───────────┴──────────╮ |
| 36 |
│size: 'size * 4' bytes│ |
| 37 |
╰──────────────────────╯ |
| 38 |
|
| 39 |
{ dg-end-multiline-output "" } */ |
| 40 |
|
| 41 |
void test7 (size_t size) |
| 42 |
{ |
| 43 |
int32_t *buf = (int32_t *) __builtin_alloca (4 * size + 3); /* { dg-warning "allocated buffer size is not a multiple of the pointee's size" } */ |
| 44 |
buf[size] = 42; /* { dg-warning "stack-based buffer overflow" } */ |
| 45 |
} |
| 46 |
|
| 47 |
/* { dg-begin-multiline-output "" } |
| 48 |
|
| 49 |
┌────────────────────────────────────────┐ |
| 50 |
│ write of '(int32_t) 42' │ |
| 51 |
└────────────────────────────────────────┘ |
| 52 |
│ │ |
| 53 |
│ │ |
| 54 |
v v |
| 55 |
┌────────────────────────────────────────────────┐ ┌───────────────────┐ |
| 56 |
│ buffer allocated on stack at (1) │ │ after valid range │ |
| 57 |
└────────────────────────────────────────────────┘ └───────────────────┘ |
| 58 |
├───────────────────────┬────────────────────────┤ ├─────────┬─────────┤ |
| 59 |
│ │ |
| 60 |
╭────────────────┴───────────────╮ ╭─────────┴────────╮ |
| 61 |
│capacity: '(size * 4) + 3' bytes│ │overflow of 1 byte│ |
| 62 |
╰────────────────────────────────╯ ╰──────────────────╯ |
| 63 |
|
| 64 |
{ dg-end-multiline-output "" } */ |
| 65 |
|
| 66 |
|
| 67 |
/* We're currently not able to generate a diagram for this case; |
| 68 |
make sure we handle this case gracefully. */ |
| 69 |
|
| 70 |
char *test99 (const char *x, const char *y) |
| 71 |
{ |
| 72 |
size_t len_x = __builtin_strlen (x); |
| 73 |
size_t len_y = __builtin_strlen (y); |
| 74 |
/* BUG (root cause): forgot to add 1 for terminator. */ |
| 75 |
size_t sz = len_x + len_y; |
| 76 |
char *result = (char *) __builtin_malloc (sz); |
| 77 |
if (!result) |
| 78 |
return NULL; |
| 79 |
__builtin_memcpy (result, x, len_x); |
| 80 |
__builtin_memcpy (result + len_x, y, len_y); |
| 81 |
/* BUG (symptom): off-by-one out-of-bounds write to heap. */ |
| 82 |
result[len_x + len_y] = '\0'; /* { dg-warning "heap-based buffer overflow" } */ |
| 83 |
return result; |
| 84 |
} |