From: Ismail Ramzi <[email protected]> When a name is output as is, quote_name_buf() stored the result of mbsnwidth() with MBSWIDTH_FLAGS in a size_t. Those flags make mbsnwidth() return -1 for invalid or unprintable characters, which wrapped to SIZE_MAX, and the MAX (0, ...) clamp had no effect on the unsigned value. Column layout then wrapped name lengths and line lengths, so 'ls -C' and 'ls -x' could exceed the line width, and 'ls --sort=width' sorted such names as the narrowest. E.g., in a UTF-8 locale with files a b d e f and $'c\377longname', ls -N --show-control-chars -C -w 20 output all names on one line.
* src/ls.c (quote_name_buf): Count rather than reject invalid and unprintable characters, as was done before coreutils-9.5. * tests/ls/invalid-name-width.sh: New test. * tests/local.mk: Reference the new test. * NEWS: Mention the bug fix. Link: https://github.com/coreutils/coreutils/pull/474 --- NEWS | 5 ++++ src/ls.c | 6 +++-- tests/local.mk | 1 + tests/ls/invalid-name-width.sh | 45 ++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100755 tests/ls/invalid-name-width.sh diff --git a/NEWS b/NEWS index ae06204b6..c19438e5f 100644 --- a/NEWS +++ b/NEWS @@ -222,6 +222,11 @@ GNU coreutils NEWS -*- outline -*- lookup on a file without an ACL. [bug introduced in coreutils-9.6] + 'ls' now correctly measures width for names with unescaped invalid + or non-printable characters. I.e., ls -N --show-control-chars + with any of -C, -x, -m, and --sort=width. + [bug introduced in coreutils-9.5] + 'mkfifo' and 'mknod' no longer have a race when setting the mode specified by --mode. Previously, they used lchmod, which follows symbolic links in the leading path components, to ensure the mode was properly set in the presence diff --git a/src/ls.c b/src/ls.c index 4082caf67..05944c1cc 100644 --- a/src/ls.c +++ b/src/ls.c @@ -4688,8 +4688,10 @@ quote_name_buf (char **inbuf, size_t bufsize, char *name, { if (MB_CUR_MAX > 1) { - displayed_width = mbsnwidth (buf, len, MBSWIDTH_FLAGS); - displayed_width = MAX (0, displayed_width); + /* Measure BUF after quoting, allowing for any invalid or + unprintable characters that remain. */ + int w = mbsnwidth (buf, len, 0); + displayed_width = MAX (0, w); } else { diff --git a/tests/local.mk b/tests/local.mk index 78e2995f1..1df952fd6 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -774,6 +774,7 @@ all_tests = \ tests/ls/infloop.sh \ tests/ls/line-separator.sh \ tests/ls/inode.sh \ + tests/ls/invalid-name-width.sh \ tests/ls/m-option.sh \ tests/ls/w-option.sh \ tests/ls/multihardlink.sh \ diff --git a/tests/ls/invalid-name-width.sh b/tests/ls/invalid-name-width.sh new file mode 100755 index 000000000..117d4a4c7 --- /dev/null +++ b/tests/ls/invalid-name-width.sh @@ -0,0 +1,45 @@ +#!/bin/sh +# Ensure names with invalid multi-byte characters output without quoting +# are given a sensible width for column layout and --sort=width. + +# Copyright (C) 2026 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ ls + +{ test -n "$LOCALE_FR_UTF8" && test "$LOCALE_FR_UTF8" != none; } || + skip_ 'French UTF-8 locale not available' +# Measure width in UTF-8, but sort file names containing invalid bytes in C. +unset LC_ALL +export LC_CTYPE="$LOCALE_FR_UTF8" LC_COLLATE=C + +bad=$(printf 'c\377longname') +mkdir d || framework_failure_ +touch d/a d/b d/d d/e d/f || framework_failure_ +touch "d/$bad" || skip_ 'invalid UTF-8 file names not supported' + +# Previously the width of $bad wrapped to SIZE_MAX, +# so all names were output on a single line exceeding the width. +ls -N --show-control-chars -C -w 20 -T 0 d > out || fail=1 +printf 'a %s e\nb d f\n' "$bad" > exp || framework_failure_ +compare exp out || fail=1 + +# Previously $bad sorted as the narrowest name. +ls -N --show-control-chars --sort=width d > out || fail=1 +printf 'a\nb\nd\ne\nf\n%s\n' "$bad" > exp || framework_failure_ +compare exp out || fail=1 + +Exit $fail -- 2.56.0
