• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #undef _FORTIFY_SOURCE
18 #define _FORTIFY_SOURCE 2
19 #include <fcntl.h>
20 #include <netinet/in.h>
21 #include <poll.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/socket.h>
26 #include <sys/stat.h>
27 #include <time.h>
28 #include <unistd.h>
29 
test_sprintf()30 void test_sprintf() {
31   char buf[4];
32 
33   // NOLINTNEXTLINE(whitespace/line_length)
34   // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
35   // clang should emit a warning, but doesn't
36   sprintf(buf, "foobar");  // NOLINT(runtime/printf)
37 
38   // NOLINTNEXTLINE(whitespace/line_length)
39   // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
40   // clang should emit a warning, but doesn't
41   sprintf(buf, "%s", "foobar");  // NOLINT(runtime/printf)
42 }
43 
test_snprintf()44 void test_snprintf() {
45   char buf[4];
46 
47   // NOLINTNEXTLINE(whitespace/line_length)
48   // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
49   // clang should emit a warning, but doesn't
50   snprintf(buf, 5, "foobar");  // NOLINT(runtime/printf)
51 
52   // NOLINTNEXTLINE(whitespace/line_length)
53   // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
54   // clang should emit a warning, but doesn't
55   snprintf(buf, 5, "%s", "foobar");  // NOLINT(runtime/printf)
56 
57   // NOLINTNEXTLINE(whitespace/line_length)
58   // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
59   // clang should emit a warning, but doesn't
60   snprintf(buf, 5, " %s ", "foobar");  // NOLINT(runtime/printf)
61 
62   // NOLINTNEXTLINE(whitespace/line_length)
63   // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
64   // clang should emit a warning, but doesn't
65   snprintf(buf, 5, "%d", 100000);  // NOLINT(runtime/printf)
66 }
67 
test_memcpy()68 void test_memcpy() {
69   char buf[4];
70 
71   // NOLINTNEXTLINE(whitespace/line_length)
72   // GCC: warning: call to void* __builtin___memcpy_chk(void*, const void*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
73   // clang should emit a warning, but doesn't
74   memcpy(buf, "foobar", sizeof("foobar"));
75 }
76 
test_memmove()77 void test_memmove() {
78   char buf[4];
79 
80   // NOLINTNEXTLINE(whitespace/line_length)
81   // GCC: warning: call to void* __builtin___memmove_chk(void*, const void*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
82   // clang should emit a warning, but doesn't
83   memmove(buf, "foobar", sizeof("foobar"));
84 }
85 
test_memset()86 void test_memset() {
87   char buf[4];
88 
89   // NOLINTNEXTLINE(whitespace/line_length)
90   // GCC: warning: call to void* __builtin___memset_chk(void*, int, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
91   // clang should emit a warning, but doesn't
92   memset(buf, 0, 6);
93 }
94 
test_strcpy()95 void test_strcpy() {
96   char buf[4];
97 
98   // NOLINTNEXTLINE(whitespace/line_length)
99   // GCC: warning: call to {{(char\* __builtin___strcpy_chk\(char\*, const char\*, unsigned int\))|(void\* __builtin___memcpy_chk\(void\*, const void\*, (long )?unsigned int, (long )?unsigned int\))}} will always overflow destination buffer
100   // clang should emit a warning, but doesn't
101   strcpy(buf, "foobar");  // NOLINT(runtime/printf)
102 }
103 
test_stpcpy()104 void test_stpcpy() {
105   char buf[4];
106 
107   // NOLINTNEXTLINE(whitespace/line_length)
108   // GCC: warning: call to char* __builtin___stpcpy_chk(char*, const char*, {{(long )?}}unsigned int) will always overflow destination buffer
109   // clang should emit a warning, but doesn't
110   stpcpy(buf, "foobar");
111 }
112 
test_strncpy()113 void test_strncpy() {
114   char buf[4];
115 
116   // NOLINTNEXTLINE(whitespace/line_length)
117   // GCC: warning: call to char* __builtin___strncpy_chk(char*, const char*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
118   // clang should emit a warning, but doesn't
119   strncpy(buf, "foobar", sizeof("foobar"));
120 }
121 
test_strcat()122 void test_strcat() {
123   char buf[4] = "";
124 
125   // NOLINTNEXTLINE(whitespace/line_length)
126   // GCC: warning: call to {{(char\* __builtin___strcat_chk\(char\*, const char\*, unsigned int\))|(void\* __builtin___memcpy_chk\(void\*, const void\*, (long )?unsigned int, (long )?unsigned int\))}} will always overflow destination buffer
127   // clang should emit a warning, but doesn't
128   strcat(buf, "foobar");  // NOLINT(runtime/printf)
129 }
130 
test_strncat()131 void test_strncat() {
132   char buf[4] = "";
133 
134   // NOLINTNEXTLINE(whitespace/line_length)
135   // GCC: warning: call to {{(char\* __builtin___strcat_chk\(char\*, const char\*, unsigned int\))|(void\* __builtin___memcpy_chk\(void\*, const void\*, (long )?unsigned int, (long )?unsigned int\))}} will always overflow destination buffer
136   // gcc output warning with __builtin___strcat_chk for __builtin___strncat_chk.
137   // clang should emit a warning, but doesn't
138   strncat(buf, "foobar", sizeof("foobar"));
139 }
140 
test_vsprintf(const char * fmt,...)141 void test_vsprintf(const char* fmt, ...) {
142   va_list va;
143   char buf[4];
144   va_start(va, fmt);
145 
146   // NOLINTNEXTLINE(whitespace/line_length)
147   // GCC: warning: call to int __builtin___vsprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, {{(__va_list)|(void\*)|(char\*)|(__va_list_tag\*)}}) will always overflow destination buffer
148   // clang should emit a warning, but doesn't
149   vsprintf(buf, "foobar", va);
150   va_end(va);
151 }
152 
test_vsnprintf(const char * fmt,...)153 void test_vsnprintf(const char* fmt, ...) {
154   va_list va;
155   char buf[4];
156   va_start(va, fmt);
157 
158   // NOLINTNEXTLINE(whitespace/line_length)
159   // GCC: warning: call to int __builtin___vsnprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, {{(__va_list)|(void\*)|(char\*)|(__va_list_tag\*)}}) will always overflow destination buffer
160   // clang should emit a warning, but doesn't
161   vsnprintf(buf, 5, "foobar", va);  // NOLINT(runtime/printf)
162 
163   va_end(va);
164 }
165 
test_fgets()166 void test_fgets() {
167   char buf[4];
168 
169   // NOLINTNEXTLINE(whitespace/line_length)
170   // GCC: error: call to '__fgets_too_small_error' declared with attribute error: fgets called with size less than zero
171   // clang should emit a warning, but doesn't
172   fgets(buf, -1, stdin);
173 
174   // NOLINTNEXTLINE(whitespace/line_length)
175   // GCC: error: call to '__fgets_too_big_error' declared with attribute error: fgets called with size bigger than buffer
176   // clang should emit a warning, but doesn't
177   fgets(buf, 6, stdin);
178 }
179 
test_recvfrom()180 void test_recvfrom() {
181   char buf[4];
182   sockaddr_in addr;
183 
184   // NOLINTNEXTLINE(whitespace/line_length)
185   // GCC: error: call to '__recvfrom_error' declared with attribute error: recvfrom called with size bigger than buffer
186   // clang should emit a warning, but doesn't
187   recvfrom(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), NULL);
188 }
189 
test_umask()190 void test_umask() {
191   // NOLINTNEXTLINE(whitespace/line_length)
192   // GCC: error: call to '__umask_invalid_mode' declared with attribute error: umask called with invalid mode
193   // clang should emit a warning, but doesn't
194   umask(01777);
195 }
196 
test_read()197 void test_read() {
198   char buf[4];
199   // NOLINTNEXTLINE(whitespace/line_length)
200   // GCC: error: call to '__read_dest_size_error' declared with attribute error: read called with size bigger than destination
201   // clang should emit a warning, but doesn't
202   read(0, buf, 6);
203 }
204 
test_open()205 void test_open() {
206   // NOLINTNEXTLINE(whitespace/line_length)
207   // GCC: error: call to '__creat_missing_mode' declared with attribute error: called with O_CREAT, but missing mode
208   // clang should emit a warning, but doesn't
209   open("/dev/null", O_CREAT);
210 
211   // NOLINTNEXTLINE(whitespace/line_length)
212   // GCC: error: call to '__creat_too_many_args' declared with attribute error: too many arguments
213   // clang should emit a warning, but doesn't
214   open("/dev/null", O_CREAT, 0, 0);
215 }
216 
test_poll()217 void test_poll() {
218   pollfd fds[1];
219   // NOLINTNEXTLINE(whitespace/line_length)
220   // GCC: error: call to '__poll_too_small_error' declared with attribute error: poll: pollfd array smaller than fd count
221   // clang should emit a warning, but doesn't
222   poll(fds, 2, 0);
223 }
224 
test_ppoll()225 void test_ppoll() {
226   pollfd fds[1];
227   timespec timeout;
228   // NOLINTNEXTLINE(whitespace/line_length)
229   // GCC: error: call to '__ppoll_too_small_error' declared with attribute error: ppoll: pollfd array smaller than fd count
230   // clang should emit a warning, but doesn't
231   ppoll(fds, 2, &timeout, NULL);
232 }
233