Subject: bug#29006: gunzip does not works with an uppercase suffix

From: Jim Meyering <[email-masked]>
Date: Sun, 29 Oct 2017 07:06:19 -0700

On Sun, Oct 29, 2017 at 6:51 AM, Jim Meyering <[email-masked]> wrote:
> On Wed, Oct 25, 2017 at 11:21 PM, meo (XeniaLab) <[email-masked]> wrote:
>> its seems that gunzip does not works with uppercase suffixes...
>
> Thank you for the report. That is a bug, indeed. Here is a small reproducer:
>
>   $ :|gzip > kT && gzip -d -S T kT
>   gzip: kT: unknown suffix -- ignored
>
> I have a fix and will post it shortly.

Thanks again. Here's the patch.
I've attributed your email address in the commit log.
If you'd prefer I use a name, tell me what to use soon.
Once I push, that log message cannot be modified.
From 621b71dc5a3e523d4663d9021a9bbcc29c19690d Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email-masked]>
Date: Sat, 28 Oct 2017 14:15:04 -0700
Subject: [PATCH] gzip: fix bug with any upper case custom ('-S'-specified)
 suffix

Any user-specified suffix with an upper case
letter would fail to match desired file.
* gzip.c (get_suffix): First, arrange to have only one return
point rather than two. Put a lower-cased (just-malloc'd) copy
of z_suffix in the suffix vector, and free it before returning.
* tests/upper-suffix: New file.
* tests/Makefile.am (TESTS): Add it.
* NEWS: Mention it.
Reported by [email-masked] in https://bugs.gnu.org/29006
---
 NEWS               |  6 ++++++
 gnulib             |  2 +-
 gzip.c             | 13 ++++++++++---
 tests/Makefile.am  |  1 +
 tests/upper-suffix | 33 +++++++++++++++++++++++++++++++++
 5 files changed, 51 insertions(+), 4 deletions(-)
 create mode 100755 tests/upper-suffix

diff --git a/NEWS b/NEWS
index 4a280c4..2b7a168 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,12 @@ GNU gzip NEWS                                    -*- outline -*-

 ** Bug fixes

+  gzip -d -S SUFFIX file.SUFFIX would fail for any upper-case byte in SUFFIX.
+  E.g., before, this command would fail:
+    $ :|gzip > kT && gzip -d -S T kT
+    gzip: kT: unknown suffix -- ignored
+  [bug present since the beginning]
+
   When decompressing data in 'pack' format, gzip no longer mishandles
   leading zeros in the end-of-block code.  [bug introduced in gzip-1.6]

diff --git a/gnulib b/gnulib
index b86cc86..4f0a093 160000
--- a/gnulib
+++ b/gnulib
@@ -1 +1 @@
-Subproject commit b86cc86ae41e68d0484c5c3957b867e4725bb986
+Subproject commit 4f0a093a593d37233c262c66ba82d8bc6b780b23
diff --git a/gzip.c b/gzip.c
index 09a1650..cfc4fe3 100644
--- a/gzip.c
+++ b/gzip.c
@@ -77,6 +77,7 @@ static char const *const license_msg[] = {
 #include "ignore-value.h"
 #include "stat-time.h"
 #include "version.h"
+#include "xalloc.h"
 #include "yesno.h"

                 /* configuration */
@@ -1180,9 +1181,12 @@ local char *get_suffix(name)
             break;
           }
       }
+
+    char *z_lower = xstrdup(z_suffix);
+    strlwr(z_lower);
     known_suffixes[suffix_of_builtin
                    ? sizeof known_suffixes / sizeof *known_suffixes - 2
-                   : 0] = z_suffix;
+                   : 0] = z_lower;
     suf = known_suffixes + suffix_of_builtin;

     nlen = strlen(name);
@@ -1193,15 +1197,18 @@ local char *get_suffix(name)
     }
     strlwr(suffix);
     slen = strlen(suffix);
+    char *match = NULL;
     do {
        int s = strlen(*suf);
        if (slen > s && ! ISSLASH (suffix[slen - s - 1])
            && strequ(suffix + slen - s, *suf)) {
-           return name+nlen-s;
+           match = name+nlen-s;
+           break;
        }
     } while (*++suf != NULL);
+    free(z_lower);

-    return NULL;
+    return match;
 }

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3b1c824..ed5dfdf 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -29,6 +29,7 @@ TESTS =					\
   trailing-nul				\
   unpack-invalid			\
   unpack-valid				\
+  upper-suffix				\
   z-suffix				\
   zdiff					\
   zgrep-f				\
diff --git a/tests/upper-suffix b/tests/upper-suffix
new file mode 100755
index 0000000..b48270e
--- /dev/null
+++ b/tests/upper-suffix
@@ -0,0 +1,33 @@
+#!/bin/sh
+# Ensure an upper-case user-specified suffix works as expected.
+# This test would have failed in gzip-1.18
+
+# Copyright 2017 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/>.
+# limit so don't run it by default.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ..
+
+# Use a suffix of "T":
+:|gzip > kT || framework_failure_
+
+fail=0
+gzip -d -S T kT > out 2> err || fail=1
+
+test -f k || fail=1
+compare /dev/null out || fail=1
+compare /dev/null err || fail=1
+
+Exit $fail
-- 
2.13.5