Skip to content

Commit

Permalink
Exposing bugs mpaland#75, mpaland#99, mpaland#109, mpaland#110 via th…
Browse files Browse the repository at this point in the history
…e test suite.
  • Loading branch information
Eyal Rozenberg authored and eyalroz committed Jul 30, 2021
1 parent 5daeb48 commit 0002294
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
6 changes: 3 additions & 3 deletions printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,14 @@ static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen


// internal itoa format
static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)
static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int precision, unsigned int width, unsigned int flags)
{
// pad leading zeros
if (!(flags & FLAGS_LEFT)) {
if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
width--;
}
while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
while ((len < precision) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = '0';
}
while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
Expand All @@ -243,7 +243,7 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma

// handle hash
if (flags & (FLAGS_HASH | FLAGS_POINTER)) {
if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) {
if (!(flags & FLAGS_PRECISION) && len && ((len == precision) || (len == width))) {
len--;
if (len && (base == 16U)) {
len--;
Expand Down
36 changes: 33 additions & 3 deletions test/test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,37 @@ TEST_CASE("- flag - non-standard format", "[]" ) {
TEST_CASE("# flag", "[]" ) {
char buffer[100];

test::sprintf(buffer, "%#.0x", 0);
REQUIRE(!strcmp(buffer, ""));
test::sprintf(buffer, "%#.1x", 0);
test::sprintf(buffer, "%#o", 0);
REQUIRE(!strcmp(buffer, "0"));
test::sprintf(buffer, "%#0o", 0);
REQUIRE(!strcmp(buffer, "0"));
test::sprintf(buffer, "%#.0o", 0);
REQUIRE(!strcmp(buffer, "0"));
test::sprintf(buffer, "%#.1o", 0);
REQUIRE(!strcmp(buffer, "0"));
test::sprintf(buffer, "%#4o", 0);
REQUIRE(!strcmp(buffer, " 0"));
test::sprintf(buffer, "%#.4o", 0);
REQUIRE(!strcmp(buffer, "0000"));

test::sprintf(buffer, "%#o", 1);
REQUIRE(!strcmp(buffer, "01"));
test::sprintf(buffer, "%#0o", 1);
REQUIRE(!strcmp(buffer, "01"));
test::sprintf(buffer, "%#.0o", 1);
REQUIRE(!strcmp(buffer, "01"));
test::sprintf(buffer, "%#.1o", 1);
REQUIRE(!strcmp(buffer, "01"));
test::sprintf(buffer, "%#4o", 1);
REQUIRE(!strcmp(buffer, " 01"));
test::sprintf(buffer, "%#.4o", 1);
REQUIRE(!strcmp(buffer, "0001"));

test::sprintf(buffer, "%#04x", 0x1001);
REQUIRE(!strcmp(buffer, "0x1001"));
test::sprintf(buffer, "%#04o", 01001);
REQUIRE(!strcmp(buffer, "01001"));

test::sprintf(buffer, "%#.0llx", (long long)0);
REQUIRE(!strcmp(buffer, ""));
test::sprintf(buffer, "%#.8x", 0x614e);
Expand Down Expand Up @@ -1341,6 +1368,9 @@ TEST_CASE("float", "[]" ) {
REQUIRE(!strcmp(buffer, "a0.5 end"));

#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
test::sprintf(buffer, "%.4g", 1.0);
REQUIRE(!strcmp(buffer, "1"));

test::sprintf(buffer, "%G", 12345.678);
REQUIRE(!strcmp(buffer, "12345.7"));

Expand Down

0 comments on commit 0002294

Please sign in to comment.