From de48f75e96829aab422eda963f684b30bd0eba54 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 10 Aug 2016 17:38:39 +1000 Subject: [PATCH] format: fix transcription of zero values to strings an iteration condition terminated the write when a value was zero rather than when all numerals were written. instead, iterate over the remaining numerals. fixes printf("%zu", 0u) --- format.ipp | 7 +++++-- test/format.cpp | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/format.ipp b/format.ipp index fa5cbbb4..7bbdd219 100644 --- a/format.ipp +++ b/format.ipp @@ -743,6 +743,7 @@ namespace util { namespace format { namespace detail { const auto numerals = digits (t, spec.base); const auto characters = numerals + (spec.positive_char ? 1 : 0); + CHECK_NEZ (numerals); // add any requested positive signifier if (spec.positive_char) @@ -775,11 +776,13 @@ namespace util { namespace format { namespace detail { // output is blank (though space padding/etc is still preserved). if (t != 0 || spec.precision != 0) { const char *NUMERALS = spec.uppercase ? - "0123456789ABCDEF" : + "0123456789ABCDEF": "0123456789abcdef"; char buffer[numerals]; - for (auto cursor = buffer; t; t /= spec.base) + size_t remain = numerals; + + for (auto cursor = buffer; remain--; t /= spec.base) *cursor++ = NUMERALS[t % spec.base]; std::reverse_copy (buffer, buffer + numerals, os); } diff --git a/test/format.cpp b/test/format.cpp index 4ecf9697..97564b72 100644 --- a/test/format.cpp +++ b/test/format.cpp @@ -53,6 +53,7 @@ main (void) CHECK_RENDER ("%lu", "1", (unsigned long)1); CHECK_RENDER ("%llu", "1", (unsigned long long)1); CHECK_RENDER ("%ju", "1", (uintmax_t)1); + CHECK_RENDER ("%zu", "0", (size_t)0); CHECK_RENDER ("%zu", "1", (size_t)1); CHECK_RENDER ("%o", "1", 01u);