Skip to content

Commit

Permalink
fix(printf): pass dummy buffer for printf to _vsnprintf instead of NULL
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
mpaland committed May 13, 2018
1 parent a6e81f9 commit d12e52b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
27 changes: 21 additions & 6 deletions test/test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
Expand Down

0 comments on commit d12e52b

Please sign in to comment.