blob: 8babdde276fadda3c34c3ba48d88342a23bbc827 [file] [log] [blame]
Ted Kremenek00fd7732011-08-19 14:35:32 +00001// RUN: %clang_cc1 -Wstrlcpy-strlcat-size -verify -fsyntax-only %s
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00002
3typedef __SIZE_TYPE__ size_t;
4size_t strlcpy (char * restrict dst, const char * restrict src, size_t size);
5size_t strlcat (char * restrict dst, const char * restrict src, size_t size);
6size_t strlen (const char *s);
7
8char s1[100];
9char s2[200];
10char * s3;
11
12struct {
13 char f1[100];
14 char f2[100][3];
15} s4, **s5;
16
17int x;
18
19void f(void)
20{
21 strlcpy(s1, s2, sizeof(s1)); // no warning
22 strlcpy(s1, s2, sizeof(s2)); // expected-warning {{size argument in 'strlcpy' call appears to be size of the source; expected the size of the destination}} expected-note {{change size argument to be the size of the destination}}
23 strlcpy(s1, s3, strlen(s3)+1); // expected-warning {{size argument in 'strlcpy' call appears to be size of the source; expected the size of the destination}} expected-note {{change size argument to be the size of the destination}}
24 strlcat(s2, s3, sizeof(s3)); // expected-warning {{size argument in 'strlcat' call appears to be size of the source; expected the size of the destination}} expected-note {{change size argument to be the size of the destination}}
25 strlcpy(s4.f1, s2, sizeof(s2)); // expected-warning {{size argument in 'strlcpy' call appears to be size of the source; expected the size of the destination}} expected-note {{change size argument to be the size of the destination}}
26 strlcpy((*s5)->f2[x], s2, sizeof(s2)); // expected-warning {{size argument in 'strlcpy' call appears to be size of the source; expected the size of the destination}} expected-note {{change size argument to be the size of the destination}}
27 strlcpy(s1+3, s2, sizeof(s2)); // expected-warning {{size argument in 'strlcpy' call appears to be size of the source; expected the size of the destination}}
28}
Ted Kremenek8f746222011-08-18 22:48:41 +000029
30// Don't issue FIXIT for flexible arrays.
31struct S {
32 int y;
33 char x[];
34};
35
36void flexible_arrays(struct S *s) {
37 char str[] = "hi";
38 strlcpy(s->x, str, sizeof(str)); // expected-warning {{size argument in 'strlcpy' call appears to be size of the source; expected the size of the destination}}
39}
40
41// Don't issue FIXIT for destinations of size 1.
42void size_1() {
43 char z[1];
44 char str[] = "hi";
45
46 strlcpy(z, str, sizeof(str)); // expected-warning {{size argument in 'strlcpy' call appears to be size of the source; expected the size of the destination}}
47}
48
49// Support VLAs.
50void vlas(int size) {
51 char z[size];
52 char str[] = "hi";
53
54 strlcpy(z, str, sizeof(str)); // expected-warning {{size argument in 'strlcpy' call appears to be size of the source; expected the size of the destination}} expected-note {{change size argument to be the size of the destination}}
55}