From 43f7c875654cb94e589ff9d0c4ac58ca9616093e Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 5 Feb 2016 11:18:41 -0800 Subject: 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 --- tests/stdio_test.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'tests/stdio_test.cpp') diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp index 2912be4aa..79a86ad2f 100644 --- a/tests/stdio_test.cpp +++ b/tests/stdio_test.cpp @@ -534,6 +534,25 @@ TEST(STDIO_TEST, snprintf_utf8_15439554) { 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; -- cgit v1.2.3