[Commits] SDL: SDL_vsnprintf: fix numerics if both zero-padding and a fiel...

libsdl.org revision control commits-owner at libsdl.org
Fri Sep 28 14:51:28 PDT 2018


details:   https://hg.libsdl.org/SDL/rev/2dbf011db466
changeset: 12253:2dbf011db466
user:      Ozkan Sezer <sezeroz at gmail.com>
date:      Sat Sep 29 00:51:24 2018 +0300
description:
SDL_vsnprintf: fix numerics if both zero-padding and a field are given.

it used to place zeroes between the sign and the number. (space-padding
from within SDL_PrintString() seems OK:  spaces are added before sign.)

also fixed the maxlen handling if the number has a sign.

diffstat:

 src/stdlib/SDL_string.c |  24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)

diffs (46 lines):

diff -r 9a334644da3e -r 2dbf011db466 src/stdlib/SDL_string.c
--- a/src/stdlib/SDL_string.c	Fri Sep 28 17:01:24 2018 +0300
+++ b/src/stdlib/SDL_string.c	Sat Sep 29 00:51:24 2018 +0300
@@ -1416,15 +1416,19 @@
 static void
 SDL_IntPrecisionAdjust(char *num, size_t maxlen, SDL_FormatInfo *info)
 {/* left-pad num with zeroes. */
-    size_t sz, pad;
+    size_t sz, pad, have_sign;
 
-    if (!info || info->precision < 0)
+    if (!info)
         return;
 
-    if (*num == '-')
+    have_sign = 0;
+    if (*num == '-' || *num == '+') {
+        have_sign = 1;
         ++num;
+        --maxlen;
+    }
     sz = SDL_strlen(num);
-    if (sz < (size_t)info->precision) {
+    if (info->precision > 0 && sz < (size_t)info->precision) {
         pad = (size_t)info->precision - sz;
         if (pad + sz + 1 <= maxlen) { /* otherwise ignore the precision */
             SDL_memmove(num + pad, num, sz + 1);
@@ -1432,6 +1436,18 @@
         }
     }
     info->precision = -1;/* so that SDL_PrintString() doesn't make a mess. */
+
+    if (info->pad_zeroes && info->width > 0 && (size_t)info->width > sz + have_sign) {
+    /* handle here: spaces are added before the sign
+       but zeroes must be placed _after_ the sign. */
+    /* sz hasn't changed: we ignore pad_zeroes if a precision is given. */
+        pad = (size_t)info->width - sz - have_sign;
+        if (pad + sz + 1 <= maxlen) {
+            SDL_memmove(num + pad, num, sz + 1);
+            SDL_memset(num, '0', pad);
+        }
+        info->width = 0; /* so that SDL_PrintString() doesn't make a mess. */
+    }
 }
 
 static size_t


More information about the commits mailing list