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 eXist-db#3803
  • Loading branch information
adamretter committed Oct 10, 2024
1 parent 437974e commit c5d0a59
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 @@ -121,6 +121,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 @@ -139,4 +145,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 c5d0a59

Please sign in to comment.