Add a test for snprintf on a PTHREAD_STACK_MIN-sized stack.
This is a common thing for people to want to do, snprintf requires
a lot of stack for itself, and PTHREAD_STACK_MIN should be usable
for realistic code.
Change-Id: Ib09cfb4e0beec1c69ee0944c3ea4c5d03a94c491
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index 2912be4..79a86ad 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -534,6 +534,25 @@
freelocale(cloc);
}
+static void* snprintf_small_stack_fn(void*) {
+ // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
+ char buf[PATH_MAX];
+ snprintf(buf, sizeof(buf), "/proc/%d", getpid());
+ return nullptr;
+}
+
+TEST(STDIO_TEST, snprintf_small_stack) {
+ // Is it safe to call snprintf on a thread with a small stack?
+ // (The snprintf implementation puts some pretty large buffers on the stack.)
+ pthread_attr_t a;
+ ASSERT_EQ(0, pthread_attr_init(&a));
+ ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
+
+ pthread_t t;
+ ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
+ ASSERT_EQ(0, pthread_join(t, nullptr));
+}
+
TEST(STDIO_TEST, fprintf_failures_7229520) {
// http://b/7229520
FILE* fp;