From 862984a870d375d380b71403191717991ccca97c Mon Sep 17 00:00:00 2001 From: Aaron Lindros Date: Fri, 8 Dec 2023 20:50:03 -0500 Subject: [PATCH] Fix encoding detection on platforms with nl_langinfo() (#2030) libarchive relies on its host program to call setlocale() on platforms with nl_langinfo() present in order to correctly detect the user selected locale. This ensures that bsdunzip does so. --- Makefile.am | 4 +++- unzip/bsdunzip.c | 8 ++++++++ unzip/test/CMakeLists.txt | 1 + unzip/test/test_I.c | 41 +++++++++++++++++++++++++++++++++++++++ unzip/test/test_I.zip.uu | 8 ++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 unzip/test/test_I.c create mode 100644 unzip/test/test_I.zip.uu diff --git a/Makefile.am b/Makefile.am index a6b199779..b5ccae02e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1522,7 +1522,8 @@ bsdunzip_test_SOURCES= \ unzip/test/test_version.c \ unzip/test/test_x.c \ unzip/test/test_Z1.c \ - unzip/test/test_P_encryption.c + unzip/test/test_P_encryption.c \ + unzip/test/test_I.c bsdunzip_test_CPPFLAGS= \ -I$(top_srcdir)/libarchive -I$(top_srcdir)/libarchive_fe \ @@ -1550,4 +1551,5 @@ bsdunzip_test_EXTRA_DIST= \ unzip/test/test_encrypted.zip.uu \ unzip/test/test_singlefile.zip.uu \ unzip/test/test_t_bad.zip.uu \ + unzip/test/test_I.zip.uu \ unzip/test/CMakeLists.txt diff --git a/unzip/bsdunzip.c b/unzip/bsdunzip.c index a47196519..af3fb14c3 100644 --- a/unzip/bsdunzip.c +++ b/unzip/bsdunzip.c @@ -57,6 +57,9 @@ #ifdef HAVE_FNMATCH_H #include #endif +#ifdef HAVE_LOCALE_H +#include +#endif #ifdef HAVE_STDARG_H #include #endif @@ -1210,6 +1213,11 @@ main(int argc, char *argv[]) lafe_setprogname(*argv, "bsdunzip"); +#if HAVE_SETLOCALE + if (setlocale(LC_ALL, "") == NULL) + lafe_warnc(0, "Failed to set default locale"); +#endif + if (isatty(STDOUT_FILENO)) tty = 1; diff --git a/unzip/test/CMakeLists.txt b/unzip/test/CMakeLists.txt index 75ca0ee82..f269c25f8 100644 --- a/unzip/test/CMakeLists.txt +++ b/unzip/test/CMakeLists.txt @@ -28,6 +28,7 @@ IF(ENABLE_UNZIP AND ENABLE_TEST) test_x.c test_Z1.c test_P_encryption.c + test_I.c ) # diff --git a/unzip/test/test_I.c b/unzip/test/test_I.c new file mode 100644 index 000000000..a6bad85a0 --- /dev/null +++ b/unzip/test/test_I.c @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Aaron Lindros + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "test.h" + +/* Test I arg - file name encoding */ +DEFINE_TEST(test_I) +{ + const char *reffile = "test_I.zip"; + int r; + + extract_reference_file(reffile); + r = systemf("%s -I UTF-8 %s >test.out 2>test.err", testprog, reffile); + assertEqualInt(0, r); + assertNonEmptyFile("test.out"); + assertEmptyFile("test.err"); + + assertTextFileContents("Hello, World!\n", "Γειά σου Κόσμε.txt"); +} diff --git a/unzip/test/test_I.zip.uu b/unzip/test/test_I.zip.uu new file mode 100644 index 000000000..8a1e7afa5 --- /dev/null +++ b/unzip/test/test_I.zip.uu @@ -0,0 +1,8 @@ +begin 644 test_I.zip +M4$L#!`H```@``&@)AU>$GNBT#@````X````>`!P`SI/.MZ+0.````#@```!X`&``` +M`````````*2!`````,Z3SK7.N