From patchwork Sat Apr 4 18:41:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos O'Donell X-Patchwork-Id: 38750 Return-Path: X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by sourceware.org (Postfix) with ESMTP id 5C288385DC02 for ; Sat, 4 Apr 2020 18:41:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 5C288385DC02 Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-443-WPHn6wBlOBWS5rAiE_zVvA-1; Sat, 04 Apr 2020 14:41:17 -0400 X-MC-Unique: WPHn6wBlOBWS5rAiE_zVvA-1 Received: by mail-qt1-f197.google.com with SMTP id x10so9981668qts.14 for ; Sat, 04 Apr 2020 11:41:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=Y768mVhnl1bO/1WBby3Dvp8pmb3upIoAsngnxopdifk=; b=BNojWAxZ/VJgKMuFKzwC2STVDE/T8spV+Xbmi7mnUIO2O2EwhN0AuqHhfo/AuDRXXG CgRohNTwaxlTZq6Ye0Yn5O8Lzg3GYMaroaJTWzZyOSrkhyszTCmzTndjDSluphAiywO9 WbDLNnW4tUnSaP2uSGdI/5yNwaYh+C7eSo9pkEQWGAoreTgaW6ou0eXwAgelNbmyZ5qe qh+KCJw0DNJYodiVwZUy+G7Kqil3SI59ecQvCUjW+BZm7Gw9b8GhwViViH0g+SuZvhDA TAz2xBNl3PbWTBkvHSHYMKzRdlDXZRHvXqa2lMCftN8H/Y4nPg1H4lBaFzf9hPhLWQYj 2LNw== X-Gm-Message-State: AGi0Puavujd9nHt2Cw9+pVoP5rcDkb5PYVtoYgUkiKyPqJv019JGijPE qNNx/6ObwOTFv/S0CneSSfPFbOUNLMqEVkz9lyPEmqcSQTa5/ecT6brqd7j1Ng9h4LAJpAclh+G ilevah7oDYpQgBFeLU/I6 X-Received: by 2002:a05:620a:11b0:: with SMTP id c16mr14850086qkk.309.1586025676413; Sat, 04 Apr 2020 11:41:16 -0700 (PDT) X-Google-Smtp-Source: APiQypJ5jEV590tJuDTA76Jv8KTYoZpylYwN9mV9rIeiq1pwHAY4jScmQ02luC1cE9fe1UhapaLVlg== X-Received: by 2002:a05:620a:11b0:: with SMTP id c16mr14850066qkk.309.1586025676091; Sat, 04 Apr 2020 11:41:16 -0700 (PDT) Received: from [192.168.1.4] (198-84-170-103.cpe.teksavvy.com. [198.84.170.103]) by smtp.gmail.com with ESMTPSA id 27sm7944963qkv.7.2020.04.04.11.41.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 04 Apr 2020 11:41:15 -0700 (PDT) Subject: [PATCH v2] Reset converter state after second wchar_t output (Bug 25734) To: Tom Honermann , Andreas Schwab , libc-alpha , Rafal Luzynski References: <691be68d-2969-1076-75a9-4185c5de5f69@honermann.net> <2adea89a-2dca-caf0-db87-08dd1aefe670@honermann.net> From: Carlos O'Donell Organization: Red Hat Message-ID: <6c3ea93f-4031-6ec1-e19d-cb0d79158b82@redhat.com> Date: Sat, 4 Apr 2020 14:41:14 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <2adea89a-2dca-caf0-db87-08dd1aefe670@honermann.net> Content-Language: en-US X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-21.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Sat, 04 Apr 2020 18:41:23 -0000 On 4/1/20 1:34 PM, Tom Honermann wrote: > Though my initial tests didn't reveal a problem, this call to > xsetlocale() is now failing for me in a fresh environment. The > reason is that a locale for "zh_HK.BIG5-HKSCS" is not generated. For > other work I've been doing, I had generated that locale, but the > glibc testsuite doesn't automatically generate it today. Correct, I've added that dep in now. > The following patch addresses this. Note that the prior dependence > on BIG5HKSCS.so was not sufficient (and is no longer needed) since > this test really depends on the locale (not just the converter) being > available. The requirement for the locale may be sufficient reason > to move this test under wcsmbs. The iconvdata Makefile currently > only requires generation of the de_DE.UTF-8 locale while the wcsmbs > Makefile already requires many locales. And since this test exercises > mbrtowc specifically, that might be a more appropriate place for it > anyway. The test depends on and needs both. I think this test should stay in iconvdata/ because we're really exercising the converter. > diff --git a/iconvdata/Makefile b/iconvdata/Makefile > index 71ddef09db..3c0c5807b7 100644 > --- a/iconvdata/Makefile > +++ b/iconvdata/Makefile > @@ -278,11 +278,12 @@ endif > include ../Rules > > ifeq ($(run-built-tests),yes) > -LOCALES := de_DE.UTF-8 > +LOCALES := de_DE.UTF-8 zh_HK.BIG5-HKSCS Yup. > include ../gen-locales.mk > > $(objpfx)bug-iconv6.out: $(gen-locales) > $(objpfx)tst-iconv7.out: $(gen-locales) > +$(objpfx)tst-iconv-big5-hkscs-to-2ucs4.out: $(gen-locales) > endif > > # Set libof-* for each routine. > @@ -340,7 +341,3 @@ tst-tables-clean: > > $(objpfx)gconv-modules: gconv-modules > cat $(sysdeps-gconv-modules) $^ > $@ > - > -# Test requires BIG5HKSCS. > -$(objpfx)tst-iconv-big5-hkscs-to-2ucs4.out: $(objpfx)gconv-modules \ > - $(addprefix $(objpfx),BIG5HKSCS.so) v2 - Add test dependency on locale. - Make test use generic converter dependency (builds all converters) - Move Rules inclusion to after locale generation to match localedata/ order. No regressions on x86_64 and i686. I'll push this next week unless someone objects. There is still the issue of *what* to return from the converter when processing the queue byte, but that will take longer to reach consensus than just fixing the current processing hang. 8< --- 8< --- 8< >From fccb8e5dfd80933c881a983353998e79cff9559c Mon Sep 17 00:00:00 2001 From: Carlos O'Donell Date: Fri, 27 Mar 2020 17:03:36 -0400 Subject: [PATCH] Reset converter state after second wchar_t output (Bug 25734) An input BIG5-HKSCS character may be converted into at most 2 wchar_t characters. After outputting the second whcar_t character (which was saved in the converter state) we must reset the state. If we fail to reset the state we will be stuck continually copying that character to the output even if we have further input to consider. We add a new test case that covers the 4 BIG5-HKSCS characters that may become 2 wchar_t characters. Reviewed-by: Tom Honermann --- iconvdata/Makefile | 17 ++- iconvdata/big5hkscs.c | 3 + iconvdata/tst-iconv-big5-hkscs-to-2ucs4.c | 160 ++++++++++++++++++++++ 3 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 iconvdata/tst-iconv-big5-hkscs-to-2ucs4.c diff --git a/iconvdata/Makefile b/iconvdata/Makefile index c83962f351..4ec2741cdc 100644 --- a/iconvdata/Makefile +++ b/iconvdata/Makefile @@ -73,7 +73,7 @@ modules.so := $(addsuffix .so, $(modules)) ifeq (yes,$(build-shared)) tests = bug-iconv1 bug-iconv2 tst-loading tst-e2big tst-iconv4 bug-iconv4 \ tst-iconv6 bug-iconv5 bug-iconv6 tst-iconv7 bug-iconv8 bug-iconv9 \ - bug-iconv10 bug-iconv11 bug-iconv12 + bug-iconv10 bug-iconv11 bug-iconv12 tst-iconv-big5-hkscs-to-2ucs4 ifeq ($(have-thread-library),yes) tests += bug-iconv3 endif @@ -275,16 +275,21 @@ endif endif endif -include ../Rules - ifeq ($(run-built-tests),yes) -LOCALES := de_DE.UTF-8 +LOCALES := \ + de_DE.UTF-8 \ + zh_HK.BIG5-HKSCS \ + $(NULL) + include ../gen-locales.mk $(objpfx)bug-iconv6.out: $(gen-locales) $(objpfx)tst-iconv7.out: $(gen-locales) +$(objpfx)tst-iconv-big5-hkscs-to-2ucs4.out: $(gen-locales) endif +include ../Rules + # Set libof-* for each routine. cpp-srcs-left := $(modules) $(generated-modules) $(libJIS-routines) \ $(libKSC-routines) $(libGB-routines) $(libCNS-routines) \ @@ -340,3 +345,7 @@ tst-tables-clean: $(objpfx)gconv-modules: gconv-modules cat $(sysdeps-gconv-modules) $^ > $@ + +# Test requires BIG5HKSCS. +$(objpfx)tst-iconv-big5-hkscs-to-2ucs4.out: $(objpfx)gconv-modules \ + $(addprefix $(objpfx),$(modules.so)) diff --git a/iconvdata/big5hkscs.c b/iconvdata/big5hkscs.c index 01fcfeba76..ef325119b1 100644 --- a/iconvdata/big5hkscs.c +++ b/iconvdata/big5hkscs.c @@ -17895,6 +17895,9 @@ static struct else \ ++inptr; \ } \ + else \ + /* Clear the queue and proceed to output the saved character. */ \ + *statep = 0; \ \ put32 (outptr, ch); \ outptr += 4; \ diff --git a/iconvdata/tst-iconv-big5-hkscs-to-2ucs4.c b/iconvdata/tst-iconv-big5-hkscs-to-2ucs4.c new file mode 100644 index 0000000000..8389adebf2 --- /dev/null +++ b/iconvdata/tst-iconv-big5-hkscs-to-2ucs4.c @@ -0,0 +1,160 @@ +/* Verify the BIG5HKSCS outputs that generate 2 wchar_t's (Bug 25734). + Copyright (C) 2020 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 +#include +#include +#include + +/* A few BIG5-HKSCS characters map in two unicode code points. + They are: + /x88/x62 => + /x88/x64 => + /x88/xa3 => + /x88/xa5 => + Each of these is special cased in iconvdata/big5hkscs.c. + This test ensures that we correctly reset the shift state after + outputting any of these characters. We do this by converting + each them followed by converting an ASCII character. If we fail + to reset the shift state (bug 25734) then we'll see the last + character in the queue output again. */ + +/* Each test has name, input bytes, and expected wide character + output. */ +struct testdata { + const char *name; + const char input[3]; + wchar_t expected[3]; +}; + +/* In BIG5-HKSCS (2008) there are 4 characters that generate multiple + wide characters. */ +struct testdata tests[4] = { + /* X => X */ + { "", "\x88\x62\x58", { 0x00CA, 0x0304, 0x0058 } }, + /* X => X */ + { "", "\x88\x64\x58", { 0x00CA, 0x030C, 0x0058 } }, + /* X => X */ + { "", "\x88\xa3\x58", { 0x00EA, 0x0304, 0x0058 } }, + /* X => X */ + { "", "\x88\xa5\x58", { 0x00EA, 0x030C, 0x0058 } } +}; + +/* Each test is of the form: + - Translate first code sequence (two bytes) + - Translate second (zero bytes) + - Translate the third (one byte). */ +static int +check_conversion (struct testdata test) +{ + int err = 0; + wchar_t wc; + mbstate_t st; + size_t ret; + const char *mbs = test.input; + int consumed = 0; + /* Input is always 3 bytes long. */ + int inlen = 3; + + memset (&st, 0, sizeof (st)); + /* First conversion: Consumes first 2 bytes. */ + ret = mbrtowc (&wc, mbs, inlen - consumed, &st); + if (ret != 2) + { + printf ("error: First conversion consumed only %zd bytes.\n", ret); + err++; + } + /* Advance the two consumed bytes. */ + mbs += ret; + consumed += ret; + if (wc != test.expected[0]) + { + printf ("error: Result of first conversion was wrong.\n"); + err++; + } + /* Second conversion: Consumes 0 bytes. */ + ret = mbrtowc (&wc, mbs, inlen - consumed, &st); + if (ret != 0) + { + printf ("error: Second conversion consumed only %zd bytes.\n", ret); + err++; + } + /* Advance the zero consumed bytes. */ + mbs += ret; + consumed += ret; + if (wc != test.expected[1]) + { + printf ("error: Result of second conversion was wrong.\n"); + err++; + } + /* After the second conversion the state of the converter should be + in the initial state. It is in the initial state because the two + input BIG5-HKSCS bytes have been consumed and the 2 wchar_t's have + been output. */ + if (mbsinit (&st) == 0) + { + printf ("error: Converter not in initial state.\n"); + err++; + } + /* Third conversion: Consumes 1 byte (it's an ASCII character). */ + ret = mbrtowc (&wc, mbs, inlen - consumed, &st); + if (ret != 1) + { + printf ("error: Third conversion consumed only %zd bytes.\n", ret); + err++; + } + /* Advance the one byte. */ + mbs += ret; + consumed += ret; + if (wc != test.expected[2]) + { + printf ("error: Result of third conversion was wrong.\n"); + err++; + } + /* Return 0 if we saw no errors. */ + return err; +} + +static int +do_test (void) +{ + int err = 0; + int ret; + /* Testing BIG5-HKSCS. */ + xsetlocale (LC_ALL, "zh_HK.BIG5-HKSCS"); + + /* Run all the special conversions. */ + for (int i = 0; i < (sizeof (tests) / sizeof (struct testdata)); i++) + { + printf ("Running test for %s\n", tests[i].name); + ret = check_conversion (tests[i]); + if (ret > 0) + printf ("Test %s failed.\n", tests[i].name); + err += ret; + } + + /* Fail if any conversion had an error. */ + if (err > 0) + FAIL_EXIT1 ("One or more conversions failed."); + + return 0; +} + +#include