Skip to content

Commit

Permalink
Fixes mpaland#121: Now properly accounting for a 2-character prefix f…
Browse files Browse the repository at this point in the history
…or `%#b`, like for `%#x`, when a precision is field width is specified and we are matching it exactly. Also, added some test suite checks for this bug.
  • Loading branch information
eyalroz committed Feb 28, 2022
1 parent 6da9ea4 commit 8b831c1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/printf/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,10 @@ static void print_integer_finalization(output_gadget_t* output, char* buf, print
// Let's take back some padding digits to fit in what will eventually
// be the format-specific prefix
if (unpadded_len < len) {
len--;
len--; // This should suffice for BASE_OCTAL
}
if (len && (base == BASE_HEX) && (unpadded_len < len)) {
len--;
if (len && (base == BASE_HEX || base == BASE_BINARY) && (unpadded_len < len)) {
len--; // ... and an extra one for 0x or 0b
}
}
if ((base == BASE_HEX) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_INTEGER_BUFFER_SIZE)) {
Expand Down
6 changes: 5 additions & 1 deletion test/test_suite_main_testcases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ PRINTF_TEST_CASE(sharp_flag)
PRINTF_TEST_CASE(sharp_flag__non_standard_format)
{
char buffer[base_buffer_size];
PRINTING_CHECK("0b110", ==, sprintf_, buffer, "%#b", 6);
PRINTING_CHECK("0b110", ==, sprintf_, buffer, "%#b", 6);
PRINTING_CHECK("0b11111111", ==, sprintf_, buffer, "%#010b", 0xff);
PRINTING_CHECK("0b011111111", ==, sprintf_, buffer, "%#011b", 0xff);
PRINTING_CHECK("077", ==, sprintf_, buffer, "%#03o", 077);
PRINTING_CHECK("0077", ==, sprintf_, buffer, "%#04o", 077);
}

#endif
Expand Down

0 comments on commit 8b831c1

Please sign in to comment.