]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
crypto/bio/bio_print.c: avoid superfluous zero padding in %#o
authorEugene Syromiatnikov <esyr@openssl.org>
Tue, 5 Aug 2025 13:14:51 +0000 (15:14 +0200)
committerNeil Horman <nhorman@openssl.org>
Fri, 29 Aug 2025 16:18:30 +0000 (12:18 -0400)
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)

crypto/bio/bio_print.c

index 4681292a2d99cdae7e112b09ce5b4ea4b15bb8b1..7600c366833b79ffc102336fa9172a10ba037c3d 100644 (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)