1 /*
2 * Copyright (C) 2013 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 #include <gtest/gtest.h>
18 #include <string.h>
19 #include <stdarg.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22
23 // We have to say "DeathTest" here so gtest knows to run this test (which exits)
24 // in its own process. Unfortunately, the C preprocessor doesn't give us an
25 // easy way to concatenate strings, so we need to use the complicated method
26 // below. *sigh*
27 #define DEATHTEST_PASTER(name) name##_DeathTest
28 #define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name)
29 #define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME)
30
31 #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2
32 struct foo {
33 char empty[0];
34 char one[1];
35 char a[10];
36 char b[10];
37 };
38
39 #ifndef __clang__
40 // This test is disabled in clang because clang doesn't properly detect
41 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,strncpy_fortified2)42 TEST(DEATHTEST, strncpy_fortified2) {
43 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
44 foo myfoo;
45 int copy_amt = atoi("11");
46 ASSERT_EXIT(strncpy(myfoo.a, "01234567890", copy_amt),
47 testing::KilledBySignal(SIGABRT), "");
48 }
49 #endif
50
51 #ifndef __clang__
52 // This test is disabled in clang because clang doesn't properly detect
53 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,sprintf_fortified2)54 TEST(DEATHTEST, sprintf_fortified2) {
55 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
56 foo myfoo;
57 char source_buf[15];
58 memcpy(source_buf, "12345678901234", 15);
59 ASSERT_EXIT(sprintf(myfoo.a, "%s", source_buf),
60 testing::KilledBySignal(SIGABRT), "");
61 }
62 #endif
63
64 #ifndef __clang__
65 // This test is disabled in clang because clang doesn't properly detect
66 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,sprintf2_fortified2)67 TEST(DEATHTEST, sprintf2_fortified2) {
68 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
69 foo myfoo;
70 ASSERT_EXIT(sprintf(myfoo.a, "0123456789"),
71 testing::KilledBySignal(SIGABRT), "");
72 }
73 #endif
74
75 #ifndef __clang__
76 // These tests are disabled in clang because clang doesn't properly detect
77 // this buffer overflow. TODO: Fix clang.
vsprintf_helper2(const char * fmt,...)78 static int vsprintf_helper2(const char *fmt, ...) {
79 foo myfoo;
80 va_list va;
81 int result;
82
83 va_start(va, fmt);
84 result = vsprintf(myfoo.a, fmt, va); // should crash here
85 va_end(va);
86 return result;
87 }
88
TEST(DEATHTEST,vsprintf_fortified2)89 TEST(DEATHTEST, vsprintf_fortified2) {
90 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
91 ASSERT_EXIT(vsprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
92 }
93
TEST(DEATHTEST,vsprintf2_fortified2)94 TEST(DEATHTEST, vsprintf2_fortified2) {
95 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
96 ASSERT_EXIT(vsprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), "");
97 }
98 #endif
99
100 #ifndef __clang__
101 // These tests are disabled in clang because clang doesn't properly detect
102 // this buffer overflow. TODO: Fix clang.
vsnprintf_helper2(const char * fmt,...)103 static int vsnprintf_helper2(const char *fmt, ...) {
104 foo myfoo;
105 va_list va;
106 int result;
107 size_t size = atoi("11");
108
109 va_start(va, fmt);
110 result = vsnprintf(myfoo.a, size, fmt, va); // should crash here
111 va_end(va);
112 return result;
113 }
114
TEST(DEATHTEST,vsnprintf_fortified2)115 TEST(DEATHTEST, vsnprintf_fortified2) {
116 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
117 ASSERT_EXIT(vsnprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
118 }
119
TEST(DEATHTEST,vsnprintf2_fortified2)120 TEST(DEATHTEST, vsnprintf2_fortified2) {
121 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
122 ASSERT_EXIT(vsnprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), "");
123 }
124 #endif
125
126 #if __BIONIC__
127
128 #ifndef __clang__
129 // zero sized target with "\0" source (should fail)
130 // This test is disabled in clang because clang doesn't properly detect
131 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,strcpy_fortified2)132 TEST(DEATHTEST, strcpy_fortified2) {
133 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
134 foo myfoo;
135 char* src = strdup("");
136 ASSERT_EXIT(strcpy(myfoo.empty, src),
137 testing::KilledBySignal(SIGABRT), "");
138 free(src);
139 }
140 #endif
141
142 #ifndef __clang__
143 // zero sized target with longer source (should fail)
144 // This test is disabled in clang because clang doesn't properly detect
145 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,strcpy2_fortified2)146 TEST(DEATHTEST, strcpy2_fortified2) {
147 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
148 foo myfoo;
149 char* src = strdup("1");
150 ASSERT_EXIT(strcpy(myfoo.empty, src),
151 testing::KilledBySignal(SIGABRT), "");
152 free(src);
153 }
154 #endif
155
156 #ifndef __clang__
157 // one byte target with longer source (should fail)
158 // This test is disabled in clang because clang doesn't properly detect
159 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,strcpy3_fortified2)160 TEST(DEATHTEST, strcpy3_fortified2) {
161 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
162 foo myfoo;
163 char* src = strdup("12");
164 ASSERT_EXIT(strcpy(myfoo.one, src),
165 testing::KilledBySignal(SIGABRT), "");
166 free(src);
167 }
168 #endif
169
170 #ifndef __clang__
171 // This test is disabled in clang because clang doesn't properly detect
172 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,strchr_fortified2)173 TEST(DEATHTEST, strchr_fortified2) {
174 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
175 foo myfoo;
176 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
177 myfoo.b[0] = '\0';
178 ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')),
179 testing::KilledBySignal(SIGABRT), "");
180 }
181 #endif
182
183 #ifndef __clang__
184 // This test is disabled in clang because clang doesn't properly detect
185 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,strrchr_fortified2)186 TEST(DEATHTEST, strrchr_fortified2) {
187 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
188 foo myfoo;
189 memcpy(myfoo.a, "0123456789", 10);
190 memcpy(myfoo.b, "01234", 6);
191 ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')),
192 testing::KilledBySignal(SIGABRT), "");
193 }
194 #endif
195
196 #ifndef __clang__
197 // This test is disabled in clang because clang doesn't properly detect
198 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,strlcpy_fortified2)199 TEST(DEATHTEST, strlcpy_fortified2) {
200 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
201 foo myfoo;
202 strcpy(myfoo.a, "01");
203 size_t n = strlen(myfoo.a);
204 ASSERT_EXIT(strlcpy(myfoo.one, myfoo.a, n),
205 testing::KilledBySignal(SIGABRT), "");
206 }
207 #endif
208
209 #ifndef __clang__
210 // This test is disabled in clang because clang doesn't properly detect
211 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,strlcat_fortified2)212 TEST(DEATHTEST, strlcat_fortified2) {
213 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
214 foo myfoo;
215 strcpy(myfoo.a, "01");
216 myfoo.one[0] = '\0';
217 size_t n = strlen(myfoo.a);
218 ASSERT_EXIT(strlcat(myfoo.one, myfoo.a, n),
219 testing::KilledBySignal(SIGABRT), "");
220 }
221 #endif
222
223 #endif /* __BIONIC__ */
224
225 #ifndef __clang__
226 // This test is disabled in clang because clang doesn't properly detect
227 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,strncat_fortified2)228 TEST(DEATHTEST, strncat_fortified2) {
229 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
230 foo myfoo;
231 size_t n = atoi("10"); // avoid compiler optimizations
232 strncpy(myfoo.a, "012345678", n);
233 ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGABRT), "");
234 }
235 #endif
236
237 #ifndef __clang__
238 // This test is disabled in clang because clang doesn't properly detect
239 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,strncat2_fortified2)240 TEST(DEATHTEST, strncat2_fortified2) {
241 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
242 foo myfoo;
243 myfoo.a[0] = '\0';
244 size_t n = atoi("10"); // avoid compiler optimizations
245 ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
246 }
247 #endif
248
TEST(DEATHTEST,strncat3_fortified2)249 TEST(DEATHTEST, strncat3_fortified2) {
250 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
251 foo myfoo;
252 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
253 myfoo.b[0] = '\0';
254 size_t n = atoi("10"); // avoid compiler optimizations
255 ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), "");
256 }
257
258 #ifndef __clang__
259 // This test is disabled in clang because clang doesn't properly detect
260 // this buffer overflow. TODO: Fix clang.
TEST(DEATHTEST,strcat_fortified2)261 TEST(DEATHTEST, strcat_fortified2) {
262 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
263 char src[11];
264 strcpy(src, "0123456789");
265 foo myfoo;
266 myfoo.a[0] = '\0';
267 ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGABRT), "");
268 }
269 #endif
270
TEST(DEATHTEST,strcat2_fortified2)271 TEST(DEATHTEST, strcat2_fortified2) {
272 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
273 foo myfoo;
274 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
275 myfoo.b[0] = '\0';
276 ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), "");
277 }
278
TEST(DEATHTEST,snprintf_fortified2)279 TEST(DEATHTEST, snprintf_fortified2) {
280 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
281 foo myfoo;
282 strcpy(myfoo.a, "012345678");
283 size_t n = strlen(myfoo.a) + 2;
284 ASSERT_EXIT(snprintf(myfoo.b, n, "a%s", myfoo.a), testing::KilledBySignal(SIGABRT), "");
285 }
286
TEST(DEATHTEST,bzero_fortified2)287 TEST(DEATHTEST, bzero_fortified2) {
288 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
289 foo myfoo;
290 memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
291 size_t n = atoi("11");
292 ASSERT_EXIT(bzero(myfoo.b, n), testing::KilledBySignal(SIGABRT), "");
293 }
294
295 #endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */
296
297 #if __BIONIC__
298 // multibyte target where we over fill (should fail)
TEST(DEATHTEST,strcpy_fortified)299 TEST(DEATHTEST, strcpy_fortified) {
300 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
301 char buf[10];
302 char *orig = strdup("0123456789");
303 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
304 free(orig);
305 }
306
307 // zero sized target with "\0" source (should fail)
TEST(DEATHTEST,strcpy2_fortified)308 TEST(DEATHTEST, strcpy2_fortified) {
309 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
310 char buf[0];
311 char *orig = strdup("");
312 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
313 free(orig);
314 }
315
316 // zero sized target with longer source (should fail)
TEST(DEATHTEST,strcpy3_fortified)317 TEST(DEATHTEST, strcpy3_fortified) {
318 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
319 char buf[0];
320 char *orig = strdup("1");
321 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
322 free(orig);
323 }
324
325 // one byte target with longer source (should fail)
TEST(DEATHTEST,strcpy4_fortified)326 TEST(DEATHTEST, strcpy4_fortified) {
327 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
328 char buf[1];
329 char *orig = strdup("12");
330 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
331 free(orig);
332 }
333
TEST(DEATHTEST,strlen_fortified)334 TEST(DEATHTEST, strlen_fortified) {
335 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
336 char buf[10];
337 memcpy(buf, "0123456789", sizeof(buf));
338 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
339 }
340
TEST(DEATHTEST,strchr_fortified)341 TEST(DEATHTEST, strchr_fortified) {
342 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
343 char buf[10];
344 memcpy(buf, "0123456789", sizeof(buf));
345 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
346 }
347
TEST(DEATHTEST,strrchr_fortified)348 TEST(DEATHTEST, strrchr_fortified) {
349 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
350 char buf[10];
351 memcpy(buf, "0123456789", sizeof(buf));
352 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
353 }
354
TEST(DEATHTEST,strlcpy_fortified)355 TEST(DEATHTEST, strlcpy_fortified) {
356 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
357 char bufa[15];
358 char bufb[10];
359 strcpy(bufa, "01234567890123");
360 size_t n = strlen(bufa);
361 ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
362 }
363
TEST(DEATHTEST,strlcat_fortified)364 TEST(DEATHTEST, strlcat_fortified) {
365 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
366 char bufa[15];
367 char bufb[10];
368 bufb[0] = '\0';
369 strcpy(bufa, "01234567890123");
370 size_t n = strlen(bufa);
371 ASSERT_EXIT(strlcat(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
372 }
373
374 #endif
375
TEST(DEATHTEST,sprintf_fortified)376 TEST(DEATHTEST, sprintf_fortified) {
377 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
378 char buf[10];
379 char source_buf[15];
380 memcpy(source_buf, "12345678901234", 15);
381 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
382 }
383
TEST(DEATHTEST,sprintf2_fortified)384 TEST(DEATHTEST, sprintf2_fortified) {
385 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
386 char buf[5];
387 ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), "");
388 }
389
vsprintf_helper(const char * fmt,...)390 static int vsprintf_helper(const char *fmt, ...) {
391 char buf[10];
392 va_list va;
393 int result;
394
395 va_start(va, fmt);
396 result = vsprintf(buf, fmt, va); // should crash here
397 va_end(va);
398 return result;
399 }
400
TEST(DEATHTEST,vsprintf_fortified)401 TEST(DEATHTEST, vsprintf_fortified) {
402 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
403 ASSERT_EXIT(vsprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
404 }
405
TEST(DEATHTEST,vsprintf2_fortified)406 TEST(DEATHTEST, vsprintf2_fortified) {
407 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
408 ASSERT_EXIT(vsprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
409 }
410
vsnprintf_helper(const char * fmt,...)411 static int vsnprintf_helper(const char *fmt, ...) {
412 char buf[10];
413 va_list va;
414 int result;
415 size_t size = atoi("11");
416
417 va_start(va, fmt);
418 result = vsnprintf(buf, size, fmt, va); // should crash here
419 va_end(va);
420 return result;
421 }
422
TEST(DEATHTEST,vsnprintf_fortified)423 TEST(DEATHTEST, vsnprintf_fortified) {
424 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
425 ASSERT_EXIT(vsnprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
426 }
427
TEST(DEATHTEST,vsnprintf2_fortified)428 TEST(DEATHTEST, vsnprintf2_fortified) {
429 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
430 ASSERT_EXIT(vsnprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
431 }
432
TEST(DEATHTEST,strncat_fortified)433 TEST(DEATHTEST, strncat_fortified) {
434 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
435 char buf[10];
436 size_t n = atoi("10"); // avoid compiler optimizations
437 strncpy(buf, "012345678", n);
438 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
439 }
440
TEST(DEATHTEST,strncat2_fortified)441 TEST(DEATHTEST, strncat2_fortified) {
442 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
443 char buf[10];
444 buf[0] = '\0';
445 size_t n = atoi("10"); // avoid compiler optimizations
446 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
447 }
448
TEST(DEATHTEST,strcat_fortified)449 TEST(DEATHTEST, strcat_fortified) {
450 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
451 char src[11];
452 strcpy(src, "0123456789");
453 char buf[10];
454 buf[0] = '\0';
455 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
456 }
457
TEST(DEATHTEST,memmove_fortified)458 TEST(DEATHTEST, memmove_fortified) {
459 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
460 char buf[20];
461 strcpy(buf, "0123456789");
462 size_t n = atoi("10");
463 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
464 }
465
TEST(DEATHTEST,memcpy_fortified)466 TEST(DEATHTEST, memcpy_fortified) {
467 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
468 char bufa[10];
469 char bufb[10];
470 strcpy(bufa, "012345678");
471 size_t n = atoi("11");
472 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
473 }
474
TEST(DEATHTEST,strncpy_fortified)475 TEST(DEATHTEST, strncpy_fortified) {
476 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
477 char bufa[15];
478 char bufb[10];
479 strcpy(bufa, "01234567890123");
480 size_t n = strlen(bufa);
481 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
482 }
483
TEST(DEATHTEST,snprintf_fortified)484 TEST(DEATHTEST, snprintf_fortified) {
485 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
486 char bufa[15];
487 char bufb[10];
488 strcpy(bufa, "0123456789");
489 size_t n = strlen(bufa) + 1;
490 ASSERT_EXIT(snprintf(bufb, n, "%s", bufa), testing::KilledBySignal(SIGABRT), "");
491 }
492
TEST(DEATHTEST,bzero_fortified)493 TEST(DEATHTEST, bzero_fortified) {
494 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
495 char buf[10];
496 memcpy(buf, "0123456789", sizeof(buf));
497 size_t n = atoi("11");
498 ASSERT_EXIT(bzero(buf, n), testing::KilledBySignal(SIGABRT), "");
499 }
500
TEST(DEATHTEST,umask_fortified)501 TEST(DEATHTEST, umask_fortified) {
502 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
503 mode_t mask = atoi("1023"); // 01777 in octal
504 ASSERT_EXIT(umask(mask), testing::KilledBySignal(SIGABRT), "");
505 }
506
507 extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
508 extern "C" char* __strcat_chk(char*, const char*, size_t);
509
TEST(TEST_NAME,strncat)510 TEST(TEST_NAME, strncat) {
511 char buf[10];
512 memset(buf, 'A', sizeof(buf));
513 buf[0] = 'a';
514 buf[1] = '\0';
515 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
516 ASSERT_EQ(buf, res);
517 ASSERT_EQ('a', buf[0]);
518 ASSERT_EQ('0', buf[1]);
519 ASSERT_EQ('1', buf[2]);
520 ASSERT_EQ('2', buf[3]);
521 ASSERT_EQ('3', buf[4]);
522 ASSERT_EQ('4', buf[5]);
523 ASSERT_EQ('\0', buf[6]);
524 ASSERT_EQ('A', buf[7]);
525 ASSERT_EQ('A', buf[8]);
526 ASSERT_EQ('A', buf[9]);
527 }
528
TEST(TEST_NAME,strncat2)529 TEST(TEST_NAME, strncat2) {
530 char buf[10];
531 memset(buf, 'A', sizeof(buf));
532 buf[0] = 'a';
533 buf[1] = '\0';
534 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
535 ASSERT_EQ(buf, res);
536 ASSERT_EQ('a', buf[0]);
537 ASSERT_EQ('0', buf[1]);
538 ASSERT_EQ('1', buf[2]);
539 ASSERT_EQ('2', buf[3]);
540 ASSERT_EQ('3', buf[4]);
541 ASSERT_EQ('4', buf[5]);
542 ASSERT_EQ('\0', buf[6]);
543 ASSERT_EQ('A', buf[7]);
544 ASSERT_EQ('A', buf[8]);
545 ASSERT_EQ('A', buf[9]);
546 }
547
TEST(TEST_NAME,strncat3)548 TEST(TEST_NAME, strncat3) {
549 char buf[10];
550 memset(buf, 'A', sizeof(buf));
551 buf[0] = '\0';
552 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
553 ASSERT_EQ(buf, res);
554 ASSERT_EQ('0', buf[0]);
555 ASSERT_EQ('1', buf[1]);
556 ASSERT_EQ('2', buf[2]);
557 ASSERT_EQ('3', buf[3]);
558 ASSERT_EQ('4', buf[4]);
559 ASSERT_EQ('\0', buf[5]);
560 ASSERT_EQ('A', buf[6]);
561 ASSERT_EQ('A', buf[7]);
562 ASSERT_EQ('A', buf[8]);
563 ASSERT_EQ('A', buf[9]);
564 }
565
TEST(TEST_NAME,strncat4)566 TEST(TEST_NAME, strncat4) {
567 char buf[10];
568 memset(buf, 'A', sizeof(buf));
569 buf[9] = '\0';
570 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
571 ASSERT_EQ(buf, res);
572 ASSERT_EQ('A', buf[0]);
573 ASSERT_EQ('A', buf[1]);
574 ASSERT_EQ('A', buf[2]);
575 ASSERT_EQ('A', buf[3]);
576 ASSERT_EQ('A', buf[4]);
577 ASSERT_EQ('A', buf[5]);
578 ASSERT_EQ('A', buf[6]);
579 ASSERT_EQ('A', buf[7]);
580 ASSERT_EQ('A', buf[8]);
581 ASSERT_EQ('\0', buf[9]);
582 }
583
TEST(TEST_NAME,strncat5)584 TEST(TEST_NAME, strncat5) {
585 char buf[10];
586 memset(buf, 'A', sizeof(buf));
587 buf[0] = 'a';
588 buf[1] = '\0';
589 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
590 ASSERT_EQ(buf, res);
591 ASSERT_EQ('a', buf[0]);
592 ASSERT_EQ('0', buf[1]);
593 ASSERT_EQ('1', buf[2]);
594 ASSERT_EQ('2', buf[3]);
595 ASSERT_EQ('3', buf[4]);
596 ASSERT_EQ('4', buf[5]);
597 ASSERT_EQ('5', buf[6]);
598 ASSERT_EQ('6', buf[7]);
599 ASSERT_EQ('7', buf[8]);
600 ASSERT_EQ('\0', buf[9]);
601 }
602
TEST(TEST_NAME,strncat6)603 TEST(TEST_NAME, strncat6) {
604 char buf[10];
605 memset(buf, 'A', sizeof(buf));
606 buf[0] = 'a';
607 buf[1] = '\0';
608 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
609 ASSERT_EQ(buf, res);
610 ASSERT_EQ('a', buf[0]);
611 ASSERT_EQ('0', buf[1]);
612 ASSERT_EQ('1', buf[2]);
613 ASSERT_EQ('2', buf[3]);
614 ASSERT_EQ('3', buf[4]);
615 ASSERT_EQ('4', buf[5]);
616 ASSERT_EQ('5', buf[6]);
617 ASSERT_EQ('6', buf[7]);
618 ASSERT_EQ('7', buf[8]);
619 ASSERT_EQ('\0', buf[9]);
620 }
621
622
TEST(TEST_NAME,strcat)623 TEST(TEST_NAME, strcat) {
624 char buf[10];
625 memset(buf, 'A', sizeof(buf));
626 buf[0] = 'a';
627 buf[1] = '\0';
628 char* res = __strcat_chk(buf, "01234", sizeof(buf));
629 ASSERT_EQ(buf, res);
630 ASSERT_EQ('a', buf[0]);
631 ASSERT_EQ('0', buf[1]);
632 ASSERT_EQ('1', buf[2]);
633 ASSERT_EQ('2', buf[3]);
634 ASSERT_EQ('3', buf[4]);
635 ASSERT_EQ('4', buf[5]);
636 ASSERT_EQ('\0', buf[6]);
637 ASSERT_EQ('A', buf[7]);
638 ASSERT_EQ('A', buf[8]);
639 ASSERT_EQ('A', buf[9]);
640 }
641
TEST(TEST_NAME,strcat2)642 TEST(TEST_NAME, strcat2) {
643 char buf[10];
644 memset(buf, 'A', sizeof(buf));
645 buf[0] = 'a';
646 buf[1] = '\0';
647 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
648 ASSERT_EQ(buf, res);
649 ASSERT_EQ('a', buf[0]);
650 ASSERT_EQ('0', buf[1]);
651 ASSERT_EQ('1', buf[2]);
652 ASSERT_EQ('2', buf[3]);
653 ASSERT_EQ('3', buf[4]);
654 ASSERT_EQ('4', buf[5]);
655 ASSERT_EQ('5', buf[6]);
656 ASSERT_EQ('6', buf[7]);
657 ASSERT_EQ('7', buf[8]);
658 ASSERT_EQ('\0', buf[9]);
659 }
660
TEST(TEST_NAME,strcat_chk_max_int_size)661 TEST(TEST_NAME, strcat_chk_max_int_size) {
662 char buf[10];
663 memset(buf, 'A', sizeof(buf));
664 buf[0] = 'a';
665 buf[1] = '\0';
666 char* res = __strcat_chk(buf, "01234567", (size_t)-1);
667 ASSERT_EQ(buf, res);
668 ASSERT_EQ('a', buf[0]);
669 ASSERT_EQ('0', buf[1]);
670 ASSERT_EQ('1', buf[2]);
671 ASSERT_EQ('2', buf[3]);
672 ASSERT_EQ('3', buf[4]);
673 ASSERT_EQ('4', buf[5]);
674 ASSERT_EQ('5', buf[6]);
675 ASSERT_EQ('6', buf[7]);
676 ASSERT_EQ('7', buf[8]);
677 ASSERT_EQ('\0', buf[9]);
678 }
679
680 extern "C" char* __strcpy_chk(char*, const char*, size_t);
681
TEST(TEST_NAME,strcpy_chk_max_int_size)682 TEST(TEST_NAME, strcpy_chk_max_int_size) {
683 char buf[10];
684 char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
685 ASSERT_EQ(buf, res);
686 ASSERT_EQ('0', buf[0]);
687 ASSERT_EQ('1', buf[1]);
688 ASSERT_EQ('2', buf[2]);
689 ASSERT_EQ('3', buf[3]);
690 ASSERT_EQ('4', buf[4]);
691 ASSERT_EQ('5', buf[5]);
692 ASSERT_EQ('6', buf[6]);
693 ASSERT_EQ('7', buf[7]);
694 ASSERT_EQ('8', buf[8]);
695 ASSERT_EQ('\0', buf[9]);
696 }
697 extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t);
698
TEST(TEST_NAME,memcpy_chk_max_int_size)699 TEST(TEST_NAME, memcpy_chk_max_int_size) {
700 char buf[10];
701 void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
702 ASSERT_EQ((void*)buf, res);
703 ASSERT_EQ('0', buf[0]);
704 ASSERT_EQ('1', buf[1]);
705 ASSERT_EQ('2', buf[2]);
706 ASSERT_EQ('3', buf[3]);
707 ASSERT_EQ('4', buf[4]);
708 ASSERT_EQ('5', buf[5]);
709 ASSERT_EQ('6', buf[6]);
710 ASSERT_EQ('7', buf[7]);
711 ASSERT_EQ('8', buf[8]);
712 ASSERT_EQ('\0', buf[9]);
713 }
714