FORTIFY_SOURCE: strcat / strncat optimize

__strcat_chk and __strncat_chk are slightly inefficient,
because they end up traversing over the same memory region
two times.

This change optimizes __strcat_chk / __strncat_chk so they
only access the memory once. Although I haven't benchmarked these
changes, it should improve the performance of these functions.

__strlen_chk - expose this function, even if -D_FORTIFY_SOURCE
isn't defined. This is needed to compile libc itself without
-D_FORTIFY_SOURCE.

Change-Id: Id2c70dff55a276b47c59db27a03734d659f84b74
This commit is contained in:
Nick Kralevich
2013-05-30 16:48:53 -07:00
parent 72f59c84fd
commit cf870199d5
7 changed files with 354 additions and 40 deletions

View File

@@ -80,6 +80,32 @@ TEST(Fortify2_DeathTest, strncat2_fortified2) {
ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGSEGV), "");
}
TEST(Fortify2_DeathTest, strncat3_fortified2) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
foo myfoo;
memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
myfoo.b[0] = '\0';
size_t n = atoi("10"); // avoid compiler optimizations
ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGSEGV), "");
}
TEST(Fortify2_DeathTest, strcat_fortified2) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
char src[11];
strcpy(src, "0123456789");
foo myfoo;
myfoo.a[0] = '\0';
ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGSEGV), "");
}
TEST(Fortify2_DeathTest, strcat2_fortified2) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
foo myfoo;
memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
myfoo.b[0] = '\0';
ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGSEGV), "");
}
/***********************************************************/
/* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test.cpp */
/***********************************************************/
@@ -138,3 +164,12 @@ TEST(Fortify2_DeathTest, strncat2_fortified) {
size_t n = atoi("10"); // avoid compiler optimizations
ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGSEGV), "");
}
TEST(Fortify2_DeathTest, strcat_fortified) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
char src[11];
strcpy(src, "0123456789");
char buf[10];
buf[0] = '\0';
ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGSEGV), "");
}