From: Eugene Syromiatnikov Date: Tue, 5 Aug 2025 13:14:51 +0000 (+0200) Subject: crypto/bio/bio_print.c: avoid superfluous zero padding in %#o X-Git-Tag: openssl-3.6.0-alpha1~41 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0f107c709c73;p=thirdparty%2Fopenssl.git crypto/bio/bio_print.c: avoid superfluous zero padding in %#o 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) --- 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)