Skip to content

Commit d4f063d

Browse files
committed
[bugfix] Avoid NPE under rare circumstances in translating a Java Exception to an XQuery Error Code.
This is a regression that was introduced in 91ab58f. Closes #7
1 parent 15aa381 commit d4f063d

File tree

7 files changed

+108
-19
lines changed

7 files changed

+108
-19
lines changed

exist-core/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@
870870
<include>src/main/java/org/exist/xquery/ErrorCodes.java</include>
871871
<include>src/main/java/org/exist/xquery/FunctionFactory.java</include>
872872
<include>src/main/java/org/exist/xquery/Optimizer.java</include>
873+
<include>src/main/java/org/exist/xquery/TryCatchExpression.java</include>
873874
<include>src/main/java/org/exist/xquery/UserDefinedFunction.java</include>
874875
<include>src/main/java/org/exist/xquery/XPathUtil.java</include>
875876
<include>src/main/java/org/exist/xquery/XQueryContext.java</include>
@@ -1160,6 +1161,7 @@
11601161
<exclude>src/main/java/org/exist/xquery/Materializable.java</exclude>
11611162
<exclude>src/main/java/org/exist/xquery/NameTest.java</exclude>
11621163
<exclude>src/main/java/org/exist/xquery/Optimizer.java</exclude>
1164+
<exclude>src/main/java/org/exist/xquery/TryCatchExpression.java</exclude>
11631165
<exclude>src/main/java/org/exist/xquery/UserDefinedFunction.java</exclude>
11641166
<exclude>src/test/java/org/exist/xquery/WatchdogTest.java</exclude>
11651167
<exclude>src/main/java/org/exist/xquery/XPathUtil.java</exclude>

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import org.exist.Namespaces;
4949
import org.exist.dom.QName;
5050

51+
import javax.annotation.Nullable;
52+
5153
/**
5254
*
5355
* @author aretter
@@ -291,14 +293,14 @@ public class ErrorCodes {
291293
public static class ErrorCode {
292294

293295
private final QName errorQName;
294-
private final String description;
296+
private @Nullable final String description;
295297

296-
public ErrorCode(String code, String description) {
298+
public ErrorCode(final String code, @Nullable final String description) {
297299
this.errorQName = new QName(code, Namespaces.EXIST_XQUERY_XPATH_ERROR_NS, Namespaces.EXIST_XQUERY_XPATH_ERROR_PREFIX);
298300
this.description = description;
299301
}
300302

301-
public ErrorCode(QName errorQName, String description) {
303+
public ErrorCode(final QName errorQName, final String description) {
302304
this.errorQName = errorQName;
303305
this.description = description;
304306
}
@@ -309,10 +311,10 @@ public QName getErrorQName() {
309311

310312
@Override
311313
public String toString() {
312-
return "(" + errorQName.getNamespaceURI() + "#" + errorQName.getLocalPart() + "):" + description;
314+
return "(" + errorQName.toString() + "): " + description;
313315
}
314316

315-
public String getDescription(){
317+
public @Nullable String getDescription(){
316318
return description;
317319
}
318320
}
@@ -333,12 +335,21 @@ private EXistErrorCode(String code, String description) {
333335

334336
public static class JavaErrorCode extends ErrorCode {
335337

336-
public JavaErrorCode(final Throwable throwable) {
337-
super(new QName(
338-
throwable.getClass().getName(),
339-
Namespaces.EXIST_JAVA_BINDING_NS,
340-
Namespaces.EXIST_JAVA_BINDING_NS_PREFIX),
341-
throwable.getMessage() != null ? throwable.getMessage() : throwable.getCause().getMessage());
338+
private JavaErrorCode(final QName errorQName, @Nullable final String description) {
339+
super(errorQName, description);
340+
}
341+
342+
public static JavaErrorCode fromThrowable(final Throwable throwable) {
343+
final QName errorQName = new QName(throwable.getClass().getName(), Namespaces.EXIST_JAVA_BINDING_NS, Namespaces.EXIST_JAVA_BINDING_NS_PREFIX);
344+
@Nullable final String description;
345+
if (throwable.getMessage() != null) {
346+
description = throwable.getMessage();
347+
} else if (throwable.getCause() != null) {
348+
description = throwable.getCause().getMessage();
349+
} else {
350+
description = null;
351+
}
352+
return new JavaErrorCode(errorQName, description);
342353
}
343354
}
344355
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
558558
return XPathUtil.javaObjectToXPath(javaResult, context, true, false, false, this);
559559

560560
} catch (final IllegalAccessException | InvocationTargetException | InstantiationException | IllegalArgumentException e) {
561-
throw new XPathException(this, new ErrorCodes.JavaErrorCode(e), e.getMessage());
561+
throw new XPathException(this, ErrorCodes.JavaErrorCode.fromThrowable(e), e.getMessage());
562562
}
563563
}
564564

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

Lines changed: 26 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
*
@@ -169,7 +193,7 @@ public Sequence eval(final Sequence contextSequence, final Item contextItem) thr
169193
}
170194
} else {
171195
// Get errorcode from all other errors and exceptions
172-
errorCode = new JavaErrorCode(throwable);
196+
errorCode = JavaErrorCode.fromThrowable(throwable);
173197
}
174198

175199
// We need the qname in the end
@@ -417,7 +441,7 @@ private ErrorCode extractErrorCode(final XPathException xpe) {
417441
}
418442

419443
// Fallback, create java error
420-
return new ErrorCodes.JavaErrorCode(retVal);
444+
return JavaErrorCode.fromThrowable(retVal);
421445
}
422446

423447
@Override

extensions/modules/exi/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@
117117
-->
118118
<header>${project.parent.relativePath}/../../elemental-parent/elemental-LGPL-21-ONLY-license.template.txt</header>
119119
<excludes>
120+
<exclude>exi-functions-demo.xql</exclude>
120121
<exclude>pom.xml</exclude>
121122
<exclude>src/**</exclude>
122-
<exclude>exi-functions-demo.xql</exclude>
123123
</excludes>
124124
</licenseSet>
125125

@@ -138,6 +138,8 @@
138138
</multi>
139139
<includes>
140140
<include>pom.xml</include>
141+
<include>src/main/java/org/exist/xquery/modules/exi/DecodeExiFunction.java</include>
142+
<include>src/main/java/org/exist/xquery/modules/exi/EncodeExiFunction.java</include>
141143
</includes>
142144
</licenseSet>
143145

@@ -148,6 +150,8 @@
148150
<header>${project.parent.relativePath}/../../exist-parent/existdb-LGPL-21-license.template.txt</header>
149151
<excludes>
150152
<exclude>pom.xml</exclude>
153+
<exclude>src/main/java/org/exist/xquery/modules/exi/DecodeExiFunction.java</exclude>
154+
<exclude>src/main/java/org/exist/xquery/modules/exi/EncodeExiFunction.java</exclude>
151155
</excludes>
152156
</licenseSet>
153157

extensions/modules/exi/src/main/java/org/exist/xquery/modules/exi/DecodeExiFunction.java

Lines changed: 26 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
*
@@ -125,8 +149,8 @@ public Sequence eval(Sequence[] args, Sequence contextSequence)
125149
context.popDocumentContext();
126150
}
127151
}
128-
catch(EXIException | SAXException | IOException exie) {
129-
throw new XPathException(this, new JavaErrorCode(exie.getCause()), exie.getMessage());
152+
catch(final EXIException | SAXException | IOException exie) {
153+
throw new XPathException(this, JavaErrorCode.fromThrowable(exie.getCause()), exie.getMessage());
130154
}
131155
}
132156

extensions/modules/exi/src/main/java/org/exist/xquery/modules/exi/EncodeExiFunction.java

Lines changed: 26 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
*
@@ -114,8 +138,8 @@ public Sequence eval(Sequence[] args, Sequence contextSequence)
114138
// TODO - test!
115139
throw new XPathException(this, ErrorCodes.FODC0002, ioex.getMessage());
116140
}
117-
catch(EXIException | SAXException exie) {
118-
throw new XPathException(this, new JavaErrorCode(exie.getCause()), exie.getMessage());
141+
catch(final EXIException | SAXException exie) {
142+
throw new XPathException(this, JavaErrorCode.fromThrowable(exie.getCause()), exie.getMessage());
119143
}
120144
}
121145

0 commit comments

Comments
 (0)