aboutsummaryrefslogtreecommitdiffstats
path: root/tests/fortify_test.cpp
diff options
context:
space:
mode:
authorStephen Hines <srhines@google.com>2013-10-11 00:45:24 -0700
committerStephen Hines <srhines@google.com>2013-10-11 12:14:49 -0700
commit6e38072addd556e3894284b5bd040ac64fffa72e (patch)
treee13c1bf92ae30ecd9050b1d519cfd24831442ddf /tests/fortify_test.cpp
parenteda2679e30b997d036e0ec572cda054adc2ac3a6 (diff)
downloadandroid_bionic-6e38072addd556e3894284b5bd040ac64fffa72e.tar.gz
android_bionic-6e38072addd556e3894284b5bd040ac64fffa72e.tar.bz2
android_bionic-6e38072addd556e3894284b5bd040ac64fffa72e.zip
Wrap sprintf()/snprintf() macros to prevent expansion errors.
Previously, FORTIFY_SOURCE used single macros to define these standard functions for use with clang. This can cause conflicts with other macros used to call these functions, particularly when those macros expand the number of arguments to the function. This change wraps our macro definitions, so that expansion properly takes place for programmer arguments first. Change-Id: I55929b1fd2a643b9d14a17631c4bcab3b0b712cf
Diffstat (limited to 'tests/fortify_test.cpp')
-rw-r--r--tests/fortify_test.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/tests/fortify_test.cpp b/tests/fortify_test.cpp
index b42c6b781..408991ef7 100644
--- a/tests/fortify_test.cpp
+++ b/tests/fortify_test.cpp
@@ -825,3 +825,21 @@ TEST(TEST_NAME, memcpy_chk_max_int_size) {
ASSERT_EQ('8', buf[8]);
ASSERT_EQ('\0', buf[9]);
}
+
+// Verify that macro expansion is done properly for sprintf/snprintf (which
+// are defined as macros in stdio.h under clang).
+#define CONTENTS "macro expansion"
+#define BUF_AND_SIZE(A) A, sizeof(A)
+#define BUF_AND_CONTENTS(A) A, CONTENTS
+#define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS
+TEST(TEST_NAME, s_n_printf_macro_expansion) {
+ char buf[BUFSIZ];
+ snprintf(BUF_AND_SIZE(buf), CONTENTS);
+ EXPECT_STREQ(CONTENTS, buf);
+
+ snprintf(BUF_AND_SIZE_AND_CONTENTS(buf));
+ EXPECT_STREQ(CONTENTS, buf);
+
+ sprintf(BUF_AND_CONTENTS(buf));
+ EXPECT_STREQ(CONTENTS, buf);
+}