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