From fb3873d4db3012ac2d1ba87d688138798787c6e0 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 10 Aug 2016 11:07:54 -0700 Subject: Fortify vsnprintf in more cases. Bug: http://b/30445072 Change-Id: I1893890f0e3b56533eef053eda1bd96a0b9a5119 --- tests/stdio_test.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests/stdio_test.cpp') diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp index 8747dfc6d..ecba4ad3b 100644 --- a/tests/stdio_test.cpp +++ b/tests/stdio_test.cpp @@ -29,14 +29,20 @@ #include +#include "BionicDeathTest.h" #include "TemporaryFile.h" #if defined(NOFORTIFY) #define STDIO_TEST stdio_nofortify +#define STDIO_DEATHTEST stdio_nofortify_DeathTest #else #define STDIO_TEST stdio +#define STDIO_DEATHTEST stdio_DeathTest #endif +class stdio_DeathTest : public BionicDeathTest {}; +class stdio_nofortify_DeathTest : public BionicDeathTest {}; + static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) { rewind(fp); @@ -1329,3 +1335,28 @@ TEST(STDIO_TEST, remove) { ASSERT_EQ(-1, remove(td.dirname)); ASSERT_EQ(ENOENT, errno); } + +TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) { + char buf[16]; + ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"), + testing::KilledBySignal(SIGABRT), +#if defined(NOFORTIFY) + "FORTIFY: vsnprintf: size .* > SSIZE_MAX" +#else + "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer" +#endif + ); +} + +TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) { + std::string buf = "world"; + ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"), + testing::KilledBySignal(SIGABRT), + "FORTIFY: vsnprintf: size .* > SSIZE_MAX"); +} + +TEST(STDIO_TEST, sprintf_30445072) { + std::string buf = "world"; + sprintf(&buf[0], "hello"); + ASSERT_EQ(buf, "hello"); +} -- cgit v1.2.3