Skip to content

Commit e5c7cc9

Browse files
committed
[bugfix] When setting XQuery external variables, ensure that the values are checked against the types of the Variable Declarations
1 parent 44f7a6e commit e5c7cc9

File tree

8 files changed

+238
-42
lines changed

8 files changed

+238
-42
lines changed

exist-core/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,23 +1066,28 @@
10661066
<include>src/main/java/org/exist/xmlrpc/RpcConnection.java</include>
10671067
<include>src/main/java/org/exist/xqj/Marshaller.java</include>
10681068
<include>src/test/java/org/exist/xqj/MarshallerTest.java</include>
1069+
<include>src/main/java/org/exist/xquery/AbstractInternalModule.java</include>
10691070
<include>src/test/java/org/exist/xquery/CardinalityTest.java</include>
10701071
<include>src/test/java/org/exist/xquery/CleanupTest.java</include>
10711072
<include>src/test/java/org/exist/xquery/ConstructedNodesRecoveryTest.java</include>
10721073
<include>src/main/java/org/exist/xquery/DeferredFunctionCall.java</include>
10731074
<include>src/main/java/org/exist/xquery/DynamicCardinalityCheck.java</include>
10741075
<include>src/main/java/org/exist/xquery/DynamicTypeCheck.java</include>
10751076
<include>src/main/java/org/exist/xquery/ErrorCodes.java</include>
1077+
<include>src/main/java/org/exist/xquery/ExternalModuleImpl.java</include>
10761078
<include>src/test/java/org/exist/xquery/ForwardReferenceTest.java</include>
10771079
<include>src/main/java/org/exist/xquery/Function.java</include>
10781080
<include>src/main/java/org/exist/xquery/FunctionFactory.java</include>
10791081
<include>src/main/java/org/exist/xquery/LocationStep.java</include>
1082+
<include>src/main/java/org/exist/xquery/Module.java</include>
10801083
<include>src/main/java/org/exist/xquery/NamedFunctionReference.java</include>
10811084
<include>src/main/java/org/exist/xquery/Optimizer.java</include>
10821085
<include>src/main/java/org/exist/xquery/PerformanceStatsImpl.java</include>
10831086
<include>src/test/java/org/exist/xquery/RestBinariesTest.java</include>
1087+
<include>src/test/java/org/exist/xquery/StoredModuleTest.java</include>
10841088
<include>src/main/java/org/exist/xquery/TryCatchExpression.java</include>
10851089
<include>src/main/java/org/exist/xquery/UserDefinedFunction.java</include>
1090+
<include>src/main/java/org/exist/xquery/VariableDeclaration.java</include>
10861091
<include>src/test/java/org/exist/xquery/WindowClauseTest.java</include>
10871092
<include>src/test/java/org/exist/xquery/XmldbBinariesTest.java</include>
10881093
<include>src/test/java/org/exist/xquery/XPathQueryTest.java</include>
@@ -1666,6 +1671,7 @@
16661671
<exclude>src/main/java/org/exist/xmlrpc/RpcConnection.java</exclude>
16671672
<exclude>src/main/java/org/exist/xqj/Marshaller.java</exclude>
16681673
<exclude>src/test/java/org/exist/xqj/MarshallerTest.java</exclude>
1674+
<exclude>src/main/java/org/exist/xquery/AbstractInternalModule.java</exclude>
16691675
<exclude>src/main/java/org/exist/xquery/Cardinality.java</exclude>
16701676
<exclude>src/test/java/org/exist/xquery/CardinalityTest.java</exclude>
16711677
<exclude>src/test/java/org/exist/xquery/CastExpressionTest.java</exclude>
@@ -1675,6 +1681,7 @@
16751681
<exclude>src/main/java/org/exist/xquery/DynamicCardinalityCheck.java</exclude>
16761682
<exclude>src/main/java/org/exist/xquery/DynamicTypeCheck.java</exclude>
16771683
<exclude>src/main/java/org/exist/xquery/ErrorCodes.java</exclude>
1684+
<exclude>src/main/java/org/exist/xquery/ExternalModuleImpl.java</exclude>
16781685
<exclude>src/test/java/org/exist/xquery/ForwardReferenceTest.java</exclude>
16791686
<exclude>src/main/java/org/exist/xquery/Function.java</exclude>
16801687
<exclude>src/main/java/org/exist/xquery/FunctionFactory.java</exclude>
@@ -1686,13 +1693,16 @@
16861693
<exclude>src/test/java/org/exist/xquery/JavaBindingTest.java</exclude>
16871694
<exclude>src/main/java/org/exist/xquery/LocationStep.java</exclude>
16881695
<exclude>src/main/java/org/exist/xquery/Materializable.java</exclude>
1696+
<exclude>src/main/java/org/exist/xquery/Module.java</exclude>
16891697
<exclude>src/main/java/org/exist/xquery/NamedFunctionReference.java</exclude>
16901698
<exclude>src/main/java/org/exist/xquery/NameTest.java</exclude>
16911699
<exclude>src/main/java/org/exist/xquery/Optimizer.java</exclude>
16921700
<exclude>src/main/java/org/exist/xquery/PerformanceStatsImpl.java</exclude>
16931701
<exclude>src/test/java/org/exist/xquery/RestBinariesTest.java</exclude>
1702+
<exclude>src/test/java/org/exist/xquery/StoredModuleTest.java</exclude>
16941703
<exclude>src/main/java/org/exist/xquery/TryCatchExpression.java</exclude>
16951704
<exclude>src/main/java/org/exist/xquery/UserDefinedFunction.java</exclude>
1705+
<exclude>src/main/java/org/exist/xquery/VariableDeclaration.java</exclude>
16961706
<exclude>src/test/java/org/exist/xquery/WatchdogTest.java</exclude>
16971707
<exclude>src/test/java/org/exist/xquery/WindowClauseTest.java</exclude>
16981708
<exclude>src/test/java/org/exist/xquery/XmldbBinariesTest.java</exclude>

exist-core/src/main/antlr/org/exist/xquery/parser/XQueryTree.g

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,8 @@ throws PermissionDeniedException, EXistException, XPathException
532532
}
533533
declaredGlobalVars.add(qn);
534534
}
535-
{ List annots = new ArrayList(); }
536-
(annotations [annots]
537-
)?
535+
{ List annots = new ArrayList(); }
536+
(annotations [annots])?
538537
(
539538
#(
540539
"as"
@@ -564,22 +563,11 @@ throws PermissionDeniedException, EXistException, XPathException
564563
step=ext:expr [defaultValue]
565564
)?
566565
{
567-
// variable may be declared in static context: retrieve and set its sequence type
568-
Variable external = null;
569-
try {
570-
external = context.resolveVariable(qname.getText());
571-
if (external != null) {
572-
external.setSequenceType(type);
573-
}
574-
} catch (XPathException ignoredException) {
575-
}
576-
577566
final VariableDeclaration decl = new VariableDeclaration(context, qn, defaultValue);
578567
decl.setSequenceType(type);
568+
decl.setExternal(true);
579569
decl.setASTNode(ext);
580-
if (external == null) {
581-
path.add(decl);
582-
}
570+
path.add(decl);
583571
if(myModule != null) {
584572
myModule.declareVariable(qn, decl);
585573
}

exist-core/src/main/java/org/exist/xquery/AbstractInternalModule.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -222,6 +246,11 @@ public Variable resolveVariable(@Nullable final AnalyzeContextInfo contextInfo,
222246

223247
@Override
224248
public boolean isVarDeclared(final QName qname) {
249+
return isVarSet(qname);
250+
}
251+
252+
@Override
253+
public boolean isVarSet(final QName qname) {
225254
return mGlobalVariables.get(qname) != null;
226255
}
227256

exist-core/src/main/java/org/exist/xquery/ExternalModuleImpl.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -210,12 +234,18 @@ public void declareVariable(QName qname, VariableDeclaration decl) throws XPathE
210234
mGlobalVariables.put(qname, decl);
211235
}
212236

213-
public boolean isVarDeclared(QName qname) {
237+
@Override
238+
public boolean isVarDeclared(final QName qname) {
214239
if (mGlobalVariables.get(qname) != null)
215240
{return true;}
216241
return mStaticVariables.get(qname) != null;
217242
}
218243

244+
@Override
245+
public boolean isVarSet(final QName qname) {
246+
return mStaticVariables.get(qname) != null;
247+
}
248+
219249
@Override
220250
@Nullable public Variable resolveVariable(final QName qname) throws XPathException {
221251
return resolveVariable(null, qname);

exist-core/src/main/java/org/exist/xquery/Module.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -104,8 +128,24 @@ public interface Module {
104128
public Variable declareVariable(QName qname, Object value) throws XPathException;
105129

106130
public Variable declareVariable(Variable var);
107-
108-
public boolean isVarDeclared(QName qname);
131+
132+
/**
133+
* Is a variable declared?
134+
*
135+
* @param qname the name of the variable.
136+
*
137+
* @return true if the variable is declared, false otherwise.
138+
*/
139+
boolean isVarDeclared(QName qname);
140+
141+
/**
142+
* Is a variable set?
143+
*
144+
* @param qname the name of the variable.
145+
*
146+
* @return true if the variable is set, false otherwise.
147+
*/
148+
boolean isVarSet(QName qname);
109149

110150
/**
111151
* Returns an iterator over all global variables in this modules, which were

0 commit comments

Comments
 (0)