From c5d0a59894a57911e64a72a7f071fa9bf6a8b72d Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Mon, 10 Apr 2023 15:12:25 +0100 Subject: [PATCH] [bugfix] fn:replace, fn:tokenize, and fn:analyze-string must throw FORX0003 when pattern matches an empty string Closes https://github.com/eXist-db/exist/issues/3803 --- .../xquery/functions/fn/FunAnalyzeString.java | 3 +++ .../exist/xquery/functions/fn/FunReplace.java | 3 +++ exist-core/src/test/xquery/regex.xml | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/exist-core/src/main/java/org/exist/xquery/functions/fn/FunAnalyzeString.java b/exist-core/src/main/java/org/exist/xquery/functions/fn/FunAnalyzeString.java index 1fd6bc13bc7..870fa3e392a 100644 --- a/exist-core/src/main/java/org/exist/xquery/functions/fn/FunAnalyzeString.java +++ b/exist-core/src/main/java/org/exist/xquery/functions/fn/FunAnalyzeString.java @@ -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 diff --git a/exist-core/src/main/java/org/exist/xquery/functions/fn/FunReplace.java b/exist-core/src/main/java/org/exist/xquery/functions/fn/FunReplace.java index 258d339fbfe..7707ee1700a 100644 --- a/exist-core/src/main/java/org/exist/xquery/functions/fn/FunReplace.java +++ b/exist-core/src/main/java/org/exist/xquery/functions/fn/FunReplace.java @@ -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 diff --git a/exist-core/src/test/xquery/regex.xml b/exist-core/src/test/xquery/regex.xml index 2f56a19d51c..349abe6f16c 100644 --- a/exist-core/src/test/xquery/regex.xml +++ b/exist-core/src/test/xquery/regex.xml @@ -121,6 +121,12 @@ lo + + fn:replace-regex-match-empty-1 + fn:replace("12.34" , "^\D*", "") + FORX0003 + + fn:tokenize-qflag-1 fn:tokenize("12.3.5.6", ".", "q") @@ -139,4 +145,16 @@ XPTY0004 + + fn:tokenize-regex-match-empty-1 + fn:tokenize("12.34" , "^\D*") + FORX0003 + + + + fn:analyze-string-regex-match-empty-1 + fn:analyze-string("12.34" , "^\D*") + FORX0003 + + \ No newline at end of file