From patchwork Mon Jun 19 13:11:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ahelenia_Ziemia=C5=84ska?= X-Patchwork-Id: 1796604 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=AucA/iLL; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Ql9F21S1Bz20Z9 for ; Mon, 19 Jun 2023 23:13:26 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8BB4F3857013 for ; Mon, 19 Jun 2023 13:13:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8BB4F3857013 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687180403; bh=/WJYX+aX+4/RMsHZScsJc9gub2OYAkimDpUtJCMD5Fc=; h=Resent-From:Resent-Date:Resent-To:Date:Cc:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=AucA/iLLVfIZ6FAdA/Uo3dJlzycvz7xM0+eQws8OGNyxmo/6WJduoFkAitE53tue2 4bcYdvZXdcO7l7kP5cYRzzLhXh6iJ1Mq6WCEh5VSYMmrs4Y25N7aIEAg5NdmGGO1FZ 1OSzpIVoX26GujpMqsyI0dX2WM3F6u+Iq4Couhm8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from tarta.nabijaczleweli.xyz (unknown [139.28.40.42]) by sourceware.org (Postfix) with ESMTP id 736E53858D3C for ; Mon, 19 Jun 2023 13:13:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 736E53858D3C Received: from tarta.nabijaczleweli.xyz (unknown [192.168.1.250]) by tarta.nabijaczleweli.xyz (Postfix) with ESMTPSA id CB496DB8 for ; Mon, 19 Jun 2023 15:13:07 +0200 (CEST) Resent-From: =?utf-8?b?0L3QsNCx?= Resent-Date: Mon, 19 Jun 2023 15:13:06 +0200 Resent-Message-ID: Resent-To: libc-alpha@sourceware.org Date: Mon, 19 Jun 2023 15:11:10 +0200 Cc: Carlos O'Donell , Adhemerval Zanella Netto , Paul Eggert , libc-alpha@sourceware.or Subject: [PATCH v8 1/3] posix: regcomp(): clear RE_DOT_NOT_NULL Message-ID: <2a40f1eeddfd3a4d5602d564c6faf9cc5569f689.1687180188.git.nabijaczleweli@nabijaczleweli.xyz> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20230517 X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_INFOUSMEBIZ, MISSING_HEADERS, PDS_RDNS_DYNAMIC_FP, RDNS_DYNAMIC, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: =?utf-8?b?0L3QsNCxIHZpYSBMaWJjLWFscGhh?= From: =?utf-8?q?Ahelenia_Ziemia=C5=84ska?= Reply-To: =?utf-8?b?0L3QsNCx?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" The POSIX API always stops at first NUL so there's no change for that. The BSD REG_STARTEND API, with its explicit range, can include NULs within that range, and those NULs are matched with . and [^]. Heretofor, for a string of "a\0c", glibc would match "[^q]c", but not ".c". This is both inconsistent and nonconformant to BSD REG_STARTEND. With this patch, they're identical like you'd expect, and the tst-reg-startend.c: ..c: a^@c: no match$ failure is removed. Another approach would be to remove it from _RE_SYNTAX_POSIX_COMMON, but it's unclear to me what the custody chain is like for that and what other regex APIs glibc offers that could be affected by this. --- I hereby disclaim all copyright interest in this changeset. posix/regcomp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posix/regcomp.c b/posix/regcomp.c index 12650714c0..a928ef6c2d 100644 --- a/posix/regcomp.c +++ b/posix/regcomp.c @@ -462,7 +462,7 @@ regcomp (regex_t *__restrict preg, const char *__restrict pattern, int cflags) { reg_errcode_t ret; reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED - : RE_SYNTAX_POSIX_BASIC); + : RE_SYNTAX_POSIX_BASIC) & ~RE_DOT_NOT_NULL; preg->buffer = NULL; preg->allocated = 0; From patchwork Mon Jun 19 13:11:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ahelenia_Ziemia=C5=84ska?= X-Patchwork-Id: 1796602 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=uA8C6kFb; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Ql9F014FGz20Wk for ; Mon, 19 Jun 2023 23:13:24 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 27627385734F for ; Mon, 19 Jun 2023 13:13:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 27627385734F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687180402; bh=4tWUttrm2W5G1BZNR2gktY9dlKr3+sCPtvt+sk7o4xI=; h=Resent-From:Resent-Date:Resent-To:Date:Cc:Subject:References: In-Reply-To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=uA8C6kFbeT2wo+v5vEbYvb9YCkAKtpPNbBYLl6a5rjw4ywSlWRkarBBIXV6k/rrh/ 26gDoUJS2d3NfWonpqdBDlvvzAvxkqbDNU1Ku2NxB434qU13LoaKCHETfJSo7ALGfM G6DljRwvyAzp/ib6YeHMBX253BxE8oDsgOjtPRd8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from tarta.nabijaczleweli.xyz (unknown [139.28.40.42]) by sourceware.org (Postfix) with ESMTP id 46C273858D32 for ; Mon, 19 Jun 2023 13:13:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 46C273858D32 Received: from tarta.nabijaczleweli.xyz (unknown [192.168.1.250]) by tarta.nabijaczleweli.xyz (Postfix) with ESMTPSA id A3476950 for ; Mon, 19 Jun 2023 15:13:06 +0200 (CEST) Resent-From: =?utf-8?b?0L3QsNCx?= Resent-Date: Mon, 19 Jun 2023 15:13:05 +0200 Resent-Message-ID: Resent-To: libc-alpha@sourceware.org Date: Mon, 19 Jun 2023 15:11:20 +0200 Cc: Carlos O'Donell , Adhemerval Zanella Netto , Paul Eggert , libc-alpha@sourceware.or Subject: [PATCH v8 2/3] posix: regexec(): fix REG_STARTEND, pmatch->rm_so != 0 w/^ anchor Message-ID: References: <2a40f1eeddfd3a4d5602d564c6faf9cc5569f689.1687180188.git.nabijaczleweli@nabijaczleweli.xyz> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <2a40f1eeddfd3a4d5602d564c6faf9cc5569f689.1687180188.git.nabijaczleweli@nabijaczleweli.xyz> User-Agent: NeoMutt/20230517 X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_INFOUSMEBIZ, MISSING_HEADERS, PDS_RDNS_DYNAMIC_FP, RDNS_DYNAMIC, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: =?utf-8?b?0L3QsNCxIHZpYSBMaWJjLWFscGhh?= From: =?utf-8?q?Ahelenia_Ziemia=C5=84ska?= Reply-To: =?utf-8?b?0L3QsNCx?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" re_search_internal () starts with /* If initial states with non-begbuf contexts have no elements, the regex must be anchored. If preg->newline_anchor is set, we'll never use init_state_nl, so do not check it. */ if (dfa->init_state->nodes.nelem == 0 && dfa->init_state_word->nodes.nelem == 0 && (dfa->init_state_nl->nodes.nelem == 0 || !preg->newline_anchor)) { if (start != 0 && last_start != 0) return REG_NOMATCH; start = last_start = 0; } and heretofor start and last_start (for example when "abc", {1, 2}, so matching just the "b") were != 0, and the return was taken for a "^b" regex, which is erroneous. Fix this by giving re_search_internal (string+rm_so, start=0), then fixing up the returned matches in an after-pass. This brings us to compatibility with the BSD spec and implementations. --- I hereby disclaim all copyright interest in this changeset. posix/regexec.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/posix/regexec.c b/posix/regexec.c index bd0cd412d0..2ef868e1f6 100644 --- a/posix/regexec.c +++ b/posix/regexec.c @@ -187,38 +187,53 @@ static reg_errcode_t extend_buffers (re_match_context_t *mctx, int min_len); string; if REG_NOTEOL is set, then $ does not match at the end. Return 0 if a match is found, REG_NOMATCH if not, REG_BADPAT if - EFLAGS is invalid. */ + EFLAGS is invalid. + + If REG_STARTEND, the bounds are + [STRING + PMATCH->rm_so, STRING + PMATCH->rm_eo) + instead of the usual + [STRING, STRING + strlen(STRING)), + but returned matches are still referenced to STRING, + and matching is unaffected (i.e. "abc", {1, 2} matches regex "^b$"). + re_search_internal () has a built-in assumption of + (start != 0) <=> (^ doesn't match), so give it a truncated view + and fix up the matches afterward. */ int regexec (const regex_t *__restrict preg, const char *__restrict string, size_t nmatch, regmatch_t pmatch[_REGEX_NELTS (nmatch)], int eflags) { reg_errcode_t err; - Idx start, length; + Idx startoff = 0, length; re_dfa_t *dfa = preg->buffer; + size_t i = 0; if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND)) return REG_BADPAT; if (eflags & REG_STARTEND) { - start = pmatch[0].rm_so; - length = pmatch[0].rm_eo; + startoff = pmatch[0].rm_so; + string += startoff; + length = pmatch[0].rm_eo - startoff; } else - { - start = 0; - length = strlen (string); - } + length = strlen (string); lock_lock (dfa->lock); if (preg->no_sub) - err = re_search_internal (preg, string, length, start, length, - length, 0, NULL, eflags); - else - err = re_search_internal (preg, string, length, start, length, - length, nmatch, pmatch, eflags); + nmatch = 0; + err = re_search_internal (preg, string, length, 0, length, + length, nmatch, pmatch, eflags); lock_unlock (dfa->lock); + + if (err == REG_NOERROR && startoff) + for (i = 0; i < nmatch; ++i) + if (pmatch[i].rm_so != -1) + { + pmatch[i].rm_so += startoff; + pmatch[i].rm_eo += startoff; + } return err != REG_NOERROR; } From patchwork Mon Jun 19 13:11:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ahelenia_Ziemia=C5=84ska?= X-Patchwork-Id: 1796603 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=SoQJ0X2j; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Ql9F12zvpz20Wk for ; Mon, 19 Jun 2023 23:13:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C67CB3857736 for ; Mon, 19 Jun 2023 13:13:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C67CB3857736 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687180402; bh=I5dpagEK0iDJzzwo4X3nn0rjQCitkab5+nADDoT5g1U=; h=Resent-From:Resent-Date:Resent-To:Date:Cc:Subject:References: In-Reply-To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=SoQJ0X2jixczpDem8Mvh+Pfh/Hb51yAkQE7wPNdLQRajsfCMeazeiVq9MJ+gZaDFX 223HK7joNFtL/mQ+7k0zJFeuLyBlD2ldGQRryOYOk6C5HOBZ8IvE4IrzUj/mwqp6sa r/lWowFXu3CKWVsibpvclvhsiFi0tu5lB+uxWVW0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from tarta.nabijaczleweli.xyz (unknown [139.28.40.42]) by sourceware.org (Postfix) with ESMTP id 2561A3858D28 for ; Mon, 19 Jun 2023 13:13:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2561A3858D28 Received: from tarta.nabijaczleweli.xyz (unknown [192.168.1.250]) by tarta.nabijaczleweli.xyz (Postfix) with ESMTPSA id 7A64DDB6 for ; Mon, 19 Jun 2023 15:13:05 +0200 (CEST) Resent-From: =?utf-8?b?0L3QsNCx?= Resent-Date: Mon, 19 Jun 2023 15:13:04 +0200 Resent-Message-ID: Resent-To: libc-alpha@sourceware.org Date: Mon, 19 Jun 2023 15:11:35 +0200 Cc: Carlos O'Donell , Adhemerval Zanella Netto , Paul Eggert , libc-alpha@sourceware.or Subject: [PATCH v8 3/3] posix: add test for REG_STARTEND Message-ID: <6bff9d695a70da980670184dcf90c15a56991093.1687180188.git.nabijaczleweli@nabijaczleweli.xyz> References: <2a40f1eeddfd3a4d5602d564c6faf9cc5569f689.1687180188.git.nabijaczleweli@nabijaczleweli.xyz> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <2a40f1eeddfd3a4d5602d564c6faf9cc5569f689.1687180188.git.nabijaczleweli@nabijaczleweli.xyz> User-Agent: NeoMutt/20230517 X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_INFOUSMEBIZ, MISSING_HEADERS, PDS_RDNS_DYNAMIC_FP, RDNS_DYNAMIC, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: =?utf-8?b?0L3QsNCxIHZpYSBMaWJjLWFscGhh?= From: =?utf-8?q?Ahelenia_Ziemia=C5=84ska?= Reply-To: =?utf-8?b?0L3QsNCx?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" This test passes on NetBSD, the illumos gate, musl with https://www.openwall.com/lists/musl/2023/05/14/1, and now glibc. It's nothing revolutionary and the behaviour it tests is largely guaranteed by the 4.4BSD-Lite manual; nevertheless, it used to fail with tst-reg-startend.c: ^a: a^@c: no match$ tst-reg-startend.c: ^a: a^@c: wanted {1, 2}, got {1, 4}$ tst-reg-startend.c: ^a: abc: no match$ tst-reg-startend.c: ^a: abc: wanted {1, 2}, got {1, 4}$ tst-reg-startend.c: ^a.c$: a^@c: no match$ tst-reg-startend.c: ^a.c$: abc: no match$ tst-reg-startend.c: ^a.*c$: a^@c: no match$ tst-reg-startend.c: ^a.*c$: abc: no match$ tst-reg-startend.c: ^a[^c]c$: a^@c: no match$ tst-reg-startend.c: ^a[^c]c$: abc: no match$ tst-reg-startend.c: ^a..: a^@c: no match$ tst-reg-startend.c: ^a..: abc: no match$ tst-reg-startend.c: ..c: a^@c: no match$ Signed-off-by: Ahelenia Ziemiańska --- posix/Makefile | 1 + posix/tst-reg-startend.c | 142 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 posix/tst-reg-startend.c diff --git a/posix/Makefile b/posix/Makefile index ad43cbdec6..10b64206f1 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -296,6 +296,7 @@ tests := \ tst-posix_spawn-setsid \ tst-preadwrite \ tst-preadwrite64 \ + tst-reg-startend \ tst-regcomp-truncated \ tst-regex \ tst-regex2 \ diff --git a/posix/tst-reg-startend.c b/posix/tst-reg-startend.c new file mode 100644 index 0000000000..854d430676 --- /dev/null +++ b/posix/tst-reg-startend.c @@ -0,0 +1,142 @@ +/* This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. */ + + +#include +#include +#include +#include +#include +#include +#include + + +static const regmatch_t bound = {1, 4}; + + +struct reg_res { + const char *regex; + regmatch_t result; +}; +static const struct reg_res reg_res_ac[] = { + {"^a", {1, 2}}, + {"c$", {3, 4}}, + {"^a.c$", {1, 4}}, + {"^a.*c$", {1, 4}}, + {"^a[^c]c$", {1, 4}}, + {"^a..", {1, 4}}, + {"..c", {1, 4}}, + {"[^z]c", {2, 4}}, + {} +}; +static const char *const data_ac[] = {"_a\0cdef", "_abcdef", NULL}; + +static const struct reg_res reg_res_aa[] = { + {"^", {1, 1}}, + {"^a", {1, 2}}, + {"a$", {3, 4}}, + {"^\\(a\\).\\1$", {1, 4}}, + {"^a[^a]*" , {1, 3}}, + {} +}; +static const char *const data_aa[] = {"_a\0adef", "_abadef", NULL}; + + +static void +testbunch (const struct reg_res *reg_reses, const char *const *const data) +{ +#define BASEERR(data) \ + support_record_failure (), \ + fprintf (stdout, __FILE__ ": %s: ", reg_reses->regex), \ + fwrite (data + bound.rm_so, 1, bound.rm_eo - bound.rm_so, stdout) + + for (; reg_reses->regex; ++reg_reses) + { + regex_t rgx; + assert (!regcomp (&rgx, reg_reses->regex, 0)); + + for (const char *const *dt = data; *dt; ++dt) + { + regmatch_t match = bound; + if (regexec (&rgx, *dt, 1, &match, REG_STARTEND)) + BASEERR(dt), fputs (": no match\n", stdout); + + if (memcmp(&match, ®_reses->result, sizeof (regmatch_t))) + BASEERR(dt), fprintf (stdout, ": wanted {%d, %d}, got {%d, %d}\n", + (int)reg_reses->result.rm_so, + (int)reg_reses->result.rm_eo, + (int)match.rm_so, (int)match.rm_eo); + } + + regfree(&rgx); + } +} + + +struct mb_data_exp { + const char *data; + bool exp; +}; +static const struct mb_data_exp mb_data_exp[] = { + {"_aaćdef", false}, + {"_aćdef", true}, + {} +}; + +static void +testmb (void) +{ + regex_t rgx; + const struct reg_res reg_reses[] = {{"ać"}}; + assert (!regcomp (&rgx, reg_reses->regex, 0)); + + for (const struct mb_data_exp *de = mb_data_exp; de->data; ++de) + { + regmatch_t match = bound; + if (regexec (&rgx, de->data, 1, &match, REG_STARTEND) == de->exp) + BASEERR(de->data), fprintf (stdout, ": %s match\n", + de->exp ? "no" : "yes"); + + if (memcmp(&match, &bound, sizeof (regmatch_t))) + BASEERR(de->data), fprintf (stdout, ": wanted {%d, %d}, got {%d, %d}\n", + (int)bound.rm_so, (int)bound.rm_eo, + (int)match.rm_so, (int)match.rm_eo); + } + + regfree(&rgx); +} + + +static int +do_test (void) +{ + assert (setlocale (LC_ALL, "C.UTF-8")); + + testbunch (reg_res_ac, data_ac); + testbunch (reg_res_aa, data_aa); + testmb (); + return 0; +} + + +#include