| 1 |
/* { dg-additional-options "-fdiagnostics-text-art-charset=unicode" } */ |
| 2 |
/* { dg-skip-if "requires hosted libstdc++ for stdlib size_t" { ! hostedlib } } */ |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <stdint.h> |
| 6 |
|
| 7 |
/* Gap of 4, then an overflow of 4. */ |
| 8 |
|
| 9 |
void test2 (size_t size) |
| 10 |
{ |
| 11 |
int32_t *buf = (int32_t *) __builtin_malloc (size * sizeof(int32_t)); /* { dg-message "\\(1\\) capacity: 'size \\* 4' bytes" "" { target c } } */ |
| 12 |
/* { dg-message "\\(1\\) capacity: '\\(size \\* 4\\)' bytes" "" { target c++ } .-1 } */ |
| 13 |
if (!buf) return; |
| 14 |
|
| 15 |
buf[size + 1] = 42; /* { dg-warning "heap-based buffer overflow" } */ |
| 16 |
__builtin_free (buf); |
| 17 |
} |
| 18 |
|
| 19 |
/* { dg-begin-multiline-output "" } |
| 20 |
|
| 21 |
┌───────────────────────┐ |
| 22 |
│write of '(int32_t) 42'│ |
| 23 |
└───────────────────────┘ |
| 24 |
│ |
| 25 |
│ |
| 26 |
v |
| 27 |
┌───────────────────────────────┐ ┌───────────────────────┐ |
| 28 |
│buffer allocated on heap at (1)│ │ after valid range │ |
| 29 |
└───────────────────────────────┘ └───────────────────────┘ |
| 30 |
├───────────────┬───────────────┤├─────┬──────┤├───────────┬───────────┤ |
| 31 |
│ │ │ |
| 32 |
╭─────────────┴────────────╮ ╭───┴───╮ ╭─────────┴─────────╮ |
| 33 |
│capacity: 'size * 4' bytes│ │4 bytes│ │overflow of 4 bytes│ |
| 34 |
╰──────────────────────────╯ ╰───────╯ ╰───────────────────╯ |
| 35 |
|
| 36 |
{ dg-end-multiline-output "" } */ |