]> Savannah Git Hosting - gnulib.git/commitdiff
vasnprintf: Fix potentially wrong zero-padding.
authorBruno Haible <bruno@clisp.org>
Wed, 22 Mar 2023 21:42:58 +0000 (22:42 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 22 Mar 2023 21:42:58 +0000 (22:42 +0100)
* lib/vasnprintf.c (VASNPRINTF): Fix zero-padding when the result starts
with a prefix "0x" or "0b".

ChangeLog
lib/vasnprintf.c

index 86321dbb06b769015e908475eac40e1bae0a193e..bd06c2864d2cca188fdc54b073fe5409ed5e45cc 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2023-03-19  Bruno Haible  <bruno@clisp.org>
+
+       vasnprintf: Fix potentially wrong zero-padding.
+       * lib/vasnprintf.c (VASNPRINTF): Fix zero-padding when the result starts
+       with a prefix "0x" or "0b".
+
 2023-03-19  Paul Eggert  <eggert@cs.ucla.edu>
 
        test-pselect, test-select: use different ports
index 1a30f20d828c6ad01546dfbba0bd5c17abb3f9a5..10709da3f1722c8646d840e0958aad8fbd8c62c6 100644 (file)
@@ -5595,6 +5595,22 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
                                   if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
                                       || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
                                     pad_ptr = NULL;
+                                  else
+                                    /* Do the zero-padding after the "0x" or
+                                       "0b" prefix, not before.  */
+                                    if (p - rp >= 2
+                                        && *rp == '0'
+                                        && (((dp->conversion == 'a'
+                                              || dp->conversion == 'x')
+                                             && rp[1] == 'x')
+                                            || ((dp->conversion == 'A'
+                                                 || dp->conversion == 'X')
+                                                && rp[1] == 'X')
+                                            || (dp->conversion == 'b'
+                                                && rp[1] == 'b')
+                                            || (dp->conversion == 'B'
+                                                && rp[1] == 'B')))
+                                      pad_ptr += 2;
                                 }
                               /* The generated string now extends from rp to p,
                                  with the zero padding insertion point being at