Skip to content

Commit 92d2b6e

Browse files
committed
[bugfix] fn:replace, fn:tokenize, and fn:analyze-string must throw FORX0003 when pattern matches an empty string
Closes #3803
1 parent fe3ff5c commit 92d2b6e

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/FunAnalyzeString.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ private void analyzeString(final MemTreeBuilder builder, final String input, Str
130130

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

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

exist-core/src/main/java/org/exist/xquery/functions/fn/FunReplace.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
120120

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

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

exist-core/src/test/xquery/regex.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@
137137
<expected>lo</expected>
138138
</test>
139139

140+
<test output="text">
141+
<task>fn:replace-regex-match-empty-1</task>
142+
<code>fn:replace("12.34" , "^\D*", "")</code>
143+
<error>FORX0003</error>
144+
</test>
145+
140146
<test output="text">
141147
<task>fn:tokenize-qflag-1</task>
142148
<code>fn:tokenize("12.3.5.6", ".", "q")</code>
@@ -155,4 +161,16 @@
155161
<error>XPTY0004</error>
156162
</test>
157163

164+
<test output="text">
165+
<task>fn:tokenize-regex-match-empty-1</task>
166+
<code>fn:tokenize("12.34" , "^\D*")</code>
167+
<error>FORX0003</error>
168+
</test>
169+
170+
<test output="text">
171+
<task>fn:analyze-string-regex-match-empty-1</task>
172+
<code>fn:analyze-string("12.34" , "^\D*")</code>
173+
<error>FORX0003</error>
174+
</test>
175+
158176
</TestSet>

0 commit comments

Comments
 (0)