diff --git a/printf.c b/printf.c index 56f60d69..f1a51e2d 100644 --- a/printf.c +++ b/printf.c @@ -647,7 +647,8 @@ int printf(const char* format, ...) { va_list va; va_start(va, format); - const int ret = _vsnprintf(_out_char, NULL, (size_t)-1, format, va); + char buffer[1]; + const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va); va_end(va); return ret; } diff --git a/test/test_suite.cpp b/test/test_suite.cpp index a5c087c2..9a02b1e9 100644 --- a/test/test_suite.cpp +++ b/test/test_suite.cpp @@ -40,23 +40,38 @@ namespace test { // dummy putchar -void test::_putchar(char) -{ } +static char printf_buffer[100]; +static size_t printf_idx = 0U; +void test::_putchar(char character) +{ + printf_buffer[printf_idx++] = character; +} TEST_CASE("printf", "[]" ) { - char buffer[100] ; + char buffer[100]; + printf_idx = 0U; + memset(printf_buffer, 0xCC, 100U); REQUIRE(test::printf("% d", 4232) == 5); + REQUIRE(!strcmp(printf_buffer, " 4232")); +} + + +TEST_CASE("snprintf", "[]" ) { + char buffer[100]; test::snprintf(buffer, 100U, "%d", -1000); REQUIRE(!strcmp(buffer, "-1000")); + + test::snprintf(buffer, 3U, "%d", -1000); + REQUIRE(!strcmp(buffer, "-1")); } TEST_CASE("space flag", "[]" ) { - char buffer[100] ; + char buffer[100]; test::sprintf(buffer, "% d", 42); REQUIRE(!strcmp(buffer, " 42")); @@ -130,7 +145,7 @@ TEST_CASE("space flag", "[]" ) { TEST_CASE("+ flag", "[]" ) { - char buffer[100] ; + char buffer[100]; test::sprintf(buffer, "%+d", 42); REQUIRE(!strcmp(buffer, "+42")); @@ -195,7 +210,7 @@ TEST_CASE("+ flag", "[]" ) { TEST_CASE("0 flag", "[]" ) { - char buffer[100] ; + char buffer[100]; test::sprintf(buffer, "%0d", 42); REQUIRE(!strcmp(buffer, "42"));