Hi,

Source: coreutils master 75e1b68 (2026-11-18); isuz85() is unchanged since
at least 35492ca.

With --ignore-garbage, basenc --z85 should discard bytes outside the Z85
alphabet, and it does for most of them, but not for NUL. isuz85() in
src/basenc.c checks the punctuation with strchr(), which also matches the
terminating NUL of its string argument, so a NUL byte is treated as a Z85
character and the input then fails to decode:

$ printf 'he|l1o' | basenc --z85 -d -i | od -An -tx1 # 35 6a 51 d1, exit 0
$ printf 'he\0l1o' | basenc --z85 -d -i # basenc: invalid input, exit 1

base64, base32 and the other encodings ignore a NUL byte under -i as
expected.

The attached patch excludes NUL in isuz85() and adds a case to the basenc
test script, which fails before the change and passes after it.

Found by an LLM-assisted tool, verified manually.

Regards,
Martin Mirchev
From 1f2f29371227de8ae0661dd0b2fa47a126337273 Mon Sep 17 00:00:00 2001
From: Martin Mirchev <[email protected]>
Date: Wed, 18 Nov 2026 23:27:24 +0800
Subject: [PATCH] basenc: --z85 --ignore-garbage: discard NUL bytes

* src/basenc.c (isuz85): Do not consider NUL a valid Z85 character.
strchr also matches the terminating NUL of the alphabet, so with
--ignore-garbage a NUL byte was kept and then rejected as invalid input.
* tests/basenc/basenc.pl: Add a test case.
---
 src/basenc.c           | 3 ++-
 tests/basenc/basenc.pl | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/basenc.c b/src/basenc.c
index 3ab53b4..cafd04e 100644
--- a/src/basenc.c
+++ b/src/basenc.c
@@ -850,7 +850,8 @@ z85_length (idx_t len)
 static bool
 isuz85 (unsigned char ch)
 {
-  return c_isalnum (ch) || strchr (".-:+=^!/*?&<>()[]{}@%$#", ch) != NULL;
+  return c_isalnum (ch)
+         || (ch && strchr (".-:+=^!/*?&<>()[]{}@%$#", ch) != NULL);
 }
 
 static char const z85_encoding[85] ATTRIBUTE_NONSTRING =
diff --git a/tests/basenc/basenc.pl b/tests/basenc/basenc.pl
index a60cef6..9c440e1 100755
--- a/tests/basenc/basenc.pl
+++ b/tests/basenc/basenc.pl
@@ -242,6 +242,7 @@ my @Tests =
  ['z85_4', '--z85 -d -i',  {IN=>' '. $z85_out},  {OUT=>$z85_in}],
  ['z85_5', '--z85 -d',     {IN=>'%j$qP'},  {OUT=>"\xFF\xDD\xBB\x99"}],
  ['z85_6', '--z85 -d -i',  {IN=>'%j~$qP'}, {OUT=>"\xFF\xDD\xBB\x99"}],
+ ['z85_7i', '--z85 -d -i', {IN=>"%j\0\$qP"}, {OUT=>"\xFF\xDD\xBB\x99"}],
 
  # z85 encoding require input to be multiple of 5 octets
  ['z85_7', '--z85 -d',  {IN=>'hello'},   {OUT=>"5jXu"}],
-- 
2.50.1

Reply via email to