Pádraig Brady <[email protected]> writes:

> On 05/01/2026 00:54, Collin Funk wrote:
>> * init.cfg (bad_unicode): New function, copied from
>> tests/fold/fold-characters.sh with the NUL removed.
>> * tests/fold/fold-characters.sh (bad_unicode): Rename to
>> bad_unicode_with_nul. Reformat long line.
>> * tests/mktemp/bad-unicode.sh: New test.
>> * tests/local.mk (all_tests): Add the new test.
>
> There were a few issues with this:
>
> It's actually creating files, and theoretically some file systems
> may reject the bad unicode, which would induce a false failure.
>
> Also it's better to use a cleanup_() function to remove files etc.
> to handle the case where a test is terminated early.
>
> Both the above issues can be avoided by passing -u to mktemp,
> so that it only does internal processing.
>
> There were typos with --tempdir and test -z.

Oops, silly mistakes on my part. I think it is more beneficial to ensure
the files and directories are created if the file system and operating
system support it.

How about using a shell redirection to check if bad unicode in file
names are supported? I attached a v2 patch doing that.

Also, I made it so files and directories are only created under the
current working directory. That removes the need for manual cleanup.

> The tests/misc/mktemp.pl should be moved to
> the tests/mktemp dir as part of this.

Yep, I was going to do that afterwards.

Collin

>From 9aa8eea8b4b34c52956dbb664391744f754e9841 Mon Sep 17 00:00:00 2001
Message-ID: <9aa8eea8b4b34c52956dbb664391744f754e9841.1767664191.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Sun, 4 Jan 2026 16:51:09 -0800
Subject: [PATCH v2] tests: mktemp: add tests for invalid Unicode options

* init.cfg (bad_unicode): New function, copied from
tests/fold/fold-characters.sh with the NUL removed.
* tests/fold/fold-characters.sh (bad_unicode): Rename to
bad_unicode_with_nul. Reformat long line.
* tests/mktemp/bad-unicode.sh: New test.
* tests/local.mk (all_tests): Add the new test.
---
 init.cfg                      |  9 +++++++
 tests/fold/fold-characters.sh |  7 ++---
 tests/local.mk                |  1 +
 tests/mktemp/bad-unicode.sh   | 50 +++++++++++++++++++++++++++++++++++
 4 files changed, 64 insertions(+), 3 deletions(-)
 create mode 100755 tests/mktemp/bad-unicode.sh

diff --git a/init.cfg b/init.cfg
index 55a75333c..82d70b77f 100644
--- a/init.cfg
+++ b/init.cfg
@@ -828,4 +828,13 @@ require_gnu_()
     || skip_ 'not running on GNU/Hurd'
 }
 
+# Sequence derived from <https://datatracker.ietf.org/doc/rfc9839>.
+bad_unicode ()
+{
+  require_built_ printf
+
+  # invalid UTF8|unpaired surrogate|C1 control|noncharacter
+  env printf '\xC3|\xED\xBA\xAD|\u0089|\xED\xA6\xBF\xED\xBF\xBF\n'
+}
+
 sanitize_path_
diff --git a/tests/fold/fold-characters.sh b/tests/fold/fold-characters.sh
index 0b399a1b6..b44276eaa 100755
--- a/tests/fold/fold-characters.sh
+++ b/tests/fold/fold-characters.sh
@@ -84,13 +84,14 @@ fold --characters input3 | tail -n 4 > out3 || fail=1
 compare exp3 out3 || fail=1
 
 # Sequence derived from <https://datatracker.ietf.org/doc/rfc9839>.
-bad_unicode ()
+bad_unicode_with_nul ()
 {
   # invalid UTF8|unpaired surrogate|NUL|C1 control|noncharacter
   env printf '\xC3|\xED\xBA\xAD|\u0000|\u0089|\xED\xA6\xBF\xED\xBF\xBF\n'
 }
-bad_unicode > /dev/null || framework_failure_
-test $({ bad_unicode | fold; bad_unicode; } | uniq | wc -l) = 1 || fail=1
+bad_unicode_with_nul > /dev/null || framework_failure_
+test $({ bad_unicode_with_nul | fold; \
+         bad_unicode_with_nul; } | uniq | wc -l) = 1 || fail=1
 # Check bad character at EOF
 test $(env printf '\xC3' | fold | wc -c) = 1 || fail=1
 
diff --git a/tests/local.mk b/tests/local.mk
index 5547a3f9a..074106e67 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -279,6 +279,7 @@ all_tests =					\
   tests/od/od.pl				\
   tests/od/od-endian.sh				\
   tests/od/od-float.sh				\
+  tests/mktemp/bad-unicode.sh			\
   tests/misc/mktemp.pl				\
   tests/misc/arch.sh				\
   tests/pr/bounded-memory.sh			\
diff --git a/tests/mktemp/bad-unicode.sh b/tests/mktemp/bad-unicode.sh
new file mode 100755
index 000000000..2583a251a
--- /dev/null
+++ b/tests/mktemp/bad-unicode.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+# Test 'mktemp' with bad Unicode characters.
+
+# 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_ mktemp
+
+: > "$(bad_unicode)" || skip_ 'bad unicode not supported in file names'
+
+for loc in C "$LOCALE_FR" "$LOCALE_FR_UTF8"; do
+  test -z "$loc" && continue
+  # Bad Unicode as a suffix.
+  file1=$(mktemp --tmpdir='.' --suffix=$(bad_unicode)) || fail=1
+  test -n "$file1" && test -f "$file1" || fail=1
+  dir1=$(mktemp --tmpdir='.' -d --suffix=$(bad_unicode)) || fail=1
+  if test -n "$dir1"; then
+    test -d "$dir1" || fail=1
+    # Bad Unicode in the argument to --tmpdir.
+    file2=$(mktemp --tmpdir="$dir1") || fail=1
+    test -n "$file2" && test -f "$file2" || fail=1
+    dir2=$(mktemp -d --tmpdir="$dir1") || fail=1
+    test -n "$dir2" && test -d "$dir2" || fail=1
+    # Bad Unicode in $TMPDIR.
+    file3=$(TMPDIR="$dir1" mktemp) || fail=1
+    test -n "$file3" && test -f "$file3" || fail=1
+    dir3=$(TMPDIR="$dir1" mktemp -d) || fail=1
+    test -n "$dir3" && test -d "$dir3" || fail=1
+    # Bad Unicode in the argument to -t.
+    file4=$(TMPDIR='.' mktemp -t "$(bad_unicode)XXXXXX") || fail=1
+    test -n "$file4" && test -f "$file4" || fail=1
+    dir4=$(TMPDIR='.' mktemp -d -t "$(bad_unicode)XXXXXX") || fail=1
+    test -n "$dir4" && test -d "$dir4" || fail=1
+  fi
+done
+
+Exit $fail
-- 
2.52.0

Reply via email to