xref: /llvm-project/clang/test/Sema/builtin-object-size.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
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(void)13 int f0(void) {
14   return OBJECT_SIZE_BUILTIN(&a); // expected-error {{too few arguments to function}}
15 }
f1(void)16 int f1(void) {
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(void)22 int f2(void) {
23   return OBJECT_SIZE_BUILTIN(&a, -1); // expected-error {{argument value -1 is outside the valid range [0, 3]}}
24 }
f3(void)25 int f3(void) {
26   return OBJECT_SIZE_BUILTIN(&a, 4); // expected-error {{argument value 4 is outside the valid range [0, 3]}}
27 }
28 
29 
30 // 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 typedef __typeof__(sizeof(int)) size_t;
37 void * memcset(void *restrict dst, int src, size_t n);
38 void * memcpy(void *restrict dst, const void *restrict src, size_t n);
39 
40 #define memset(dest, src, len) __builtin___memset_chk(dest, src, len, OBJECT_SIZE_BUILTIN(dest, 0))
41 #define memcpy(dest, src, len) __builtin___memcpy_chk(dest, src, len, OBJECT_SIZE_BUILTIN(dest, 0))
42 #define memcpy1(dest, src, len) __builtin___memcpy_chk(dest, src, len, OBJECT_SIZE_BUILTIN(dest, 4))
43 #define NULL ((void *)0)
44 
f5(void)45 void f5(void)
46 {
47   char buf[10];
48   memset((void *)0x100000000ULL, 0, 0x1000);
49   memcpy((char *)NULL + 0x10000, buf, 0x10);
50   memcpy1((char *)NULL + 0x10000, buf, 0x10); // expected-error {{argument value 4 is outside the valid range [0, 3]}}
51 }
52 
f6(void)53 void f6(void)
54 {
55   char b[5];
56   char buf[10];
57   __builtin___memccpy_chk (buf, b, '\0', sizeof(b), OBJECT_SIZE_BUILTIN (buf, 0));
58   __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}}
59 }
60 
pr28314(void)61 int pr28314(void) {
62   struct {
63     struct InvalidField a; // expected-error{{has incomplete type}} expected-note 3{{forward declaration of 'struct InvalidField'}}
64     char b[0];
65   } *p;
66 
67   struct {
68     struct InvalidField a; // expected-error{{has incomplete type}}
69     char b[1];
70   } *p2;
71 
72   struct {
73     struct InvalidField a; // expected-error{{has incomplete type}}
74     char b[2];
75   } *p3;
76 
77   int a = 0;
78   a += OBJECT_SIZE_BUILTIN(&p->a, 0);
79   a += OBJECT_SIZE_BUILTIN(p->b, 0);
80   a += OBJECT_SIZE_BUILTIN(p2->b, 0);
81   a += OBJECT_SIZE_BUILTIN(p3->b, 0);
82   return a;
83 }
84 
pr31843(void)85 int pr31843(void) {
86   int n = 0;
87 
88   struct { int f; } a;
89   int b;
90   n += OBJECT_SIZE_BUILTIN(({&(b ? &a : &a)->f; pr31843;}), 0); // expected-warning{{expression result unused}}
91 
92   struct statfs { char f_mntonname[1024];};
93   struct statfs *outStatFSBuf;
94   n += OBJECT_SIZE_BUILTIN(outStatFSBuf->f_mntonname ? "" : "", 1); // expected-warning{{address of array}}
95   n += OBJECT_SIZE_BUILTIN(outStatFSBuf->f_mntonname ?: "", 1);
96 
97   return n;
98 }
99 
100 typedef struct {
101   char string[512];
102 } NestedArrayStruct;
103 
104 typedef struct {
105   int x;
106   NestedArrayStruct session[];
107 } IncompleteArrayStruct;
108 
rd36094951_IAS_builtin_object_size_assertion(IncompleteArrayStruct * p)109 void rd36094951_IAS_builtin_object_size_assertion(IncompleteArrayStruct *p) {
110 #define rd36094951_CHECK(mode)                                                 \
111   __builtin___strlcpy_chk(p->session[0].string, "ab", 2,                       \
112                           OBJECT_SIZE_BUILTIN(p->session[0].string, mode))
113   rd36094951_CHECK(0);
114   rd36094951_CHECK(1);
115   rd36094951_CHECK(2);
116   rd36094951_CHECK(3);
117 }
118