]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gcc.dg/Wstringop-overflow-14.c
d23a248446bf9382c015a76cd8c48d7a29ec0847
[thirdparty/gcc.git] / gcc / testsuite / gcc.dg / Wstringop-overflow-14.c
1 /* Test to verify that past-the-end multibyte writes via lvalues of wider
2 types than char are diagnosed.
3 { dg-do compile }
4 { dg-require-effective-target int32plus }
5 { dg-options "-O2 -Wall" } */
6
7 typedef __INT16_TYPE__ int16_t;
8 typedef __INT32_TYPE__ int32_t;
9 typedef __INT64_TYPE__ int64_t;
10 typedef __SIZE_TYPE__ size_t;
11
12 void* memcpy (void*, const void*, size_t);
13 char* strcpy (char*, const char*);
14
15 char a4[4], a8[8], a16[16];
16
17 const char s4[] = "1234";
18 const char t4[] = "4321";
19
20 void test_memcpy_cond (int i)
21 {
22 char *p = a4 + 1;
23 const char *q = i ? s4 : t4;
24 memcpy (p, q, 4); // { dg-warning "writing 4 bytes into a region of size 3" }
25 }
26
27
28 void test_int16 (void)
29 {
30 char *p = a4 + 1;
31 *(int16_t*)p = 0;
32 *(int16_t*)(p + 2) = 0; // { dg-warning "writing 2 bytes into a region of size 1" }
33 }
34
35
36 void test_int32 (void)
37 {
38 char *p = a8 + 3;
39 *(int32_t*)p = 0;
40 *(int32_t*)(p + 2) = 0; // { dg-warning "writing 4 bytes into a region of size 3" }
41 }
42
43
44 void test_int64 (void)
45 {
46 char *p = a16 + 5;
47 *(int64_t*)p = 0;
48 *(int64_t*)(p + 5) = 0; // { dg-warning "writing 8 bytes into a region of size 6" }
49 }