diff options
Diffstat (limited to 'test/Analysis/malloc.c')
-rw-r--r-- | test/Analysis/malloc.c | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/test/Analysis/malloc.c b/test/Analysis/malloc.c index 58d40a3..9c08bbc 100644 --- a/test/Analysis/malloc.c +++ b/test/Analysis/malloc.c @@ -270,6 +270,222 @@ void PR7217() { buf[1] = 'c'; // not crash } +void cast_emtpy_struct() { + struct st { + }; + + struct st *s = malloc(sizeof(struct st)); // no-warning + free(s); +} + +void cast_struct_1() { + struct st { + int i[100]; + char j[]; + }; + + struct st *s = malloc(sizeof(struct st)); // no-warning + free(s); +} + +void cast_struct_2() { + struct st { + int i[100]; + char j[0]; + }; + + struct st *s = malloc(sizeof(struct st)); // no-warning + free(s); +} + +void cast_struct_3() { + struct st { + int i[100]; + char j[1]; + }; + + struct st *s = malloc(sizeof(struct st)); // no-warning + free(s); +} + +void cast_struct_4() { + struct st { + int i[100]; + char j[2]; + }; + + struct st *s = malloc(sizeof(struct st)); // no-warning + free(s); +} + +void cast_struct_5() { + struct st { + char i[200]; + char j[1]; + }; + + struct st *s = malloc(sizeof(struct st) - sizeof(char)); // no-warning + free(s); +} + +void cast_struct_warn_1() { + struct st { + int i[100]; + char j[2]; + }; + + struct st *s = malloc(sizeof(struct st) + 2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}} + free(s); +} + +void cast_struct_warn_2() { + struct st { + int i[100]; + char j[2]; + }; + + struct st *s = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}} + free(s); +} + +void cast_struct_flex_array_1() { + struct st { + int i[100]; + char j[]; + }; + + struct st *s = malloc(sizeof(struct st) + 3); // no-warning + free(s); +} + +void cast_struct_flex_array_2() { + struct st { + int i[100]; + char j[0]; + }; + + struct st *s = malloc(sizeof(struct st) + 3); // no-warning + free(s); +} + +void cast_struct_flex_array_3() { + struct st { + int i[100]; + char j[1]; + }; + + struct st *s = malloc(sizeof(struct st) + 3); // no-warning + free(s); +} + +void cast_struct_flex_array_4() { + struct foo { + char f[32]; + }; + struct st { + char i[100]; + struct foo data[]; + }; + + struct st *s = malloc(sizeof(struct st) + 3 * sizeof(struct foo)); // no-warning + free(s); +} + +void cast_struct_flex_array_5() { + struct foo { + char f[32]; + }; + struct st { + char i[100]; + struct foo data[0]; + }; + + struct st *s = malloc(sizeof(struct st) + 3 * sizeof(struct foo)); // no-warning + free(s); +} + +void cast_struct_flex_array_6() { + struct foo { + char f[32]; + }; + struct st { + char i[100]; + struct foo data[1]; + }; + + struct st *s = malloc(sizeof(struct st) + 3 * sizeof(struct foo)); // no-warning + free(s); +} + +void cast_struct_flex_array_warn_1() { + struct foo { + char f[32]; + }; + struct st { + char i[100]; + struct foo data[]; + }; + + struct st *s = malloc(3 * sizeof(struct st) + 3 * sizeof(struct foo)); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}} + free(s); +} + +void cast_struct_flex_array_warn_2() { + struct foo { + char f[32]; + }; + struct st { + char i[100]; + struct foo data[0]; + }; + + struct st *s = malloc(3 * sizeof(struct st) + 3 * sizeof(struct foo)); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}} + free(s); +} + +void cast_struct_flex_array_warn_3() { + struct foo { + char f[32]; + }; + struct st { + char i[100]; + struct foo data[1]; + }; + + struct st *s = malloc(3 * sizeof(struct st) + 3 * sizeof(struct foo)); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}} + free(s); +} + +void cast_struct_flex_array_warn_4() { + struct st { + int i[100]; + int j[]; + }; + + struct st *s = malloc(sizeof(struct st) + 3); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}} + free(s); +} + +void cast_struct_flex_array_warn_5() { + struct st { + int i[100]; + int j[0]; + }; + + struct st *s = malloc(sizeof(struct st) + 3); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}} + free(s); +} + +void cast_struct_flex_array_warn_6() { + struct st { + int i[100]; + int j[1]; + }; + + struct st *s = malloc(sizeof(struct st) + 3); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}} + free(s); +} + void mallocCastToVoid() { void *p = malloc(2); const void *cp = p; // not crash |