Skip to content

Commit e7c8c3f

Browse files
committed
Fix bugs in Painless SCatch node (#45880)
This fixes two bugs: - A recently introduced bug where an NPE will be thrown if a catch block is empty. - A long-time bug where an NPE will be thrown if multiple catch blocks in a row are empty for the same try block.
1 parent 30e62a5 commit e7c8c3f

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SCatch.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public SCatch(Location location, String type, String name, SBlock block) {
5353
this.block = block;
5454
}
5555

56-
@Override
5756
void extractVariables(Set<String> variables) {
5857
variables.add(name);
5958

@@ -109,7 +108,7 @@ void write(MethodWriter writer, Globals globals) {
109108

110109
writer.visitTryCatchBlock(begin, end, jump, MethodWriter.getType(variable.clazz).getInternalName());
111110

112-
if (exception != null && !block.allEscape) {
111+
if (exception != null && (block == null || !block.allEscape)) {
113112
writer.goTo(exception);
114113
}
115114
}

modules/lang-painless/src/test/java/org/elasticsearch/painless/TryCatchTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,47 @@ public void testNoCatch() {
5555
});
5656
assertEquals("test", exception.getMessage());
5757
}
58+
59+
public void testNoCatchBlock() {
60+
assertEquals(0, exec("try { return Integer.parseInt('f') } catch (NumberFormatException nfe) {} return 0;"));
61+
62+
assertEquals(0, exec("try { return Integer.parseInt('f') } " +
63+
"catch (NumberFormatException nfe) {}" +
64+
"catch (Exception e) {}" +
65+
" return 0;"));
66+
67+
assertEquals(0, exec("try { throw new IllegalArgumentException('test') } " +
68+
"catch (NumberFormatException nfe) {}" +
69+
"catch (Exception e) {}" +
70+
" return 0;"));
71+
72+
assertEquals(0, exec("try { throw new IllegalArgumentException('test') } " +
73+
"catch (NumberFormatException nfe) {}" +
74+
"catch (IllegalArgumentException iae) {}" +
75+
"catch (Exception e) {}" +
76+
" return 0;"));
77+
}
78+
79+
public void testMultiCatch() {
80+
assertEquals(1, exec(
81+
"try { return Integer.parseInt('f') } " +
82+
"catch (NumberFormatException nfe) {return 1;} " +
83+
"catch (ArrayIndexOutOfBoundsException aioobe) {return 2;} " +
84+
"catch (Exception e) {return 3;}"
85+
));
86+
87+
assertEquals(2, exec(
88+
"try { return new int[] {}[0] } " +
89+
"catch (NumberFormatException nfe) {return 1;} " +
90+
"catch (ArrayIndexOutOfBoundsException aioobe) {return 2;} " +
91+
"catch (Exception e) {return 3;}"
92+
));
93+
94+
assertEquals(3, exec(
95+
"try { throw new IllegalArgumentException('test'); } " +
96+
"catch (NumberFormatException nfe) {return 1;} " +
97+
"catch (ArrayIndexOutOfBoundsException aioobe) {return 2;} " +
98+
"catch (Exception e) {return 3;}"
99+
));
100+
}
58101
}

0 commit comments

Comments
 (0)