From 0f107c709c7392e9cc472e9b82448880d9b3afb1 Mon Sep 17 00:00:00 2001 From: Eugene Syromiatnikov Date: Tue, 5 Aug 2025 15:14:51 +0200 Subject: [PATCH] crypto/bio/bio_print.c: avoid superfluous zero padding in %#o MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Zero prefix in the alternative octal form count towards precision, per [1]: For o conversion, it **shall increase the precision**... [1] https://pubs.opengroup.org/onlinepubs/9799919799//functions/printf.html Signed-off-by: Eugene Syromiatnikov Reviewed-by: Saša Nedvědický Reviewed-by: Neil Horman (Merged from https://github.com/openssl/openssl/pull/28177) --- crypto/bio/bio_print.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crypto/bio/bio_print.c b/crypto/bio/bio_print.c index 4681292a2d9..7600c366833 100644 --- a/crypto/bio/bio_print.c +++ b/crypto/bio/bio_print.c @@ -452,6 +452,8 @@ fmtint(char **sbuffer, size_t *currlen, size_t *maxlen, int64_t value, int base, int min, int max, int flags) { + static const char oct_prefix[] = "0"; + int signvalue = 0; const char *prefix = ""; uint64_t uvalue; @@ -476,7 +478,7 @@ fmtint(char **sbuffer, if (flags & DP_F_NUM) { if (value != 0) { if (base == 8) - prefix = "0"; + prefix = oct_prefix; if (base == 16) prefix = flags & DP_F_UP ? "0X" : "0x"; } @@ -492,7 +494,12 @@ fmtint(char **sbuffer, place--; convert[place] = 0; - zpadlen = max - place; + /* + * "#" (alternative form): + * - For o conversion, it shall increase the precision, if and only + * if necessary, to force the first digit of the result to be a zero + */ + zpadlen = max - place - (prefix == oct_prefix); spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - (int)strlen(prefix); if (zpadlen < 0) -- 2.47.3