Skip to content

Commit

Permalink
fix(printf): fix negative argument precision
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
mpaland committed Sep 14, 2018
1 parent f8a2be3 commit 6dae168
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
precision = _atoi(&format);
}
else if (*format == '*') {
precision = (unsigned int)va_arg(va, int);
const int prec = (int)va_arg(va, int);
precision = prec > 0 ? (unsigned int)prec : 0U;
format++;
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,9 @@ TEST_CASE("misc", "[]" ) {
test::sprintf(buffer, "%.*f", 2, 0.33333333);
REQUIRE(!strcmp(buffer, "0.33"));

test::sprintf(buffer, "%.*d", -1, 1);
REQUIRE(!strcmp(buffer, "1"));

test::sprintf(buffer, "%.3s", "foobar");
REQUIRE(!strcmp(buffer, "foo"));

Expand Down

0 comments on commit 6dae168

Please sign in to comment.