| 1 |
/* The multiline output assumes sizeof(size_t) == 8. |
| 2 |
{ dg-require-effective-target lp64 } */ |
| 3 |
|
| 4 |
/* { dg-additional-options "-fdiagnostics-text-art-charset=unicode" } */ |
| 5 |
/* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */ |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
#include <stdint.h> |
| 10 |
|
| 11 |
struct str { |
| 12 |
size_t len; |
| 13 |
char data[]; |
| 14 |
}; |
| 15 |
|
| 16 |
struct str * |
| 17 |
make_str_badly (const char *src) |
| 18 |
{ |
| 19 |
size_t len = strlen(src); |
| 20 |
struct str *str = (struct str *) malloc(sizeof(str) + len); /* { dg-message "\\(1\\) capacity: 'len \\+ 8' bytes" "" { target c } } */ |
| 21 |
/* { dg-message "\\(1\\) capacity: '\\(len \\+ 8\\)' bytes" "" { target c++ } .-1 } */ |
| 22 |
if (!str) |
| 23 |
return NULL; |
| 24 |
str->len = len; |
| 25 |
memcpy(str->data, src, len); |
| 26 |
str->data[len] = '\0'; /* { dg-warning "heap-based buffer overflow" } */ |
| 27 |
return str; |
| 28 |
} |
| 29 |
|
| 30 |
/* { dg-begin-multiline-output "" } |
| 31 |
|
| 32 |
┌──────────────────────────────────┐ |
| 33 |
│ write of '(char) 0' │ |
| 34 |
└──────────────────────────────────┘ |
| 35 |
│ |
| 36 |
│ |
| 37 |
v |
| 38 |
┌──────────────────────────────────┐┌──────────────────────────────────┐ |
| 39 |
│ buffer allocated on heap at (1) ││ after valid range │ |
| 40 |
└──────────────────────────────────┘└──────────────────────────────────┘ |
| 41 |
├────────────────┬─────────────────┤├────────────────┬─────────────────┤ |
| 42 |
│ │ |
| 43 |
╭────────────┴────────────╮ ╭─────────┴────────╮ |
| 44 |
│capacity: 'len + 8' bytes│ │overflow of 1 byte│ |
| 45 |
╰─────────────────────────╯ ╰──────────────────╯ |
| 46 |
|
| 47 |
{ dg-end-multiline-output "" } */ |