From 8bafa7452ec0892572b0b49f86022ce945c5e908 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Thu, 20 Jun 2013 12:17:44 -0700 Subject: [PATCH] libc: enable FORTIFY_SOURCE clang strlcpy Change-Id: Idcfe08f5afc3dde592416df9eba83f64e130c7c2 --- libc/include/string.h | 4 ++-- tests/fortify1_test.cpp | 10 ++++++++++ tests/fortify1_test_clang.cpp | 10 ++++++++++ tests/fortify2_test.cpp | 20 ++++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/libc/include/string.h b/libc/include/string.h index ac31f7d1e..2b7d47c40 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -144,7 +144,6 @@ void* memset(void *s, int c, size_t n) { return __builtin___memset_chk(s, c, n, __builtin_object_size (s, 0)); } -#if !defined(__clang__) extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t) __asm__(__USER_LABEL_PREFIX__ "strlcpy"); __errordecl(__strlcpy_error, "strlcpy called with size bigger than buffer"); @@ -154,6 +153,7 @@ __BIONIC_FORTIFY_INLINE size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) { size_t bos = __bos(dest); +#if !defined(__clang__) // Compiler doesn't know destination size. Don't call __strlcpy_chk if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { return __strlcpy_real(dest, src, size); @@ -170,10 +170,10 @@ size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) { if (__builtin_constant_p(size) && (size > bos)) { __strlcpy_error(); } +#endif /* !defined(__clang__) */ return __strlcpy_chk(dest, src, size, bos); } -#endif /* !defined(__clang__) */ #if !defined(__clang__) extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t) diff --git a/tests/fortify1_test.cpp b/tests/fortify1_test.cpp index be59a1824..b8751bbef 100644 --- a/tests/fortify1_test.cpp +++ b/tests/fortify1_test.cpp @@ -80,6 +80,16 @@ TEST(Fortify1_DeathTest, strrchr_fortified) { memcpy(buf, "0123456789", sizeof(buf)); ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), ""); } + +TEST(Fortify1_DeathTest, strlcpy_fortified) { + ::testing::FLAGS_gtest_death_test_style = "threadsafe"; + char bufa[15]; + char bufb[10]; + strcpy(bufa, "01234567890123"); + size_t n = strlen(bufa); + ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); +} + #endif TEST(Fortify1_DeathTest, sprintf_fortified) { diff --git a/tests/fortify1_test_clang.cpp b/tests/fortify1_test_clang.cpp index 0c0fb2bef..fed9f13db 100644 --- a/tests/fortify1_test_clang.cpp +++ b/tests/fortify1_test_clang.cpp @@ -80,6 +80,16 @@ TEST(Fortify1_Clang_DeathTest, strrchr_fortified) { memcpy(buf, "0123456789", sizeof(buf)); ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), ""); } + +TEST(Fortify1_Clang_DeathTest, strlcpy_fortified) { + ::testing::FLAGS_gtest_death_test_style = "threadsafe"; + char bufa[15]; + char bufb[10]; + strcpy(bufa, "01234567890123"); + size_t n = strlen(bufa); + ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); +} + #endif TEST(Fortify1_Clang_DeathTest, strncat_fortified) { diff --git a/tests/fortify2_test.cpp b/tests/fortify2_test.cpp index b48a077bd..b6f666106 100644 --- a/tests/fortify2_test.cpp +++ b/tests/fortify2_test.cpp @@ -94,6 +94,16 @@ TEST(Fortify2_DeathTest, strrchr_fortified2) { ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')), testing::KilledBySignal(SIGABRT), ""); } + +TEST(Fortify2_DeathTest, strlcpy_fortified2) { + ::testing::FLAGS_gtest_death_test_style = "threadsafe"; + foo myfoo; + strcpy(myfoo.a, "01"); + size_t n = strlen(myfoo.a); + ASSERT_EXIT(strlcpy(myfoo.one, myfoo.a, n), + testing::KilledBySignal(SIGABRT), ""); +} + #endif TEST(Fortify2_DeathTest, strncat_fortified2) { @@ -199,6 +209,16 @@ TEST(Fortify2_DeathTest, strrchr_fortified) { memcpy(buf, "0123456789", sizeof(buf)); ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), ""); } + +TEST(Fortify2_DeathTest, strlcpy_fortified) { + ::testing::FLAGS_gtest_death_test_style = "threadsafe"; + char bufa[15]; + char bufb[10]; + strcpy(bufa, "01234567890123"); + size_t n = strlen(bufa); + ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); +} + #endif TEST(Fortify2_DeathTest, sprintf_fortified) { -- 2.11.0