Skip to content

Commit

Permalink
[bugfix] fn:replace, fn:tokenize, and fn:analyze-string must throw FO…
Browse files Browse the repository at this point in the history
…RX0003 when pattern matches an empty string

Closes #3803
  • Loading branch information
adamretter committed Apr 10, 2023
1 parent fe3ff5c commit 92d2b6e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ private void analyzeString(final MemTreeBuilder builder, final String input, Str

try {
final RegularExpression regularExpression = config.compileRegularExpression(pattern, flags, "XP30", warnings);
if (regularExpression.matches("")) {
throw new XPathException(this, ErrorCodes.FORX0003, "regular expression could match empty string");
}

//TODO(AR) cache the regular expression... might be possible through Saxon config

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro

try {
final RegularExpression regularExpression = config.compileRegularExpression(pattern, flags, "XP30", warnings);
if (regularExpression.matches("")) {
throw new XPathException(this, ErrorCodes.FORX0003, "regular expression could match empty string");
}

//TODO(AR) cache the regular expression... might be possible through Saxon config

Expand Down
18 changes: 18 additions & 0 deletions exist-core/src/test/xquery/regex.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@
<expected>lo</expected>
</test>

<test output="text">
<task>fn:replace-regex-match-empty-1</task>
<code>fn:replace("12.34" , "^\D*", "")</code>
<error>FORX0003</error>
</test>

<test output="text">
<task>fn:tokenize-qflag-1</task>
<code>fn:tokenize("12.3.5.6", ".", "q")</code>
Expand All @@ -155,4 +161,16 @@
<error>XPTY0004</error>
</test>

<test output="text">
<task>fn:tokenize-regex-match-empty-1</task>
<code>fn:tokenize("12.34" , "^\D*")</code>
<error>FORX0003</error>
</test>

<test output="text">
<task>fn:analyze-string-regex-match-empty-1</task>
<code>fn:analyze-string("12.34" , "^\D*")</code>
<error>FORX0003</error>
</test>

</TestSet>

0 comments on commit 92d2b6e

Please sign in to comment.