From a44e9afdd16105d6f36319cb538666d9cc78435a Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Thu, 17 Jan 2013 15:41:33 -0800 Subject: FORTIFY_SOURCE: optimize Don't do the fortify_source checks if we can determine, at compile time, that the provided operation is safe. This avoids silliness like calling fortify source on things like: size_t len = strlen("asdf"); printf("%d\n", len); and allows the compiler to optimize this code to: printf("%d\n", 4); Defer to gcc's builtin functions instead of pointing our code to the libc implementation. Change-Id: I5e1dcb61946461c4afaaaa983e39f07c7a0df0ae --- tests/string_test.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tests/string_test.cpp') diff --git a/tests/string_test.cpp b/tests/string_test.cpp index 08c73a1aa..1720058e6 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -316,6 +316,27 @@ TEST(string_DeathTest, strcpy_fortified) { ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), ""); free(orig); } + +TEST(string_DeathTest, strlen_fortified) { + ::testing::FLAGS_gtest_death_test_style = "threadsafe"; + char buf[10]; + memcpy(buf, "0123456789", sizeof(buf)); + ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGSEGV), ""); +} + +TEST(string_DeathTest, strchr_fortified) { + ::testing::FLAGS_gtest_death_test_style = "threadsafe"; + char buf[10]; + memcpy(buf, "0123456789", sizeof(buf)); + ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), ""); +} + +TEST(string_DeathTest, strrchr_fortified) { + ::testing::FLAGS_gtest_death_test_style = "threadsafe"; + char buf[10]; + memcpy(buf, "0123456789", sizeof(buf)); + ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), ""); +} #endif #if __BIONIC__ -- cgit v1.2.3