Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hPrlb-0003Ww-PW for pgsql-bugs@arkaria.postgresql.org; Sun, 12 May 2019 16:56:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hPrlZ-0004jN-Al for pgsql-bugs@arkaria.postgresql.org; Sun, 12 May 2019 16:56:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hPrlY-0004jG-W2 for pgsql-bugs@lists.postgresql.org; Sun, 12 May 2019 16:56:01 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hPrlS-0003Dg-80 for pgsql-bugs@lists.postgresql.org; Sun, 12 May 2019 16:55:59 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x4CGtlkA020584; Sun, 12 May 2019 12:55:48 -0400 From: Tom Lane To: Andrew Gierth cc: "Daniel Verite" , "Robert Schreiber" , pgsql-bugs@lists.postgresql.org Subject: Re: PostgreSQL 9.3.5 substring(text from pattern for escape) bug In-reply-to: <87imuggwi4.fsf@news-spur.riddles.org.uk> References: <785708a0-a64c-42ee-9173-f596c1fc92ec@manitou-mail.org> <32565.1557590361@sss.pgh.pa.us> <87mujsgz14.fsf@news-spur.riddles.org.uk> <27602.1557633043@sss.pgh.pa.us> <87imuggwi4.fsf@news-spur.riddles.org.uk> Comments: In-reply-to Andrew Gierth message dated "Sun, 12 May 2019 05:27:48 +0100" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <20537.1557680058.0@sss.pgh.pa.us> Date: Sun, 12 May 2019 12:55:47 -0400 Message-ID: <20583.1557680147@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <20537.1557680058.1@sss.pgh.pa.us> Andrew Gierth writes: > "Tom" == Tom Lane writes: > Tom> Huh, interesting. So we should be translating the initial > Tom> substring to a non-greedy pattern. I believe Spencer's engine can > Tom> handle that by sticking (?:...){1,1}? around it. > Your suggested fix doesn't seem to work. If the leading/trailing > substrings do not have | or parens in then it seems to work to wrap them > in (?:(?:)??...), thanks to the rule that the first quantified atom in a > subexpression sets the whole subexpression's greediness, but handling | > or parens correctly seems harder. [ pokes at that... ] Huh. That's a bug, which AFAICS is aboriginal in Henry's code: it optimizes away a {1,1} quantifier without regard to whether the quantifier is attempting to impose a different greediness preference than its argument would have naturally. The attached seems to fix it. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-regex-greediness-imposition.patch"; charset="us-ascii" Content-ID: <20537.1557680058.2@sss.pgh.pa.us> Content-Description: fix-regex-greediness-imposition.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/regex/regcomp.c b/src/backend/regex/regcomp.c index eb1f3d5..8cd7d56 100644 --- a/src/backend/regex/regcomp.c +++ b/src/backend/regex/regcomp.c @@ -1155,7 +1155,10 @@ parseqatom(struct vars *v, /* rest of branch can be strung starting from atom->end */ s2 =3D atom->end; } - else if (m =3D=3D 1 && n =3D=3D 1) + else if (m =3D=3D 1 && n =3D=3D 1 && + (qprefer =3D=3D 0 || + (atom->flags & (LONGER | SHORTER | MIXED)) =3D=3D 0 || + qprefer =3D=3D (atom->flags & (LONGER | SHORTER | MIXED)))) { /* no/vacuous quantifier: done */ EMPTYARC(s, atom->begin); /* empty prefix */ diff --git a/src/test/regress/expected/regex.out b/src/test/regress/expect= ed/regex.out index c0bfa8a..f372003 100644 --- a/src/test/regress/expected/regex.out +++ b/src/test/regress/expected/regex.out @@ -492,6 +492,55 @@ select regexp_matches('foo/bar/baz', {foo,bar,baz} (1 row) = +-- Test that greediness can be overridden by outer quantifier +select regexp_matches('llmmmfff', '^(l*)(.*)(f*)$'); + regexp_matches = +---------------- + {ll,mmmfff,""} +(1 row) + +select regexp_matches('llmmmfff', '^(l*){1,1}(.*)(f*)$'); + regexp_matches = +---------------- + {ll,mmmfff,""} +(1 row) + +select regexp_matches('llmmmfff', '^(l*){1,1}?(.*)(f*)$'); + regexp_matches = +------------------ + {"",llmmmfff,""} +(1 row) + +select regexp_matches('llmmmfff', '^(l*){1,1}?(.*){1,1}?(f*)$'); + regexp_matches = +---------------- + {"",llmmm,fff} +(1 row) + +select regexp_matches('llmmmfff', '^(l*?)(.*)(f*)$'); + regexp_matches = +------------------ + {"",llmmmfff,""} +(1 row) + +select regexp_matches('llmmmfff', '^(l*?){1,1}(.*)(f*)$'); + regexp_matches = +---------------- + {ll,mmmfff,""} +(1 row) + +select regexp_matches('llmmmfff', '^(l*?){1,1}?(.*)(f*)$'); + regexp_matches = +------------------ + {"",llmmmfff,""} +(1 row) + +select regexp_matches('llmmmfff', '^(l*?){1,1}?(.*){1,1}?(f*)$'); + regexp_matches = +---------------- + {"",llmmm,fff} +(1 row) + -- Test for infinite loop in cfindloop with zero-length possible match -- but no actual match (can only happen in the presence of backrefs) select 'a' ~ '$()|^\1'; diff --git a/src/test/regress/sql/regex.sql b/src/test/regress/sql/regex.s= ql index 1361b62..a174224 100644 --- a/src/test/regress/sql/regex.sql +++ b/src/test/regress/sql/regex.sql @@ -118,6 +118,16 @@ select regexp_matches('Programmer', '(\w)(.*?\1)', 'g= '); select regexp_matches('foo/bar/baz', '^([^/]+?)(?:/([^/]+?))(?:/([^/]+?))?$', ''); = +-- Test that greediness can be overridden by outer quantifier +select regexp_matches('llmmmfff', '^(l*)(.*)(f*)$'); +select regexp_matches('llmmmfff', '^(l*){1,1}(.*)(f*)$'); +select regexp_matches('llmmmfff', '^(l*){1,1}?(.*)(f*)$'); +select regexp_matches('llmmmfff', '^(l*){1,1}?(.*){1,1}?(f*)$'); +select regexp_matches('llmmmfff', '^(l*?)(.*)(f*)$'); +select regexp_matches('llmmmfff', '^(l*?){1,1}(.*)(f*)$'); +select regexp_matches('llmmmfff', '^(l*?){1,1}?(.*)(f*)$'); +select regexp_matches('llmmmfff', '^(l*?){1,1}?(.*){1,1}?(f*)$'); + -- Test for infinite loop in cfindloop with zero-length possible match -- but no actual match (can only happen in the presence of backrefs) select 'a' ~ '$()|^\1'; ------- =_aaaaaaaaaa0--