From 56e9acb2927874770363462cc6f5d2ee7d2b4c6d Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 30 Oct 2023 10:47:34 -0700 Subject: [PATCH] join: fix recently introduced NUL bug * src/join.c (xfields): Simplify and fix bug with fields that start with a NUL byte when -t is not used. * tests/misc/join-utf8.sh: Also test when -t is not used, and when a field starts with NUL. --- src/join.c | 20 +++++++------------- tests/misc/join-utf8.sh | 10 +++++++--- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/join.c b/src/join.c index b3ad274659..a89b5de284 100644 --- a/src/join.c +++ b/src/join.c @@ -296,23 +296,17 @@ xfields (struct line *line) return; if (!tab.len) - { - while (ptr < lim) - { - ptr = skip_buf_matching (ptr, lim, newline_or_blank, true); - if (!*ptr) - break; - char *sep = skip_buf_matching (ptr, lim, newline_or_blank, false); - extract_field (line, ptr, sep - ptr); - ptr = sep; - } - } + while ((ptr = skip_buf_matching (ptr, lim, newline_or_blank, true)) < lim) + { + char *sep = skip_buf_matching (ptr, lim, newline_or_blank, false); + extract_field (line, ptr, sep - ptr); + ptr = sep; + } else { if (tab.ch != '\n') for (char *sep; - ((sep = skip_buf_matching (ptr, lim, eq_tab, false)) - < lim); + (sep = skip_buf_matching (ptr, lim, eq_tab, false)) < lim; ptr = sep + mcel_scan (sep, lim).len) extract_field (line, ptr, sep - ptr); diff --git a/tests/misc/join-utf8.sh b/tests/misc/join-utf8.sh index a2bc3b1e51..0e56ff5ae0 100755 --- a/tests/misc/join-utf8.sh +++ b/tests/misc/join-utf8.sh @@ -29,7 +29,10 @@ multiplication_sign='×' en_dash='–' old_Persian_word_divider='𐏐' +tflag= + for s in \ + ' ' \ "$vertical_line" \ "$multiplication_sign" \ "$en_dash" \ @@ -37,10 +40,11 @@ for s in \ do printf '0%sA\n1%sa\n2%sb\n4%sc\n' "$s" "$s" "$s" "$s" >a || framework_failure_ - printf '0%sB\n1%sd\n3%se\n4%sf\n' "$s" "$s" "$s" "$s" >b || + printf '0%sB\n1%sd\n3%se\n4%s\0f\n' "$s" "$s" "$s" "$s" >b || framework_failure_ - join -t"$s" -a1 -a2 -eouch -o0,1.2,2.2 a b >out || fail=1 - printf '0%sA%sB\n1%sa%sd\n2%sb%souch\n3%souch%se\n4%sc%sf\n' \ + join $tflag$s -a1 -a2 -eouch -o0,1.2,2.2 a b >out || fail=1 + tflag=-t + printf '0%sA%sB\n1%sa%sd\n2%sb%souch\n3%souch%se\n4%sc%s\0f\n' \ "$s" "$s" "$s" "$s" "$s" "$s" "$s" "$s" "$s" "$s" >exp || framework_failure_ compare exp out || fail=1 -- 2.47.2