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 <esyr@openssl.org>

Reviewed-by: Saša Nedvědický <sashan@openssl.org>
Reviewed-by: Neil Horman <nhorman@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/28177)
This commit is contained in:
Eugene Syromiatnikov 2025-08-05 15:14:51 +02:00 committed by Neil Horman
parent 0f6e826f7b
commit 0f107c709c

View file

@ -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)