summaryrefslogtreecommitdiff
path: root/src/stdlib
diff options
context:
space:
mode:
authorOzkan Sezer <sezeroz@gmail.com>2018-09-29 00:51:24 +0300
committerOzkan Sezer <sezeroz@gmail.com>2018-09-29 00:51:24 +0300
commitcee2dd18864a03d840eb65484a17e31326aa50f5 (patch)
tree9b2019267a9eab3f406b8be1e762a04bbc37f1b3 /src/stdlib
parent68ea79728f6f2da2f2854e0410e35f0ed6e2ee3e (diff)
downloadsdl-cee2dd18864a03d840eb65484a17e31326aa50f5.tar.gz
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 (limited to 'src/stdlib')
-rw-r--r--src/stdlib/SDL_string.c24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index a41dc2534..94e461bbf 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -1416,15 +1416,19 @@ SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *str
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 @@ SDL_IntPrecisionAdjust(char *num, size_t maxlen, SDL_FormatInfo *info)
}
}
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