From patchwork Tue Feb 8 09:40:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 50912 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A12D23858402 for ; Tue, 8 Feb 2022 09:41:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A12D23858402 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1644313272; bh=HOu3g3GBhZBoQwPNyqQXcrzbtJ5vgB3VuixatOvrE20=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Zc/xWOTYqLIQnA9yz2LcviblhSd/DZdWMccSkqyRQ2DagbixOoSl8N8nXSNAZvL/m BkvMQksM0xmFvAyjBctXBCXUCwci3hSK+fI2tA08SotheIGehfldkpxZHEpkBj76UW NSuc/aE20bi8MGi1SZbhs/4CEhK+jGYffrJ5eItM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cyan.elm.relay.mailchannels.net (cyan.elm.relay.mailchannels.net [23.83.212.47]) by sourceware.org (Postfix) with ESMTPS id A67943858D1E for ; Tue, 8 Feb 2022 09:40:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A67943858D1E X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 4AB2F12135E; Tue, 8 Feb 2022 09:40:46 +0000 (UTC) Received: from pdx1-sub0-mail-a305.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id E1EA51200B6; Tue, 8 Feb 2022 09:40:45 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a305.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.114.70.244 (trex/6.4.3); Tue, 08 Feb 2022 09:40:46 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Ruddy-Well-Made: 7058f03d02361a4d_1644313246197_2626018889 X-MC-Loop-Signature: 1644313246196:848824036 X-MC-Ingress-Time: 1644313246196 Received: from rhbox.redhat.com (unknown [1.186.224.172]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a305.dreamhost.com (Postfix) with ESMTPSA id 4JtJ0X00Sqz3R; Tue, 8 Feb 2022 01:40:43 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v3] setlocale: Fail if iconv module for charset is not present [BZ #27996] Date: Tue, 8 Feb 2022 15:10:34 +0530 Message-Id: <20220208094034.1075797-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20210630085642.2661589-1-siddhesh@sourceware.org> References: <20210630085642.2661589-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3494.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NEUTRAL, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" setlocale currently succeeds even if the requested locale uses a charset that does not have a converter module installed. Check for existence of the charset (either the one requested through the input name or the one needed by the selected locale file) and fail if it doesn't. The new test tst-invalid-charset verifes that loading test5 and test6 locales fail because both locales have charsets without a converter, viz. test5 and test6 respectively. Also, test6.c has been removed as it was unused. Signed-off-by: Siddhesh Poyarekar --- Tested on x86_64 Changes from v2: - Added GNU Toolchain Authors copyright - Rebased on latest master locale/findlocale.c | 78 +++++++++++++----- localedata/Makefile | 11 ++- localedata/tests/test6.c | 136 ------------------------------- localedata/tst-invalid-charset.c | 31 +++++++ 4 files changed, 96 insertions(+), 160 deletions(-) delete mode 100644 localedata/tests/test6.c create mode 100644 localedata/tst-invalid-charset.c diff --git a/locale/findlocale.c b/locale/findlocale.c index 64f687ea9d..e78664b0bf 100644 --- a/locale/findlocale.c +++ b/locale/findlocale.c @@ -1,4 +1,5 @@ /* Copyright (C) 1996-2022 Free Software Foundation, Inc. + Copyright The GNU Toolchain Authors. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -97,6 +98,30 @@ valid_locale_name (const char *name) return 1; } +/* Return true if we have gconv modules to transform between the INTERNAL + encoding and CODESET. */ +static bool +codeset_has_module (const char *codeset) +{ + struct __gconv_step *steps; + size_t nsteps; + + char *ccodeset = (char *) alloca (strlen (codeset) + 3); + strip (ccodeset, codeset); + + if (__gconv_find_transform ("INTERNAL", ccodeset, &steps, &nsteps, 0) + != __GCONV_OK) + return false; + __gconv_close_transform (steps, nsteps); + + if (__gconv_find_transform (ccodeset, "INTERNAL", &steps, &nsteps, 0) + != __GCONV_OK) + return false; + __gconv_close_transform (steps, nsteps); + + return true; +} + struct __locale_data * _nl_find_locale (const char *locale_path, size_t locale_path_len, int category, const char **name) @@ -199,6 +224,10 @@ _nl_find_locale (const char *locale_path, size_t locale_path_len, /* Memory allocate problem. */ return NULL; + /* The requested codeset does not have a converter, don't use it. */ + if (codeset != NULL && !codeset_has_module (codeset)) + return NULL; + /* If exactly this locale was already asked for we have an entry with the complete name. */ locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category], @@ -247,6 +276,33 @@ _nl_find_locale (const char *locale_path, size_t locale_path_len, return NULL; } + /* Get the codeset information from the locale file. */ + static const int codeset_idx[] = + { + [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET), + [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET), + [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET), + [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET), + [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET), + [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET), + [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET), + [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET), + [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET), + [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET), + [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET), + [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET) + }; + const struct __locale_data *data; + const char *locale_codeset; + + data = (const struct __locale_data *) locale_file->data; + locale_codeset = (const char *) data->values[codeset_idx[category]].string; + assert (locale_codeset != NULL); + + /* The locale codeset does not have a converter, don't use it. */ + if (locale_codeset[0] != '\0' && !codeset_has_module (locale_codeset)) + return NULL; + /* The LC_CTYPE category allows to check whether a locale is really usable. If the locale name contains a charset name and the charset name used in the locale (present in the LC_CTYPE data) is @@ -255,31 +311,9 @@ _nl_find_locale (const char *locale_path, size_t locale_path_len, in the locale name. */ if (codeset != NULL) { - /* Get the codeset information from the locale file. */ - static const int codeset_idx[] = - { - [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET), - [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET), - [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET), - [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET), - [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET), - [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET), - [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET), - [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET), - [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET), - [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET), - [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET), - [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET) - }; - const struct __locale_data *data; - const char *locale_codeset; char *clocale_codeset; char *ccodeset; - data = (const struct __locale_data *) locale_file->data; - locale_codeset = - (const char *) data->values[codeset_idx[category]].string; - assert (locale_codeset != NULL); /* Note the length of the allocated memory: +3 for up to two slashes and the NUL byte. */ clocale_codeset = (char *) alloca (strlen (locale_codeset) + 3); diff --git a/localedata/Makefile b/localedata/Makefile index 9ae2e5c161..8504303654 100644 --- a/localedata/Makefile +++ b/localedata/Makefile @@ -125,11 +125,13 @@ test-input := \ test-input-data = $(addsuffix .in, $(test-input)) test-output := $(foreach s, .out .xout, \ $(addsuffix $s, $(basename $(test-input)))) +# Note that tst-invalid-charset depends on test5 and test6 being locales that +# do not have valid charset converters. ld-test-names := test1 test2 test3 test4 test5 test6 test7 ld-test-srcs := $(addprefix tests/,$(addsuffix .cm,$(ld-test-names)) \ $(addsuffix .def,$(ld-test-names)) \ $(addsuffix .ds,test5 test6) \ - test6.c trans.def) + trans.def) fmon-tests = n01y12 n02n40 n10y31 n11y41 n12y11 n20n32 n30y20 n41n00 \ y01y10 y02n22 y22n42 y30y21 y32n31 y40y00 y42n21 @@ -163,6 +165,7 @@ tests = \ tst-c-utf8-consistency \ tst-digits \ tst-iconv-math-trans \ + tst-invalid-charset \ tst-leaks \ tst-mbswcs1 \ tst-mbswcs2 \ @@ -423,7 +426,10 @@ $(objpfx)tst-langinfo-setlocale-static.out: tst-langinfo.sh \ '$(run-program-env)' '$(test-program-cmd-after-env)' > $@; \ $(evaluate-test) +# These tests depend on tst-locale because they use the locales compiled by +# that test. $(objpfx)tst-digits.out: $(objpfx)tst-locale.out +$(objpfx)tst-invalid-charset.out: $(objpfx)tst-locale.out $(objpfx)tst-mbswcs6.out: $(addprefix $(objpfx),$(CTYPE_FILES)) endif @@ -484,7 +490,8 @@ $(objpfx)mtrace-tst-leaks.out: $(objpfx)tst-leaks.out $(common-objpfx)malloc/mtrace $(objpfx)tst-leaks.mtrace > $@; \ $(evaluate-test) -bug-setlocale1-ENV-only = LOCPATH=$(objpfx) LC_CTYPE=de_DE.UTF-8 +bug-setlocale1-ENV-only = GCONV_PATH=$(common-objpfx)iconvdata \ + LOCPATH=$(objpfx) LC_CTYPE=de_DE.UTF-8 bug-setlocale1-static-ENV-only = $(bug-setlocale1-ENV-only) $(objdir)/iconvdata/gconv-modules: diff --git a/localedata/tests/test6.c b/localedata/tests/test6.c deleted file mode 100644 index fc841454f0..0000000000 --- a/localedata/tests/test6.c +++ /dev/null @@ -1,136 +0,0 @@ -/* Test program for character classes and mappings. - Copyright (C) 1999-2022 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include -#include - - -int -main (void) -{ - const char lower[] = "abcdefghijklmnopqrstuvwxyz"; - const char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -#define LEN (sizeof (upper) - 1) - const wchar_t wlower[] = L"abcdefghijklmnopqrstuvwxyz"; - const wchar_t wupper[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - int i; - int result = 0; - - setlocale (LC_ALL, "test6"); - - for (i = 0; i < LEN; ++i) - { - /* Test basic table handling (basic == not more than 256 characters). - The charmaps swaps the normal lower-upper case meaning of the - ASCII characters used in the source code while the Unicode mapping - in the repertoire map has the normal correspondents. This test - shows the independence of the tables for `char' and `wchar_t' - characters. */ - - if (islower (lower[i])) - { - printf ("islower ('%c') false\n", lower[i]); - result = 1; - } - if (! isupper (lower[i])) - { - printf ("isupper ('%c') false\n", lower[i]); - result = 1; - } - - if (! islower (upper[i])) - { - printf ("islower ('%c') false\n", upper[i]); - result = 1; - } - if (isupper (upper[i])) - { - printf ("isupper ('%c') false\n", upper[i]); - result = 1; - } - - if (toupper (lower[i]) != lower[i]) - { - printf ("toupper ('%c') false\n", lower[i]); - result = 1; - } - if (tolower (lower[i]) != upper[i]) - { - printf ("tolower ('%c') false\n", lower[i]); - result = 1; - } - - if (tolower (upper[i]) != upper[i]) - { - printf ("tolower ('%c') false\n", upper[i]); - result = 1; - } - if (toupper (upper[i]) != lower[i]) - { - printf ("toupper ('%c') false\n", upper[i]); - result = 1; - } - - if (iswlower (wupper[i])) - { - printf ("iswlower (L'%c') false\n", upper[i]); - result = 1; - } - if (! iswupper (wupper[i])) - { - printf ("iswupper (L'%c') false\n", upper[i]); - result = 1; - } - - if (iswupper (wlower[i])) - { - printf ("iswupper (L'%c') false\n", lower[i]); - result = 1; - } - if (! iswlower (wlower[i])) - { - printf ("iswlower (L'%c') false\n", lower[i]); - result = 1; - } - - if (towupper (wlower[i]) != wupper[i]) - { - printf ("towupper ('%c') false\n", lower[i]); - result = 1; - } - if (towlower (wlower[i]) != wlower[i]) - { - printf ("towlower ('%c') false\n", lower[i]); - result = 1; - } - - if (towlower (wupper[i]) != wlower[i]) - { - printf ("towlower ('%c') false\n", upper[i]); - result = 1; - } - if (towupper (wupper[i]) != wupper[i]) - { - printf ("towupper ('%c') false\n", upper[i]); - result = 1; - } - } - - return result; -} diff --git a/localedata/tst-invalid-charset.c b/localedata/tst-invalid-charset.c new file mode 100644 index 0000000000..067bb689da --- /dev/null +++ b/localedata/tst-invalid-charset.c @@ -0,0 +1,31 @@ +/* Test program to verify that setlocale fails for charsets that do not have a + converter. + Copyright The GNU Toolchain Authors. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + + +int +main (void) +{ + /* Fail if setlocale succeeds for any of these locales. */ + return (setlocale (LC_ALL, "test5") != NULL + || setlocale (LC_ALL, "test6") != NULL); +}