blob: 554a11247cbfd1e2cde3e1894827da5d8bf480e9 [file] [log] [blame]
Nick Kralevich5bcf3982013-06-28 10:34:09 -07001/*
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>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080018#include <signal.h>
Nick Kralevich5bcf3982013-06-28 10:34:09 -070019#include <string.h>
20#include <stdarg.h>
Nick Kralevicha6cde392013-06-29 08:15:25 -070021#include <sys/types.h>
22#include <sys/stat.h>
Nick Kralevich60f4f9a2013-09-24 16:32:07 -070023#include <sys/socket.h>
Nick Kralevichb91791d2013-10-02 14:14:40 -070024#include <malloc.h>
Nick Kralevichb036b5c2013-10-09 20:16:34 -070025#include <fcntl.h>
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070026#include <sys/prctl.h>
Nick Kralevich5bcf3982013-06-28 10:34:09 -070027
28// We have to say "DeathTest" here so gtest knows to run this test (which exits)
29// in its own process. Unfortunately, the C preprocessor doesn't give us an
30// easy way to concatenate strings, so we need to use the complicated method
31// below. *sigh*
32#define DEATHTEST_PASTER(name) name##_DeathTest
33#define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name)
34#define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME)
35
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070036class DEATHTEST : public testing::Test {
37 protected:
38 virtual void SetUp() {
39 old_dumpable_ = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
40 // Suppress debuggerd stack traces. Too slow.
41 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
42 }
43
44 virtual void TearDown() {
45 prctl(PR_SET_DUMPABLE, old_dumpable_, 0, 0, 0, 0);
46 }
47 private:
48 int old_dumpable_;
49};
50
Nick Kralevich5bcf3982013-06-28 10:34:09 -070051#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2
52struct foo {
53 char empty[0];
54 char one[1];
55 char a[10];
56 char b[10];
57};
58
59#ifndef __clang__
60// This test is disabled in clang because clang doesn't properly detect
61// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070062TEST_F(DEATHTEST, stpncpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070063 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
64 foo myfoo;
65 int copy_amt = atoi("11");
66 ASSERT_EXIT(stpncpy(myfoo.a, "01234567890", copy_amt),
67 testing::KilledBySignal(SIGABRT), "");
68}
69#endif
70
71#ifndef __clang__
72// This test is disabled in clang because clang doesn't properly detect
73// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070074TEST_F(DEATHTEST, stpncpy2_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070075 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
76 foo myfoo;
77 memset(&myfoo, 0, sizeof(myfoo));
78 myfoo.one[0] = 'A'; // not null terminated string
79 ASSERT_EXIT(stpncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)),
80 testing::KilledBySignal(SIGABRT), "");
81}
82#endif
83
84#ifndef __clang__
85// This test is disabled in clang because clang doesn't properly detect
86// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070087TEST_F(DEATHTEST, strncpy_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070088 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
89 foo myfoo;
90 int copy_amt = atoi("11");
91 ASSERT_EXIT(strncpy(myfoo.a, "01234567890", copy_amt),
92 testing::KilledBySignal(SIGABRT), "");
93}
94#endif
95
96#ifndef __clang__
97// This test is disabled in clang because clang doesn't properly detect
98// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070099TEST_F(DEATHTEST, strncpy2_fortified2) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700100 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
101 foo myfoo;
102 memset(&myfoo, 0, sizeof(myfoo));
103 myfoo.one[0] = 'A'; // not null terminated string
104 ASSERT_EXIT(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)),
105 testing::KilledBySignal(SIGABRT), "");
106}
107#endif
108
109#ifndef __clang__
110// This test is disabled in clang because clang doesn't properly detect
111// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700112TEST_F(DEATHTEST, sprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700113 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
114 foo myfoo;
115 char source_buf[15];
116 memcpy(source_buf, "12345678901234", 15);
117 ASSERT_EXIT(sprintf(myfoo.a, "%s", source_buf),
118 testing::KilledBySignal(SIGABRT), "");
119}
120#endif
121
122#ifndef __clang__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700123// These tests are disabled in clang because clang doesn't properly detect
124// this buffer overflow. TODO: Fix clang.
125static int vsprintf_helper2(const char *fmt, ...) {
126 foo myfoo;
127 va_list va;
128 int result;
129
130 va_start(va, fmt);
131 result = vsprintf(myfoo.a, fmt, va); // should crash here
132 va_end(va);
133 return result;
134}
135
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700136TEST_F(DEATHTEST, vsprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700137 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
138 ASSERT_EXIT(vsprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
139}
140
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700141TEST_F(DEATHTEST, vsprintf2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700142 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
143 ASSERT_EXIT(vsprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), "");
144}
145#endif
146
147#ifndef __clang__
148// These tests are disabled in clang because clang doesn't properly detect
149// this buffer overflow. TODO: Fix clang.
150static int vsnprintf_helper2(const char *fmt, ...) {
151 foo myfoo;
152 va_list va;
153 int result;
154 size_t size = atoi("11");
155
156 va_start(va, fmt);
157 result = vsnprintf(myfoo.a, size, fmt, va); // should crash here
158 va_end(va);
159 return result;
160}
161
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700162TEST_F(DEATHTEST, vsnprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700163 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
164 ASSERT_EXIT(vsnprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
165}
166
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700167TEST_F(DEATHTEST, vsnprintf2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700168 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
169 ASSERT_EXIT(vsnprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), "");
170}
171#endif
172
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700173#ifndef __clang__
174// zero sized target with "\0" source (should fail)
175// This test is disabled in clang because clang doesn't properly detect
176// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700177TEST_F(DEATHTEST, stpcpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700178#if defined(__BIONIC__)
179 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
180 foo myfoo;
181 char* src = strdup("");
182 ASSERT_EXIT(stpcpy(myfoo.empty, src),
183 testing::KilledBySignal(SIGABRT), "");
184 free(src);
185#else // __BIONIC__
186 GTEST_LOG_(INFO) << "This test does nothing.\n";
187#endif // __BIONIC__
188}
189#endif
190
191#ifndef __clang__
192// zero sized target with "\0" source (should fail)
193// This test is disabled in clang because clang doesn't properly detect
194// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700195TEST_F(DEATHTEST, strcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800196#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700197 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
198 foo myfoo;
199 char* src = strdup("");
200 ASSERT_EXIT(strcpy(myfoo.empty, src),
201 testing::KilledBySignal(SIGABRT), "");
202 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800203#else // __BIONIC__
204 GTEST_LOG_(INFO) << "This test does nothing.\n";
205#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700206}
207#endif
208
209#ifndef __clang__
210// zero sized target with longer source (should fail)
211// This test is disabled in clang because clang doesn't properly detect
212// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700213TEST_F(DEATHTEST, strcpy2_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800214#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700215 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
216 foo myfoo;
217 char* src = strdup("1");
218 ASSERT_EXIT(strcpy(myfoo.empty, src),
219 testing::KilledBySignal(SIGABRT), "");
220 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800221#else // __BIONIC__
222 GTEST_LOG_(INFO) << "This test does nothing.\n";
223#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700224}
225#endif
226
227#ifndef __clang__
228// one byte target with longer source (should fail)
229// This test is disabled in clang because clang doesn't properly detect
230// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700231TEST_F(DEATHTEST, strcpy3_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800232#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700233 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
234 foo myfoo;
235 char* src = strdup("12");
236 ASSERT_EXIT(strcpy(myfoo.one, src),
237 testing::KilledBySignal(SIGABRT), "");
238 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800239#else // __BIONIC__
240 GTEST_LOG_(INFO) << "This test does nothing.\n";
241#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700242}
243#endif
244
245#ifndef __clang__
246// This test is disabled in clang because clang doesn't properly detect
247// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700248TEST_F(DEATHTEST, strchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800249#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700250 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
251 foo myfoo;
252 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
253 myfoo.b[0] = '\0';
254 ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')),
255 testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800256#else // __BIONIC__
257 GTEST_LOG_(INFO) << "This test does nothing.\n";
258#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700259}
260#endif
261
262#ifndef __clang__
263// This test is disabled in clang because clang doesn't properly detect
264// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700265TEST_F(DEATHTEST, strrchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800266#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700267 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
268 foo myfoo;
269 memcpy(myfoo.a, "0123456789", 10);
270 memcpy(myfoo.b, "01234", 6);
271 ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')),
272 testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800273#else // __BIONIC__
274 GTEST_LOG_(INFO) << "This test does nothing.\n";
275#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700276}
277#endif
278
279#ifndef __clang__
280// This test is disabled in clang because clang doesn't properly detect
281// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700282TEST_F(DEATHTEST, strlcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800283#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700284 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
285 foo myfoo;
286 strcpy(myfoo.a, "01");
287 size_t n = strlen(myfoo.a);
288 ASSERT_EXIT(strlcpy(myfoo.one, myfoo.a, n),
289 testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800290#else // __BIONIC__
291 GTEST_LOG_(INFO) << "This test does nothing.\n";
292#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700293}
294#endif
295
Nick Kralevicha6cde392013-06-29 08:15:25 -0700296#ifndef __clang__
297// This test is disabled in clang because clang doesn't properly detect
298// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700299TEST_F(DEATHTEST, strlcat_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800300#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700301 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
302 foo myfoo;
303 strcpy(myfoo.a, "01");
304 myfoo.one[0] = '\0';
305 size_t n = strlen(myfoo.a);
306 ASSERT_EXIT(strlcat(myfoo.one, myfoo.a, n),
307 testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800308#else // __BIONIC__
309 GTEST_LOG_(INFO) << "This test does nothing.\n";
310#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700311}
312#endif
313
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700314#ifndef __clang__
315// This test is disabled in clang because clang doesn't properly detect
316// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700317TEST_F(DEATHTEST, strncat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700318 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
319 foo myfoo;
320 size_t n = atoi("10"); // avoid compiler optimizations
321 strncpy(myfoo.a, "012345678", n);
322 ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGABRT), "");
323}
324#endif
325
326#ifndef __clang__
327// This test is disabled in clang because clang doesn't properly detect
328// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700329TEST_F(DEATHTEST, strncat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700330 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
331 foo myfoo;
332 myfoo.a[0] = '\0';
333 size_t n = atoi("10"); // avoid compiler optimizations
334 ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
335}
336#endif
337
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700338TEST_F(DEATHTEST, strncat3_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700339 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
340 foo myfoo;
341 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
342 myfoo.b[0] = '\0';
343 size_t n = atoi("10"); // avoid compiler optimizations
344 ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), "");
345}
346
347#ifndef __clang__
348// This test is disabled in clang because clang doesn't properly detect
349// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700350TEST_F(DEATHTEST, strcat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700351 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
352 char src[11];
353 strcpy(src, "0123456789");
354 foo myfoo;
355 myfoo.a[0] = '\0';
356 ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGABRT), "");
357}
358#endif
359
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700360TEST_F(DEATHTEST, strcat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700361 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
362 foo myfoo;
363 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
364 myfoo.b[0] = '\0';
365 ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), "");
366}
367
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700368TEST_F(DEATHTEST, snprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700369 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
370 foo myfoo;
371 strcpy(myfoo.a, "012345678");
372 size_t n = strlen(myfoo.a) + 2;
373 ASSERT_EXIT(snprintf(myfoo.b, n, "a%s", myfoo.a), testing::KilledBySignal(SIGABRT), "");
374}
375
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700376TEST_F(DEATHTEST, bzero_fortified2) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700377 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
378 foo myfoo;
379 memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
380 size_t n = atoi("11");
381 ASSERT_EXIT(bzero(myfoo.b, n), testing::KilledBySignal(SIGABRT), "");
382}
383
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700384#endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */
385
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700386// multibyte target where we over fill (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700387TEST_F(DEATHTEST, strcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800388#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700389 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
390 char buf[10];
391 char *orig = strdup("0123456789");
392 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
393 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800394#else // __BIONIC__
395 GTEST_LOG_(INFO) << "This test does nothing.\n";
396#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700397}
398
399// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700400TEST_F(DEATHTEST, strcpy2_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800401#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700402 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
403 char buf[0];
404 char *orig = strdup("");
405 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
406 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800407#else // __BIONIC__
408 GTEST_LOG_(INFO) << "This test does nothing.\n";
409#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700410}
411
412// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700413TEST_F(DEATHTEST, strcpy3_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800414#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700415 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
416 char buf[0];
417 char *orig = strdup("1");
418 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
419 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800420#else // __BIONIC__
421 GTEST_LOG_(INFO) << "This test does nothing.\n";
422#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700423}
424
425// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700426TEST_F(DEATHTEST, strcpy4_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800427#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700428 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
429 char buf[1];
430 char *orig = strdup("12");
431 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
432 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800433#else // __BIONIC__
434 GTEST_LOG_(INFO) << "This test does nothing.\n";
435#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700436}
437
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700438TEST_F(DEATHTEST, strlen_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800439#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700440 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
441 char buf[10];
442 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700443 ASSERT_EXIT(printf("%zd", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800444#else // __BIONIC__
445 GTEST_LOG_(INFO) << "This test does nothing.\n";
446#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700447}
448
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700449TEST_F(DEATHTEST, strchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800450#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700451 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
452 char buf[10];
453 memcpy(buf, "0123456789", sizeof(buf));
454 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800455#else // __BIONIC__
456 GTEST_LOG_(INFO) << "This test does nothing.\n";
457#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700458}
459
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700460TEST_F(DEATHTEST, strrchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800461#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700462 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
463 char buf[10];
464 memcpy(buf, "0123456789", sizeof(buf));
465 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800466#else // __BIONIC__
467 GTEST_LOG_(INFO) << "This test does nothing.\n";
468#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700469}
470
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700471TEST_F(DEATHTEST, strlcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800472#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700473 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
474 char bufa[15];
475 char bufb[10];
476 strcpy(bufa, "01234567890123");
477 size_t n = strlen(bufa);
478 ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800479#else // __BIONIC__
480 GTEST_LOG_(INFO) << "This test does nothing.\n";
481#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700482}
483
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700484TEST_F(DEATHTEST, strlcat_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800485#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700486 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
487 char bufa[15];
488 char bufb[10];
489 bufb[0] = '\0';
490 strcpy(bufa, "01234567890123");
491 size_t n = strlen(bufa);
492 ASSERT_EXIT(strlcat(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800493#else // __BIONIC__
494 GTEST_LOG_(INFO) << "This test does nothing.\n";
495#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700496}
497
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700498TEST_F(DEATHTEST, sprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700499 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
500 char buf[10];
501 char source_buf[15];
502 memcpy(source_buf, "12345678901234", 15);
503 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
504}
505
Nick Kralevichb91791d2013-10-02 14:14:40 -0700506#ifndef __clang__
507// This test is disabled in clang because clang doesn't properly detect
508// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700509TEST_F(DEATHTEST, sprintf_malloc_fortified) {
Nick Kralevichb91791d2013-10-02 14:14:40 -0700510 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
511 char* buf = (char *) malloc(10);
512 char source_buf[11];
513 memcpy(source_buf, "1234567890", 11);
514 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
515 free(buf);
516}
517#endif
518
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700519static int vsprintf_helper(const char *fmt, ...) {
520 char buf[10];
521 va_list va;
522 int result;
523
524 va_start(va, fmt);
525 result = vsprintf(buf, fmt, va); // should crash here
526 va_end(va);
527 return result;
528}
529
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700530TEST_F(DEATHTEST, vsprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700531 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
532 ASSERT_EXIT(vsprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
533}
534
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700535TEST_F(DEATHTEST, vsprintf2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700536 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
537 ASSERT_EXIT(vsprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
538}
539
540static int vsnprintf_helper(const char *fmt, ...) {
541 char buf[10];
542 va_list va;
543 int result;
544 size_t size = atoi("11");
545
546 va_start(va, fmt);
547 result = vsnprintf(buf, size, fmt, va); // should crash here
548 va_end(va);
549 return result;
550}
551
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700552TEST_F(DEATHTEST, vsnprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700553 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
554 ASSERT_EXIT(vsnprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
555}
556
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700557TEST_F(DEATHTEST, vsnprintf2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700558 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
559 ASSERT_EXIT(vsnprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
560}
561
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700562TEST_F(DEATHTEST, strncat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700563 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
564 char buf[10];
565 size_t n = atoi("10"); // avoid compiler optimizations
566 strncpy(buf, "012345678", n);
567 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
568}
569
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700570TEST_F(DEATHTEST, strncat2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700571 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
572 char buf[10];
573 buf[0] = '\0';
574 size_t n = atoi("10"); // avoid compiler optimizations
575 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
576}
577
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700578TEST_F(DEATHTEST, strcat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700579 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
580 char src[11];
581 strcpy(src, "0123456789");
582 char buf[10];
583 buf[0] = '\0';
584 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
585}
586
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700587TEST_F(DEATHTEST, memmove_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700588 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
589 char buf[20];
590 strcpy(buf, "0123456789");
591 size_t n = atoi("10");
592 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
593}
594
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700595TEST_F(DEATHTEST, memcpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700596 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
597 char bufa[10];
598 char bufb[10];
599 strcpy(bufa, "012345678");
600 size_t n = atoi("11");
601 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
602}
603
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700604TEST_F(DEATHTEST, stpncpy_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700605 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
606 char bufa[15];
607 char bufb[10];
608 strcpy(bufa, "01234567890123");
609 size_t n = strlen(bufa);
610 ASSERT_EXIT(stpncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
611}
612
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700613TEST_F(DEATHTEST, stpncpy2_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700614 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
615 char dest[11];
616 char src[10];
617 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
618 ASSERT_EXIT(stpncpy(dest, src, sizeof(dest)), testing::KilledBySignal(SIGABRT), "");
619}
620
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700621TEST_F(DEATHTEST, strncpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700622 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
623 char bufa[15];
624 char bufb[10];
625 strcpy(bufa, "01234567890123");
626 size_t n = strlen(bufa);
627 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
628}
629
Christopher Ferris950a58e2014-04-04 14:38:18 -0700630
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700631TEST_F(DEATHTEST, strncpy2_fortified) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700632 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
633 char dest[11];
634 char src[10];
635 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
636 ASSERT_EXIT(strncpy(dest, src, sizeof(dest)), testing::KilledBySignal(SIGABRT), "");
637}
638
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700639TEST_F(DEATHTEST, snprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700640 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
641 char bufa[15];
642 char bufb[10];
643 strcpy(bufa, "0123456789");
644 size_t n = strlen(bufa) + 1;
645 ASSERT_EXIT(snprintf(bufb, n, "%s", bufa), testing::KilledBySignal(SIGABRT), "");
646}
647
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700648TEST_F(DEATHTEST, bzero_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700649 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
650 char buf[10];
651 memcpy(buf, "0123456789", sizeof(buf));
652 size_t n = atoi("11");
653 ASSERT_EXIT(bzero(buf, n), testing::KilledBySignal(SIGABRT), "");
654}
655
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700656TEST_F(DEATHTEST, umask_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700657 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
658 mode_t mask = atoi("1023"); // 01777 in octal
659 ASSERT_EXIT(umask(mask), testing::KilledBySignal(SIGABRT), "");
660}
661
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700662TEST_F(DEATHTEST, recv_fortified) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700663 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
664 size_t data_len = atoi("11"); // suppress compiler optimizations
665 char buf[10];
666 ASSERT_EXIT(recv(0, buf, data_len, 0), testing::KilledBySignal(SIGABRT), "");
667}
668
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700669TEST_F(DEATHTEST, FD_ISSET_fortified) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700670#if defined(__BIONIC__) // glibc catches this at compile-time.
Nick Kralevich90201d52013-10-02 16:11:30 -0700671 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
672 fd_set set;
673 memset(&set, 0, sizeof(set));
674 ASSERT_EXIT(FD_ISSET(-1, &set), testing::KilledBySignal(SIGABRT), "");
Elliott Hughes409588c2014-04-23 23:02:43 -0700675#endif
Nick Kralevich90201d52013-10-02 16:11:30 -0700676}
677
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700678TEST_F(DEATHTEST, FD_ISSET_2_fortified) {
Nick Kralevich7943df62013-10-03 14:08:39 -0700679 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
680 char buf[1];
681 fd_set* set = (fd_set*) buf;
682 ASSERT_EXIT(FD_ISSET(0, set), testing::KilledBySignal(SIGABRT), "");
683}
684
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700685TEST_F(DEATHTEST, read_fortified) {
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700686 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
687 char buf[1];
688 size_t ct = atoi("2"); // prevent optimizations
689 int fd = open("/dev/null", O_RDONLY);
690 ASSERT_EXIT(read(fd, buf, ct), testing::KilledBySignal(SIGABRT), "");
691 close(fd);
692}
693
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700694extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
695extern "C" char* __strcat_chk(char*, const char*, size_t);
696
697TEST(TEST_NAME, strncat) {
698 char buf[10];
699 memset(buf, 'A', sizeof(buf));
700 buf[0] = 'a';
701 buf[1] = '\0';
702 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
703 ASSERT_EQ(buf, res);
704 ASSERT_EQ('a', buf[0]);
705 ASSERT_EQ('0', buf[1]);
706 ASSERT_EQ('1', buf[2]);
707 ASSERT_EQ('2', buf[3]);
708 ASSERT_EQ('3', buf[4]);
709 ASSERT_EQ('4', buf[5]);
710 ASSERT_EQ('\0', buf[6]);
711 ASSERT_EQ('A', buf[7]);
712 ASSERT_EQ('A', buf[8]);
713 ASSERT_EQ('A', buf[9]);
714}
715
716TEST(TEST_NAME, strncat2) {
717 char buf[10];
718 memset(buf, 'A', sizeof(buf));
719 buf[0] = 'a';
720 buf[1] = '\0';
721 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
722 ASSERT_EQ(buf, res);
723 ASSERT_EQ('a', buf[0]);
724 ASSERT_EQ('0', buf[1]);
725 ASSERT_EQ('1', buf[2]);
726 ASSERT_EQ('2', buf[3]);
727 ASSERT_EQ('3', buf[4]);
728 ASSERT_EQ('4', buf[5]);
729 ASSERT_EQ('\0', buf[6]);
730 ASSERT_EQ('A', buf[7]);
731 ASSERT_EQ('A', buf[8]);
732 ASSERT_EQ('A', buf[9]);
733}
734
735TEST(TEST_NAME, strncat3) {
736 char buf[10];
737 memset(buf, 'A', sizeof(buf));
738 buf[0] = '\0';
739 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
740 ASSERT_EQ(buf, res);
741 ASSERT_EQ('0', buf[0]);
742 ASSERT_EQ('1', buf[1]);
743 ASSERT_EQ('2', buf[2]);
744 ASSERT_EQ('3', buf[3]);
745 ASSERT_EQ('4', buf[4]);
746 ASSERT_EQ('\0', buf[5]);
747 ASSERT_EQ('A', buf[6]);
748 ASSERT_EQ('A', buf[7]);
749 ASSERT_EQ('A', buf[8]);
750 ASSERT_EQ('A', buf[9]);
751}
752
753TEST(TEST_NAME, strncat4) {
754 char buf[10];
755 memset(buf, 'A', sizeof(buf));
756 buf[9] = '\0';
757 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
758 ASSERT_EQ(buf, res);
759 ASSERT_EQ('A', buf[0]);
760 ASSERT_EQ('A', buf[1]);
761 ASSERT_EQ('A', buf[2]);
762 ASSERT_EQ('A', buf[3]);
763 ASSERT_EQ('A', buf[4]);
764 ASSERT_EQ('A', buf[5]);
765 ASSERT_EQ('A', buf[6]);
766 ASSERT_EQ('A', buf[7]);
767 ASSERT_EQ('A', buf[8]);
768 ASSERT_EQ('\0', buf[9]);
769}
770
771TEST(TEST_NAME, strncat5) {
772 char buf[10];
773 memset(buf, 'A', sizeof(buf));
774 buf[0] = 'a';
775 buf[1] = '\0';
776 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
777 ASSERT_EQ(buf, res);
778 ASSERT_EQ('a', buf[0]);
779 ASSERT_EQ('0', buf[1]);
780 ASSERT_EQ('1', buf[2]);
781 ASSERT_EQ('2', buf[3]);
782 ASSERT_EQ('3', buf[4]);
783 ASSERT_EQ('4', buf[5]);
784 ASSERT_EQ('5', buf[6]);
785 ASSERT_EQ('6', buf[7]);
786 ASSERT_EQ('7', buf[8]);
787 ASSERT_EQ('\0', buf[9]);
788}
789
790TEST(TEST_NAME, strncat6) {
791 char buf[10];
792 memset(buf, 'A', sizeof(buf));
793 buf[0] = 'a';
794 buf[1] = '\0';
795 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
796 ASSERT_EQ(buf, res);
797 ASSERT_EQ('a', buf[0]);
798 ASSERT_EQ('0', buf[1]);
799 ASSERT_EQ('1', buf[2]);
800 ASSERT_EQ('2', buf[3]);
801 ASSERT_EQ('3', buf[4]);
802 ASSERT_EQ('4', buf[5]);
803 ASSERT_EQ('5', buf[6]);
804 ASSERT_EQ('6', buf[7]);
805 ASSERT_EQ('7', buf[8]);
806 ASSERT_EQ('\0', buf[9]);
807}
808
809
810TEST(TEST_NAME, strcat) {
811 char buf[10];
812 memset(buf, 'A', sizeof(buf));
813 buf[0] = 'a';
814 buf[1] = '\0';
815 char* res = __strcat_chk(buf, "01234", sizeof(buf));
816 ASSERT_EQ(buf, res);
817 ASSERT_EQ('a', buf[0]);
818 ASSERT_EQ('0', buf[1]);
819 ASSERT_EQ('1', buf[2]);
820 ASSERT_EQ('2', buf[3]);
821 ASSERT_EQ('3', buf[4]);
822 ASSERT_EQ('4', buf[5]);
823 ASSERT_EQ('\0', buf[6]);
824 ASSERT_EQ('A', buf[7]);
825 ASSERT_EQ('A', buf[8]);
826 ASSERT_EQ('A', buf[9]);
827}
828
829TEST(TEST_NAME, strcat2) {
830 char buf[10];
831 memset(buf, 'A', sizeof(buf));
832 buf[0] = 'a';
833 buf[1] = '\0';
834 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
835 ASSERT_EQ(buf, res);
836 ASSERT_EQ('a', buf[0]);
837 ASSERT_EQ('0', buf[1]);
838 ASSERT_EQ('1', buf[2]);
839 ASSERT_EQ('2', buf[3]);
840 ASSERT_EQ('3', buf[4]);
841 ASSERT_EQ('4', buf[5]);
842 ASSERT_EQ('5', buf[6]);
843 ASSERT_EQ('6', buf[7]);
844 ASSERT_EQ('7', buf[8]);
845 ASSERT_EQ('\0', buf[9]);
846}
Nick Kralevich93501d32013-08-28 10:47:43 -0700847
Christopher Ferris950a58e2014-04-04 14:38:18 -0700848TEST(TEST_NAME, stpncpy) {
849 char src[10];
850 char dst[10];
851 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
852 stpncpy(dst, src, sizeof(dst));
853 ASSERT_EQ('0', dst[0]);
854 ASSERT_EQ('1', dst[1]);
855 ASSERT_EQ('2', dst[2]);
856 ASSERT_EQ('3', dst[3]);
857 ASSERT_EQ('4', dst[4]);
858 ASSERT_EQ('5', dst[5]);
859 ASSERT_EQ('6', dst[6]);
860 ASSERT_EQ('7', dst[7]);
861 ASSERT_EQ('8', dst[8]);
862 ASSERT_EQ('9', dst[9]);
863}
864
865TEST(TEST_NAME, stpncpy2) {
866 char src[10];
867 char dst[15];
868 memcpy(src, "012345678\0", sizeof(src));
869 stpncpy(dst, src, sizeof(dst));
870 ASSERT_EQ('0', dst[0]);
871 ASSERT_EQ('1', dst[1]);
872 ASSERT_EQ('2', dst[2]);
873 ASSERT_EQ('3', dst[3]);
874 ASSERT_EQ('4', dst[4]);
875 ASSERT_EQ('5', dst[5]);
876 ASSERT_EQ('6', dst[6]);
877 ASSERT_EQ('7', dst[7]);
878 ASSERT_EQ('8', dst[8]);
879 ASSERT_EQ('\0', dst[9]);
880 ASSERT_EQ('\0', dst[10]);
881 ASSERT_EQ('\0', dst[11]);
882 ASSERT_EQ('\0', dst[12]);
883 ASSERT_EQ('\0', dst[13]);
884 ASSERT_EQ('\0', dst[14]);
885}
886
Nick Kralevich93501d32013-08-28 10:47:43 -0700887TEST(TEST_NAME, strncpy) {
888 char src[10];
889 char dst[10];
890 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
891 strncpy(dst, src, sizeof(dst));
892 ASSERT_EQ('0', dst[0]);
893 ASSERT_EQ('1', dst[1]);
894 ASSERT_EQ('2', dst[2]);
895 ASSERT_EQ('3', dst[3]);
896 ASSERT_EQ('4', dst[4]);
897 ASSERT_EQ('5', dst[5]);
898 ASSERT_EQ('6', dst[6]);
899 ASSERT_EQ('7', dst[7]);
900 ASSERT_EQ('8', dst[8]);
901 ASSERT_EQ('9', dst[9]);
902}
903
904TEST(TEST_NAME, strncpy2) {
905 char src[10];
906 char dst[15];
907 memcpy(src, "012345678\0", sizeof(src));
908 strncpy(dst, src, sizeof(dst));
909 ASSERT_EQ('0', dst[0]);
910 ASSERT_EQ('1', dst[1]);
911 ASSERT_EQ('2', dst[2]);
912 ASSERT_EQ('3', dst[3]);
913 ASSERT_EQ('4', dst[4]);
914 ASSERT_EQ('5', dst[5]);
915 ASSERT_EQ('6', dst[6]);
916 ASSERT_EQ('7', dst[7]);
917 ASSERT_EQ('8', dst[8]);
918 ASSERT_EQ('\0', dst[9]);
919 ASSERT_EQ('\0', dst[10]);
920 ASSERT_EQ('\0', dst[11]);
921 ASSERT_EQ('\0', dst[12]);
922 ASSERT_EQ('\0', dst[13]);
923 ASSERT_EQ('\0', dst[14]);
924}
Christopher Ferris16e185c2013-09-10 16:56:34 -0700925
926TEST(TEST_NAME, strcat_chk_max_int_size) {
927 char buf[10];
928 memset(buf, 'A', sizeof(buf));
929 buf[0] = 'a';
930 buf[1] = '\0';
931 char* res = __strcat_chk(buf, "01234567", (size_t)-1);
932 ASSERT_EQ(buf, res);
933 ASSERT_EQ('a', buf[0]);
934 ASSERT_EQ('0', buf[1]);
935 ASSERT_EQ('1', buf[2]);
936 ASSERT_EQ('2', buf[3]);
937 ASSERT_EQ('3', buf[4]);
938 ASSERT_EQ('4', buf[5]);
939 ASSERT_EQ('5', buf[6]);
940 ASSERT_EQ('6', buf[7]);
941 ASSERT_EQ('7', buf[8]);
942 ASSERT_EQ('\0', buf[9]);
943}
944
Christopher Ferris950a58e2014-04-04 14:38:18 -0700945extern "C" char* __stpcpy_chk(char*, const char*, size_t);
946
947TEST(TEST_NAME, stpcpy_chk_max_int_size) {
948 char buf[10];
949 char* res = __stpcpy_chk(buf, "012345678", (size_t)-1);
950 ASSERT_EQ(buf + strlen("012345678"), res);
951 ASSERT_STREQ("012345678", buf);
952}
953
Christopher Ferris16e185c2013-09-10 16:56:34 -0700954extern "C" char* __strcpy_chk(char*, const char*, size_t);
955
956TEST(TEST_NAME, strcpy_chk_max_int_size) {
957 char buf[10];
958 char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
959 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700960 ASSERT_STREQ("012345678", buf);
Christopher Ferris16e185c2013-09-10 16:56:34 -0700961}
962
963extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t);
964
965TEST(TEST_NAME, memcpy_chk_max_int_size) {
966 char buf[10];
967 void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
968 ASSERT_EQ((void*)buf, res);
969 ASSERT_EQ('0', buf[0]);
970 ASSERT_EQ('1', buf[1]);
971 ASSERT_EQ('2', buf[2]);
972 ASSERT_EQ('3', buf[3]);
973 ASSERT_EQ('4', buf[4]);
974 ASSERT_EQ('5', buf[5]);
975 ASSERT_EQ('6', buf[6]);
976 ASSERT_EQ('7', buf[7]);
977 ASSERT_EQ('8', buf[8]);
978 ASSERT_EQ('\0', buf[9]);
979}
Stephen Hines6e380722013-10-11 00:45:24 -0700980
981// Verify that macro expansion is done properly for sprintf/snprintf (which
982// are defined as macros in stdio.h under clang).
983#define CONTENTS "macro expansion"
984#define BUF_AND_SIZE(A) A, sizeof(A)
985#define BUF_AND_CONTENTS(A) A, CONTENTS
986#define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS
987TEST(TEST_NAME, s_n_printf_macro_expansion) {
988 char buf[BUFSIZ];
989 snprintf(BUF_AND_SIZE(buf), CONTENTS);
990 EXPECT_STREQ(CONTENTS, buf);
991
992 snprintf(BUF_AND_SIZE_AND_CONTENTS(buf));
993 EXPECT_STREQ(CONTENTS, buf);
994
995 sprintf(BUF_AND_CONTENTS(buf));
996 EXPECT_STREQ(CONTENTS, buf);
997}