From nobody Wed Sep 20 13:26:11 2023 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RrK6q61Dgz4tfZM; Wed, 20 Sep 2023 13:26:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RrK6q5WMPz3S6H; Wed, 20 Sep 2023 13:26:11 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695216371; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=whO49MEuSobizP7h2Y7Z3fjcyQNdZh0g+X2XOe1bCzg=; b=iZnuah3D4ynsD8JRKMkRr3czuKqbfeyb7WdRLJJKuZ7ZY1IcsxUhz0s0TAFI5uXSec96nR eO4nveWGHffseTjywYyqy6WC3V9sR5jflzw6dC1xSdlYy/8pFJek+d1WHiRyHDJbzCz2Ms +5PJVX4O7dAXw9At2MVlthr0muVwBQywBTzqRKOrR1Ii6NEqalXZnmWZrnzBfQXIjfKfc4 3o4syAbHs0p7z38rICq9Vmpw/fknrgLg60eOY+Z23SsRafOZBZ71viRA1as9LzHTmn7QeS JhFFEa0EG/39u4zaEWwHYMYRFbZyE6eNxD7AFI5jAawu2xpyVbrjimksSey9zg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695216371; a=rsa-sha256; cv=none; b=OivUQECi3YHkT5GFkSVqGBl62vJGWc3l/sIwqXnPvECirdnLDcDiXL56mNHke0q4zQDjAl GtptXnG4yPQyAnV+KqMO/4qzN6viePy2Qa20yvRADPoKkD3p9oWpcrlpfaHw5lz7ybtZHd bN7ZUyqsNn0Yh/2hqBmdIW4fb7j7vrpahyaFOsx8MGDfG8R/EQbaCSy/Luk+aTxinxbZPO wrdhU47frnZCFKm47GQgeg4ioyZoyUwgnkhHhDMwQVvmIB8d11EeASO+VPdd1iPkVNpRlq MVGd6fxAASVx341qedPXdnepLvTApQINPTio1ZY//lOO8no32RMSYXnyz7nCHw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695216371; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=whO49MEuSobizP7h2Y7Z3fjcyQNdZh0g+X2XOe1bCzg=; b=EzMyihUd7ThpEVCtIe1xcfpKPflciBp6H+Qsdl7HuRr6CHeceKSAAxSnpTsxT07s0V6qr7 i5AeM7wZHH15nZKvyIOe8sHWuO9OaEaGu0DRpOfQ9MCrT0bTcbhoBIztwwT7ArbNtJ5uVp FXrXackdq70RLv9ZLw9pr9CPxNRPs1GpPr36qNwHSKaNP7wI4SDyDX53/+0D9U1qX0W6RE IBQUd6VE1qCwjDUHMhKkCXaD2vuyeCPb4uade3zCghIeYsHyorrTptQgsCpVC6kfcrbNPB EedelVZrTNFbDfItzCljx80Kx4sYLdJsvRZeHqPFu6ntZbuLJFI7naiP7yRa7g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RrK6q4TFKzx7j; Wed, 20 Sep 2023 13:26:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 38KDQBSP086503; Wed, 20 Sep 2023 13:26:11 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38KDQB6Q086500; Wed, 20 Sep 2023 13:26:11 GMT (envelope-from git) Date: Wed, 20 Sep 2023 13:26:11 GMT Message-Id: <202309201326.38KDQB6Q086500@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Yuri Pankov Subject: git: a4916232acd6 - stable/13 - libc: use separate collate objects for C, POSIX, and C.UTF-8 List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: yuripv X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: a4916232acd614d4d7d7226d678b427aacb9730e Auto-Submitted: auto-generated The branch stable/13 has been updated by yuripv: URL: https://cgit.FreeBSD.org/src/commit/?id=a4916232acd614d4d7d7226d678b427aacb9730e commit a4916232acd614d4d7d7226d678b427aacb9730e Author: Yuri Pankov AuthorDate: 2023-03-28 15:10:47 +0000 Commit: Yuri Pankov CommitDate: 2023-09-20 13:10:52 +0000 libc: use separate collate objects for C, POSIX, and C.UTF-8 Fix newlocale() overwriting the locale name in collate object when same instance was used for those locales, and querylocale() reporting unexpected value for LC_COLLATE_MASK. PR: 255646, 269375 Reviewed by: markj, bapt (previous version) Differential Revision: https://reviews.freebsd.org/D30146 (cherry picked from commit 269dea90d6eb32690730b0f6a17fb41170483104) --- lib/libc/locale/collate.c | 17 ++++- lib/libc/tests/locale/Makefile | 1 + lib/libc/tests/locale/newlocale_test.c | 111 +++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 3 deletions(-) diff --git a/lib/libc/locale/collate.c b/lib/libc/locale/collate.c index 3031d33dbb08..be995a5b963f 100644 --- a/lib/libc/locale/collate.c +++ b/lib/libc/locale/collate.c @@ -66,6 +66,14 @@ struct xlocale_collate __xlocale_C_collate = { {{0}, "C"}, 1, 0, 0, 0 }; +struct xlocale_collate __xlocale_POSIX_collate = { + {{0}, "POSIX"}, 1, 0, 0, 0 +}; + +struct xlocale_collate __xlocale_CUTF8_collate = { + {{0}, "C.UTF-8"}, 1, 0, 0, 0 +}; + static int __collate_load_tables_l(const char *encoding, struct xlocale_collate *table); @@ -82,10 +90,13 @@ destruct_collate(void *t) void * __collate_load(const char *encoding, __unused locale_t unused) { - if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0 || - strncmp(encoding, "C.", 2) == 0) { + if (strcmp(encoding, "C") == 0) return (&__xlocale_C_collate); - } + else if (strcmp(encoding, "POSIX") == 0) + return (&__xlocale_POSIX_collate); + else if (strcmp(encoding, "C.UTF-8") == 0) + return (&__xlocale_CUTF8_collate); + struct xlocale_collate *table = calloc(sizeof(struct xlocale_collate), 1); if (table == NULL) diff --git a/lib/libc/tests/locale/Makefile b/lib/libc/tests/locale/Makefile index 608dffc9768d..03e89571aacd 100644 --- a/lib/libc/tests/locale/Makefile +++ b/lib/libc/tests/locale/Makefile @@ -12,6 +12,7 @@ ATF_TESTS_C+= mbsnrtowcs_2_test ATF_TESTS_C+= mbsrtowcs_test ATF_TESTS_C+= mbstowcs_2_test ATF_TESTS_C+= mbtowc_2_test +ATF_TESTS_C+= newlocale_test ATF_TESTS_C+= towctrans_test ATF_TESTS_C+= wcrtomb_test ATF_TESTS_C+= wcsnrtombs_test diff --git a/lib/libc/tests/locale/newlocale_test.c b/lib/libc/tests/locale/newlocale_test.c new file mode 100644 index 000000000000..cb0ebb323a42 --- /dev/null +++ b/lib/libc/tests/locale/newlocale_test.c @@ -0,0 +1,111 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2023 Yuri Pankov + * + * 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. + * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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 + +#include + +#include + +struct { + int lpmask; + const char *lpname; +} lparts[] = { + { LC_COLLATE_MASK, "LC_COLLATE" }, + { LC_CTYPE_MASK, "LC_CTYPE" }, + { LC_MONETARY_MASK, "LC_MONETARY" }, + { LC_NUMERIC_MASK, "LC_NUMERIC" }, + { LC_TIME_MASK, "LC_TIME" }, + { LC_MESSAGES_MASK, "LC_MESSAGES" }, +}; + +static void +check_lparts(const char *expected) +{ + int i; + + for (i = 0; i < nitems(lparts); i++) { + const char *actual; + + actual = querylocale(lparts[i].lpmask, uselocale(NULL)); + ATF_CHECK_STREQ_MSG(expected, actual, "wrong value for %s", + lparts[i].lpname); + } +} + +static void +do_locale_switch(const char *loc1, const char *loc2) +{ + locale_t l1, l2; + + /* Create and use the first locale */ + l1 = newlocale(LC_ALL_MASK, loc1, NULL); + ATF_REQUIRE(l1 != NULL); + ATF_REQUIRE(uselocale(l1) != NULL); + check_lparts(loc1); + /* + * Create and use second locale, creation deliberately done only after + * the first locale check as newlocale() call would previously clobber + * the first locale contents. + */ + l2 = newlocale(LC_ALL_MASK, loc2, NULL); + ATF_REQUIRE(l2 != NULL); + ATF_REQUIRE(uselocale(l2) != NULL); + check_lparts(loc2); + /* Switch back to first locale */ + ATF_REQUIRE(uselocale(l1) != NULL); + check_lparts(loc1); + + freelocale(l1); + freelocale(l2); +} + +/* + * PR 255646, 269375: Check that newlocale()/uselocale() used to switch between + * C, POSIX, and C.UTF-8 locales (and only these) do not stomp on other locale + * contents (collate part specifically). + * The issue is cosmetic only as all three have empty collate parts, but we need + * to correctly report the one in use in any case. + */ + +ATF_TC_WITHOUT_HEAD(newlocale_c_posix_cu8_test); +ATF_TC_BODY(newlocale_c_posix_cu8_test, tc) +{ + do_locale_switch("C", "POSIX"); + do_locale_switch("C", "C.UTF-8"); + do_locale_switch("POSIX", "C"); + do_locale_switch("POSIX", "C.UTF-8"); + do_locale_switch("C.UTF-8", "C"); + do_locale_switch("C.UTF-8", "POSIX"); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, newlocale_c_posix_cu8_test); + + return (atf_no_error()); +}