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.
This commit is contained in:
zoulasc 2019-12-08 14:41:27 -05:00 committed by Arnold Robbins
parent 108224b484
commit ff5d67610c

6
run.c
View File

@ -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);