| 1 |
#include <stdlib.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdint.h> |
| 4 |
|
| 5 |
/* Tests related to statically allocated buffers. */ |
| 6 |
|
| 7 |
typedef struct a { |
| 8 |
int16_t s; |
| 9 |
} a; |
| 10 |
|
| 11 |
int32_t *test_1 (void) |
| 12 |
{ |
| 13 |
a A; /* { dg-message "\\d+ bytes" "note" } */ |
| 14 |
A.s = 1; |
| 15 |
int32_t *ptr = (int32_t *) &A; /* { dg-line assign1 } */ |
| 16 |
return ptr; |
| 17 |
|
| 18 |
/* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } assign1 } */ |
| 19 |
/* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target *-*-* } assign1 } */ |
| 20 |
} |
| 21 |
|
| 22 |
int32_t *test2 (void) |
| 23 |
{ |
| 24 |
char arr[sizeof (int32_t)]; |
| 25 |
int32_t *ptr = (int32_t *)arr; |
| 26 |
return ptr; |
| 27 |
} |
| 28 |
|
| 29 |
int32_t *test3 (void) |
| 30 |
{ |
| 31 |
char arr[sizeof (int16_t)]; /* { dg-message "\\d+ bytes" "note" } */ |
| 32 |
int32_t *ptr = (int32_t *)arr; /* { dg-line assign3 } */ |
| 33 |
return ptr; |
| 34 |
|
| 35 |
/* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } assign3 } */ |
| 36 |
/* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target *-*-* } assign3 } */ |
| 37 |
} |