← Commit list

8d269ef85

Denys Vlasenko
Denys Vlasenko
2021-07-12
SUCCESS Duration: 154s

Subject

awk: fix printf "%-10c", 0

Binary

No binary (build failed)

Architecture & Compiler Flags

Architecture: x86_64-linux-gnu Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0

—

Build Log

(log yok)

Commit Diff GitHub →

editors/awk.c+5 -4
@@ -2360,10 +2360,11 @@ static char *awk_printf(node *n, size_t *len)
*/
if (c == 'c') {
char cc = is_numeric(arg) ? getvar_i(arg) : *getvar_s(arg);
- s = xasprintf(s, cc);
- /* + 1 if cc == NUL: handle printf "%c" 0 case
- * (and printf "%22c" 0 etc, but still fails for e.g. printf "%-22c" 0) */
- slen = strlen(s) + (cc == '\0');
+ char *r = xasprintf(s, cc ? cc : '^' /* else strlen will be wrong */);
+ slen = strlen(r);
+ if (cc == '\0') /* if cc is NUL, re-format the string with it */
+ sprintf(r, s, cc);
+ s = r;
} else {
if (c == 's') {
s = xasprintf(s, getvar_s(arg));
testsuite/awk.tests+8 -0
@@ -415,6 +415,14 @@ testing "awk printf('%c') can output NUL" \
"awk '{printf(\"hello%c null\n\", 0)}'" "hello\0 null\n" "" "\n"
SKIP=
+optional FEATURE_AWK_GNU_EXTENSIONS
+testing "awk printf('%-10c') can output NUL" \
+ "awk 'BEGIN { printf \"[%-10c]\n\", 0 }' | od -tx1" "\
+0000000 5b 00 20 20 20 20 20 20 20 20 20 5d 0a
+0000015
+" "" ""
+SKIP=
+
# testing "description" "command" "result" "infile" "stdin"
testing 'awk negative field access' \
'awk 2>&1 -- '\''{ $(-1) }'\' \