From a5a920383fe85f4dada0cb80418f7a935c12dc4a Mon Sep 17 00:00:00 2001 From: Sean Kurtz Date: Sat, 28 Nov 2026 00:26:01 -0600 Subject: 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. --- src/printf.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/printf.c') 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': -- cgit From d60c99985c1c4e058dd2de7e6272ef4d48a14686 Mon Sep 17 00:00:00 2001 From: Sean Kurtz Date: Sat, 28 Nov 2026 00:50:57 -0600 Subject: perf(pattern,printf): skip the class table on ASCII, a bare %s through vsnprintf in_range() looked the two ends of `[a-z]` up in the alpha/digit tables to get alphabet order. In ASCII the code order *is* the alphabet order, so the compiler can take one comparison; the tables stay for EBCDIC. convert() formatted a bare "%s" through the spec builder and vsnprintf(), which pads, truncates and checks flags it was never given. Out it goes straight to out_puts(); anything with a flag, width or precision still takes the general path. make check unchanged (126 passed, 1 skipped, known linux/i-dscomplete); the whole suite under ASan+UBSan reports nothing. --- src/pattern.c | 5 +++++ src/printf.c | 4 ++++ 2 files changed, 9 insertions(+) (limited to 'src/printf.c') diff --git a/src/pattern.c b/src/pattern.c index 46c667c..dded417 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -30,6 +30,10 @@ static int index_in(const char *set, unsigned char c) { * alphabet. Any other range is by character code. */ static bool in_range(unsigned char lo, unsigned char hi, unsigned char c) { +#if ZASH_ASCII + /* Code order is alphabet order here: one comparison, no table. */ + return lo <= c && c <= hi; +#else static const char *const sets[] = { "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", @@ -44,6 +48,7 @@ static bool in_range(unsigned char lo, unsigned char hi, unsigned char c) { } } return lo <= c && c <= hi; +#endif } /** diff --git a/src/printf.c b/src/printf.c index f366f2d..fabc0b3 100644 --- a/src/printf.c +++ b/src/printf.c @@ -240,6 +240,10 @@ static const char *convert(printf_state *pf, const char *f) { } case 's': { const char *s = next_arg(pf); + if (n == 1) { /* a bare "%s": there is nothing to pad or truncate */ + out_puts(&out1, s ? s : ""); + break; + } spec[n++] = 's'; spec[n] = '\0'; out_printf(&out1, spec, s ? s : ""); -- cgit