From ff5d67610caab71d38625c3f1788547d00391337 Mon Sep 17 00:00:00 2001 From: zoulasc Date: Sun, 8 Dec 2019 14:41:27 -0500 Subject: [PATCH] Fix printf formats for integers (#57) * More cleanups: - sprinkle const - add a macro (setptr) that cheats const to temporarily NUL terminate strings remove casts from allocations - use strdup instead of strlen+strcpy - use x = malloc(sizeof(*x)) instead of x = malloc(sizeof(type of *x))) - add -Wcast-qual (and casts through unitptr_t in the two macros we cheat (xfree, setptr)). * More cleanups: - add const - use bounded sscanf - use snprintf instead of sprintf * More cleanup: - use snprintf/strlcat instead of sprintf/strcat - use %j instead of %l since we are casting to intmax_t/uintmax_t * Merge the 3 copies of the code that evaluated array strings with separators and convert them to keep track of lengths and use memcpy instead of strcat. * Fix formats for 32 bit machines broken by previous commit. We use intmax_t to provide maximum range for both 32 and 64 bit machines. --- run.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/run.c b/run.c index 624a784..4069f59 100644 --- a/run.c +++ b/run.c @@ -898,7 +898,7 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co break; case 'o': case 'x': case 'X': case 'u': flag = *(s-1) == 'l' ? 'd' : 'u'; - *(t-1) = 'l'; + *(t-1) = 'j'; *t = *s; *++t = '\0'; break; @@ -934,8 +934,8 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co case 'a': case 'A': case 'f': snprintf(p, BUFSZ(p), fmt, getfval(x)); break; - case 'd': snprintf(p, BUFSZ(p), fmt, (long) getfval(x)); break; - case 'u': snprintf(p, BUFSZ(p), fmt, (int) getfval(x)); break; + case 'd': snprintf(p, BUFSZ(p), fmt, (intmax_t) getfval(x)); break; + case 'u': snprintf(p, BUFSZ(p), fmt, (uintmax_t) getfval(x)); break; case 's': t = getsval(x); n = strlen(t);