From 7075d314a0875caa45c8571f7aa52f732f3612a9 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Fri, 14 Sep 2018 14:42:41 +0200 Subject: fix(printf): zero precision and zero value hash problem Fixes #26 --- printf.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'printf.c') diff --git a/printf.c b/printf.c index d71665f..9ef669f 100644 --- a/printf.c +++ b/printf.c @@ -163,9 +163,9 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma // handle hash if (flags & FLAGS_HASH) { - if (((len == prec) || (len == width)) && (len > 0U)) { + if (len && ((len == prec) || (len == width))) { len--; - if ((base == 16U) && (len > 0U)) { + if (len && (base == 16U)) { len--; } } @@ -181,7 +181,7 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma } // handle sign - if ((len == width) && (negative || (flags & FLAGS_PLUS) || (flags & FLAGS_SPACE))) { + if (len && (len == width) && (negative || (flags & FLAGS_PLUS) || (flags & FLAGS_SPACE))) { len--; } if (len < PRINTF_NTOA_BUFFER_SIZE) { @@ -225,6 +225,11 @@ static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxl char buf[PRINTF_NTOA_BUFFER_SIZE]; size_t len = 0U; + // no hash for 0 values + if (!value) { + flags &= ~FLAGS_HASH; + } + // write if precision != 0 and value is != 0 if (!(flags & FLAGS_PRECISION) || value) { do { @@ -245,6 +250,11 @@ static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t char buf[PRINTF_NTOA_BUFFER_SIZE]; size_t len = 0U; + // no hash for 0 values + if (!value) { + flags &= ~FLAGS_HASH; + } + // write if precision != 0 and value is != 0 if (!(flags & FLAGS_PRECISION) || value) { do { -- cgit v1.2.3