1 /* PR 35503 - Warn about restricted pointers
2 Test to exercise that -Wrestrict warnings are issued for memory and
3 sring functions when they are declared in system headers (i.e., not
4 just when they are explicitly declared in the source file.)
5 Also verify that the warnings are issued even for calls where the
6 source of the aliasing violation is in a different function than
7 the restricted call.
8 { dg-do compile }
9 { dg-options "-O2 -Wrestrict" } */
10
11 #if __has_include (<stddef.h>)
12 # include <stddef.h>
13 #else
14 /* For cross-compilers. */
15 typedef __PTRDIFF_TYPE__ ptrdiff_t;
16 typedef __SIZE_TYPE__ size_t;
17 #endif
18
19 #if __has_include (<string.h>)
20 # include <string.h>
21 # undef memcpy
22 # undef strcat
23 # undef strcpy
24 # undef strncat
25 # undef strncpy
26 #else
27 extern void* memcpy (void*, const void*, size_t);
28 extern char* strcat (char*, const char*);
29 extern char* strcpy (char*, const char*);
30 extern char* strncat (char*, const char*, size_t);
31 extern char* strncpy (char*, const char*, size_t);
32 #endif
33
34
35 static void wrap_memcpy (void *d, const void *s, size_t n)
36 {
37 memcpy (d, s, n); /* { dg-warning "accessing 2 bytes at offsets 0 and 1 overlaps 1 byte at offset 1" "memcpy" } */
38 }
39
40 void call_memcpy (char *d)
41 {
42 const void *s = d + 1;
43 wrap_memcpy (d, s, 2);
44 }
45
46
47 static void wrap_strcat (char *d, const char *s)
48 {
49 strcat (d, s); /* { dg-warning "source argument is the same as destination" "strcat" } */
50 }
51
52 void call_strcat (char *d)
53 {
54 const char *s = d;
55 wrap_strcat (d, s);
56 }
57
58
59 static void wrap_strcpy (char *d, const char *s)
60 {
61 strcpy (d, s); /* { dg-warning "source argument is the same as destination" "strcpy" } */
62 }
63
64 void call_strcpy (char *d)
65 {
66 const char *s = d;
67 wrap_strcpy (d, s);
68 }
69
70
71 static void wrap_strncat (char *d, const char *s, size_t n)
72 {
73 strncat (d, s, n); /* { dg-warning "source argument is the same as destination" "strncat" } */
74 }
75
76 void call_strncat (char *d, size_t n)
77 {
78 const char *s = d;
79 wrap_strncat (d, s, n);
80 }
81
82
83 static void wrap_strncpy (char *d, const char *s, size_t n)
84 {
85 strncpy (d, s, n); /* { dg-warning "source argument is the same as destination" "strncpy" } */
86 }
87
88 void call_strncpy (char *d, size_t n)
89 {
90 const char *s = d;
91 wrap_strncpy (d, s, n);
92 }