diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:26:01 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:26:01 -0600 |
| commit | a5a920383fe85f4dada0cb80418f7a935c12dc4a (patch) | |
| tree | 812c7372c1f25d6ed4ef2cfb7212ff3c84aadc89 | |
| parent | ec1cd3eb8e1aa177329f25d134f372d3e999d4a4 (diff) | |
| download | zash-a5a920383fe85f4dada0cb80418f7a935c12dc4a.tar.gz | |
fix(printf): a negative '*' precision is no precision
`printf '%.*d' -5 1` built the conversion "%.-5jd" and printed it
literally: a width and precision taken with '*' were written with "%d"
of an (int) cast, so a negative precision produced ".-5" in the spec.
copy_number() now writes the value with "%jd" - no narrowing, which the
review asked for - and, for a precision, drops the '.' again when the
value is negative: C99 says a negative precision is as if it were
omitted. A negative width stays, where snprintf() reads the '-' as a
flag.
tests/posix/printf-ext.sh covers all four shapes; the expected output
is what dash and bash print.
Verified: make check, and the same test under ASan+UBSan on Linux.
| -rw-r--r-- | src/printf.c | 20 | ||||
| -rw-r--r-- | tests/posix/printf-ext.exp | bin | 315 -> 338 bytes | |||
| -rw-r--r-- | tests/posix/printf-ext.sh | 3 |
3 files changed, 19 insertions, 4 deletions
diff --git a/src/printf.c b/src/printf.c index 3e33c53..f366f2d 100644 --- a/src/printf.c +++ b/src/printf.c @@ -150,12 +150,23 @@ static double arg_double(printf_state *pf) { * @param f Position in the format. * @param spec Specification being built. * @param n Receives the new length of `spec`. + * @param dot Length of `spec` before the '.' that starts this precision, + * or 0 when this is the width. * @return Position in the format after the number. */ static const char *copy_number(printf_state *pf, const char *f, char *spec, - size_t *n) { + size_t *n, size_t dot) { if (*f == '*') { - *n += (size_t)snprintf(spec + *n, 16, "%d", (int)arg_int(pf)); + intmax_t v = arg_int(pf); + /* + * C99: a negative precision is as if the precision were omitted, + * so drop the '.' as well; a negative width is a '-' flag, which + * snprintf() reads from the "%*d" this writes. + */ + if (dot && v < 0) + *n = dot; + else + *n += (size_t)snprintf(spec + *n, 24, "%jd", v); return f + 1; } int digits = 0; @@ -184,10 +195,11 @@ static const char *convert(printf_state *pf, const char *f) { spec[n++] = *f; f++; } - f = copy_number(pf, f, spec, &n); + f = copy_number(pf, f, spec, &n, 0); if (*f == '.') { + size_t dot = n; spec[n++] = '.'; - f = copy_number(pf, f + 1, spec, &n); + f = copy_number(pf, f + 1, spec, &n, dot); } switch (*f) { case 'd': diff --git a/tests/posix/printf-ext.exp b/tests/posix/printf-ext.exp Binary files differindex a94b9f3..8ddaab7 100644 --- a/tests/posix/printf-ext.exp +++ b/tests/posix/printf-ext.exp diff --git a/tests/posix/printf-ext.sh b/tests/posix/printf-ext.sh index 60bc69c..066aa2e 100644 --- a/tests/posix/printf-ext.sh +++ b/tests/posix/printf-ext.sh @@ -1,6 +1,9 @@ # printf: conversions, widths, escapes and errors. printf '%5.2f|%-8.3e|%g|%G\n' 3.14159 31415.9 0.0001 1e20 printf '%*d|%-*d|%.*f\n' 6 42 6 42 2 2.71828 +# A negative '*' precision means no precision at all; a negative width +# is a '-' flag. (It used to build "%.-5jd", which printf rejected.) +printf '%.*d|%.*s|%*.*f|%0*d|\n' -5 1 -1 abc 3 -2 1.5 -6 42 printf '%c%c%c\n' abc "" z | od -An -c | tr -s ' ' printf '%5c|%-5c|\n' x y printf '%x %X %o %u %i\n' 255 255 8 42 -7 |
