1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -triple x86_64-apple-darwin9 -verify %s
3 // RUN: %clang_cc1 -DDYNAMIC -fsyntax-only -triple x86_64-apple-darwin9 -verify %s
4 
5 #ifndef DYNAMIC
6 #define OBJECT_SIZE_BUILTIN __builtin_object_size
7 #else
8 #define OBJECT_SIZE_BUILTIN __builtin_dynamic_object_size
9 #endif
10 
11 int a[10];
12 
f0()13 int f0() {
14   return OBJECT_SIZE_BUILTIN(&a); // expected-error {{too few arguments to function}}
15 }
f1()16 int f1() {
17   return (OBJECT_SIZE_BUILTIN(&a, 0) +
18           OBJECT_SIZE_BUILTIN(&a, 1) +
19           OBJECT_SIZE_BUILTIN(&a, 2) +
20           OBJECT_SIZE_BUILTIN(&a, 3));
21 }
f2()22 int f2() {
23   return OBJECT_SIZE_BUILTIN(&a, -1); // expected-error {{argument value -1 is outside the valid range [0, 3]}}
24 }
f3()25 int f3() {
26   return OBJECT_SIZE_BUILTIN(&a, 4); // expected-error {{argument value 4 is outside the valid range [0, 3]}}
27 }
28 
29 
30 // rdar://6252231 - cannot call vsnprintf with va_list on x86_64
f4(const char * fmt,...)31 void f4(const char *fmt, ...) {
32  __builtin_va_list args;
33  __builtin___vsnprintf_chk (0, 42, 0, 11, fmt, args); // expected-warning {{'vsnprintf' will always overflow; destination buffer has size 11, but size argument is 42}}
34 }
35 
36 // rdar://18334276
37 typedef __typeof__(sizeof(int)) size_t;
38 void * memcset(void *restrict dst, int src, size_t n);
39 void * memcpy(void *restrict dst, const void *restrict src, size_t n);
40 
41 #define memset(dest, src, len) __builtin___memset_chk(dest, src, len, OBJECT_SIZE_BUILTIN(dest, 0))
42 #define memcpy(dest, src, len) __builtin___memcpy_chk(dest, src, len, OBJECT_SIZE_BUILTIN(dest, 0))
43 #define memcpy1(dest, src, len) __builtin___memcpy_chk(dest, src, len, OBJECT_SIZE_BUILTIN(dest, 4))
44 #define NULL ((void *)0)
45 
f5(void)46 void f5(void)
47 {
48   char buf[10];
49   memset((void *)0x100000000ULL, 0, 0x1000);
50   memcpy((char *)NULL + 0x10000, buf, 0x10);
51   memcpy1((char *)NULL + 0x10000, buf, 0x10); // expected-error {{argument value 4 is outside the valid range [0, 3]}}
52 }
53 
54 // rdar://18431336
f6(void)55 void f6(void)
56 {
57   char b[5];
58   char buf[10];
59   __builtin___memccpy_chk (buf, b, '\0', sizeof(b), OBJECT_SIZE_BUILTIN (buf, 0));
60   __builtin___memccpy_chk (b, buf, '\0', sizeof(buf), OBJECT_SIZE_BUILTIN (b, 0));  // expected-warning {{'memccpy' will always overflow; destination buffer has size 5, but size argument is 10}}
61 }
62 
pr28314(void)63 int pr28314(void) {
64   struct {
65     struct InvalidField a; // expected-error{{has incomplete type}} expected-note 3{{forward declaration of 'struct InvalidField'}}
66     char b[0];
67   } *p;
68 
69   struct {
70     struct InvalidField a; // expected-error{{has incomplete type}}
71     char b[1];
72   } *p2;
73 
74   struct {
75     struct InvalidField a; // expected-error{{has incomplete type}}
76     char b[2];
77   } *p3;
78 
79   int a = 0;
80   a += OBJECT_SIZE_BUILTIN(&p->a, 0);
81   a += OBJECT_SIZE_BUILTIN(p->b, 0);
82   a += OBJECT_SIZE_BUILTIN(p2->b, 0);
83   a += OBJECT_SIZE_BUILTIN(p3->b, 0);
84   return a;
85 }
86 
pr31843()87 int pr31843() {
88   int n = 0;
89 
90   struct { int f; } a;
91   int b;
92   n += OBJECT_SIZE_BUILTIN(({&(b ? &a : &a)->f; pr31843;}), 0); // expected-warning{{expression result unused}}
93 
94   struct statfs { char f_mntonname[1024];};
95   struct statfs *outStatFSBuf;
96   n += OBJECT_SIZE_BUILTIN(outStatFSBuf->f_mntonname ? "" : "", 1); // expected-warning{{address of array}}
97   n += OBJECT_SIZE_BUILTIN(outStatFSBuf->f_mntonname ?: "", 1);
98 
99   return n;
100 }
101 
102 typedef struct {
103   char string[512];
104 } NestedArrayStruct;
105 
106 typedef struct {
107   int x;
108   NestedArrayStruct session[];
109 } IncompleteArrayStruct;
110 
rd36094951_IAS_builtin_object_size_assertion(IncompleteArrayStruct * p)111 void rd36094951_IAS_builtin_object_size_assertion(IncompleteArrayStruct *p) {
112 #define rd36094951_CHECK(mode)                                                 \
113   __builtin___strlcpy_chk(p->session[0].string, "ab", 2,                       \
114                           OBJECT_SIZE_BUILTIN(p->session[0].string, mode))
115   rd36094951_CHECK(0);
116   rd36094951_CHECK(1);
117   rd36094951_CHECK(2);
118   rd36094951_CHECK(3);
119 }
120