summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Regan <john@jrjrtech.com>2019-02-05 18:12:24 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2019-02-05 19:21:51 +0000
commitaac54c9d0f36ecadd205da3985996a9e56e1fa14 (patch)
tree2d193e32d27ab94f31c21e31e21fb9e6eff21066 /src
parentf5322e41ae8fa0124bb5adcb3ef12db6ebeabeab (diff)
downloadskalibs-aac54c9d0f36ecadd205da3985996a9e56e1fa14.tar.xz
fix null-pointer behavior in fmt and fmtlist functions
This fixes a segfault when a user calls uint320_fmt(NULL,...), and makes the fmtlist functions return the correct number of characters when called with NULL.
Diffstat (limited to 'src')
-rw-r--r--src/libstddjb/fmtscan-internal.h23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/libstddjb/fmtscan-internal.h b/src/libstddjb/fmtscan-internal.h
index 34994a0..0e68788 100644
--- a/src/libstddjb/fmtscan-internal.h
+++ b/src/libstddjb/fmtscan-internal.h
@@ -118,11 +118,8 @@ size_t uint##bits##_fmtlist (char *s, uint##bits##_t const *tab, size_t n) \
{ \
size_t w = uint##bits##_fmt(s, tab[i]) ; \
len += w ; \
- if (s) \
- { \
- s += w ; \
- if (i < n-1) { *s++ = ',' ; len++ ; } \
- } \
+ if (s) s += w ; \
+ if (i < n-1) { len++ ; if (s) *s++ = ',' ; } \
} \
return len ; \
} \
@@ -146,9 +143,14 @@ size_t uint##bits##_fmt_base (char *s, uint##bits##_t x, unsigned int base) \
#define FMTB0(bits) \
size_t uint##bits##0_fmt_base (char *s, uint##bits##_t x, size_t n, unsigned int base) \
{ \
+ size_t r = n ; \
size_t len = uint##bits##_fmt_base(0, x, base) ; \
- while (n-- > len) *s++ = '0' ; \
- return uint##bits##_fmt_base(s, x, base) ; \
+ if (s) \
+ { \
+ while (n-- > len) *s++ = '0' ; \
+ len = uint##bits##_fmt_base(s, x, base) ; \
+ } \
+ return len > r ? len : r ; \
} \
#define FMTS(bits) \
@@ -167,11 +169,8 @@ size_t int##bits##_fmtlist (char *s, int##bits##_t const *tab, size_t n) \
{ \
size_t w = int##bits##_fmt(s, tab[i]) ; \
len += w ; \
- if (s) \
- { \
- s += w ; \
- if (i < n-1) { *s++ = ',' ; len++ ; } \
- } \
+ if (s) s += w ; \
+ if (i < n-1) { len++ ; if (s) *s++ = ',' ; } \
} \
return len ; \
} \