Skip to content

Commit 53eb612

Browse files
committed
SPR-6059 Avoiding NPE for OR operator. If the value is null instead of a valid boolean or Boolean, it will now trigger an EvaluationException.
1 parent 4a315ba commit 53eb612

4 files changed

Lines changed: 58 additions & 7 deletions

File tree

‎org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpAnd.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import org.springframework.expression.TypedValue;
2121
import org.springframework.expression.spel.ExpressionState;
2222
import org.springframework.expression.spel.SpelEvaluationException;
23+
import org.springframework.expression.spel.SpelMessage;
2324
import org.springframework.expression.spel.support.BooleanTypedValue;
2425

2526
/**
2627
* Represents the boolean AND operation.
2728
*
2829
* @author Andy Clement
30+
* @author Mark Fisher
2931
* @since 3.0
3032
*/
3133
public class OpAnd extends Operator {
@@ -40,7 +42,9 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep
4042
boolean rightValue;
4143

4244
try {
43-
leftValue = (Boolean)state.convertValue(getLeftOperand().getValueInternal(state), BOOLEAN_TYPE_DESCRIPTOR);
45+
TypedValue typedValue = getLeftOperand().getValueInternal(state);
46+
this.assertTypedValueNotNull(typedValue);
47+
leftValue = (Boolean)state.convertValue(typedValue, BOOLEAN_TYPE_DESCRIPTOR);
4448
}
4549
catch (SpelEvaluationException ee) {
4650
ee.setPosition(getLeftOperand().getStartPosition());
@@ -52,7 +56,9 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep
5256
}
5357

5458
try {
55-
rightValue = (Boolean)state.convertValue(getRightOperand().getValueInternal(state), BOOLEAN_TYPE_DESCRIPTOR);
59+
TypedValue typedValue = getRightOperand().getValueInternal(state);
60+
this.assertTypedValueNotNull(typedValue);
61+
rightValue = (Boolean)state.convertValue(typedValue, BOOLEAN_TYPE_DESCRIPTOR);
5662
}
5763
catch (SpelEvaluationException ee) {
5864
ee.setPosition(getRightOperand().getStartPosition());
@@ -62,4 +68,10 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep
6268
return /* leftValue && */BooleanTypedValue.forValue(rightValue);
6369
}
6470

71+
private void assertTypedValueNotNull(TypedValue typedValue) {
72+
if (TypedValue.NULL_TYPED_VALUE.equals(typedValue)) {
73+
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
74+
}
75+
}
76+
6577
}

‎org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpOr.java‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
package org.springframework.expression.spel.ast;
1818

1919
import org.springframework.expression.EvaluationException;
20+
import org.springframework.expression.TypedValue;
2021
import org.springframework.expression.spel.ExpressionState;
2122
import org.springframework.expression.spel.SpelEvaluationException;
23+
import org.springframework.expression.spel.SpelMessage;
2224
import org.springframework.expression.spel.support.BooleanTypedValue;
2325

2426
/**
2527
* Represents the boolean OR operation.
2628
*
2729
* @author Andy Clement
30+
* @author Mark Fisher
2831
* @since 3.0
2932
*/
3033
public class OpOr extends Operator {
@@ -37,8 +40,10 @@ public OpOr(int pos, SpelNodeImpl... operands) {
3740
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
3841
boolean leftValue;
3942
boolean rightValue;
40-
try {
41-
leftValue = (Boolean)state.convertValue(getLeftOperand().getValueInternal(state), BOOLEAN_TYPE_DESCRIPTOR);
43+
try {
44+
TypedValue typedValue = getLeftOperand().getValueInternal(state);
45+
this.assertTypedValueNotNull(typedValue);
46+
leftValue = (Boolean)state.convertValue(typedValue, BOOLEAN_TYPE_DESCRIPTOR);
4247
}
4348
catch (SpelEvaluationException see) {
4449
see.setPosition(getLeftOperand().getStartPosition());
@@ -50,7 +55,9 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati
5055
}
5156

5257
try {
53-
rightValue = (Boolean)state.convertValue(getRightOperand().getValueInternal(state), BOOLEAN_TYPE_DESCRIPTOR);
58+
TypedValue typedValue = getRightOperand().getValueInternal(state);
59+
this.assertTypedValueNotNull(typedValue);
60+
rightValue = (Boolean)state.convertValue(typedValue, BOOLEAN_TYPE_DESCRIPTOR);
5461
}
5562
catch (SpelEvaluationException see) {
5663
see.setPosition(getRightOperand().getStartPosition()); // TODO end positions here and in similar situations
@@ -60,4 +67,10 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati
6067
return BooleanTypedValue.forValue(leftValue || rightValue);
6168
}
6269

70+
private void assertTypedValueNotNull(TypedValue typedValue) {
71+
if (TypedValue.NULL_TYPED_VALUE.equals(typedValue)) {
72+
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
73+
}
74+
}
75+
6376
}

‎org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OperatorNot.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616

1717
package org.springframework.expression.spel.ast;
1818

19-
import org.springframework.core.convert.TypeDescriptor;
2019
import org.springframework.expression.EvaluationException;
20+
import org.springframework.expression.TypedValue;
2121
import org.springframework.expression.spel.ExpressionState;
2222
import org.springframework.expression.spel.SpelEvaluationException;
23+
import org.springframework.expression.spel.SpelMessage;
2324
import org.springframework.expression.spel.support.BooleanTypedValue;
2425

2526
/**
2627
* Represents a NOT operation.
2728
*
2829
* @author Andy Clement
30+
* @author Mark Fisher
2931
* @since 3.0
3032
*/
3133
public class OperatorNot extends SpelNodeImpl { // Not is a unary operator so do not extend BinaryOperator
@@ -37,7 +39,11 @@ public OperatorNot(int pos, SpelNodeImpl operand) {
3739
@Override
3840
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
3941
try {
40-
boolean value = (Boolean) state.convertValue(children[0].getValueInternal(state), TypeDescriptor.valueOf(boolean.class));
42+
TypedValue typedValue = children[0].getValueInternal(state);
43+
if (TypedValue.NULL_TYPED_VALUE.equals(typedValue)) {
44+
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
45+
}
46+
boolean value = (Boolean) state.convertValue(typedValue, BOOLEAN_TYPE_DESCRIPTOR);
4147
return BooleanTypedValue.forValue(!value);
4248
}
4349
catch (SpelEvaluationException see) {

‎org.springframework.expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,26 @@ public void testUnaryNotWithNullValue() {
308308
parser.parseExpression("!null").getValue();
309309
}
310310

311+
@Test(expected = EvaluationException.class)
312+
public void testAndWithNullValueOnLeft() {
313+
parser.parseExpression("null and true").getValue();
314+
}
315+
316+
@Test(expected = EvaluationException.class)
317+
public void testAndWithNullValueOnRight() {
318+
parser.parseExpression("true and null").getValue();
319+
}
320+
321+
@Test(expected = EvaluationException.class)
322+
public void testOrWithNullValueOnLeft() {
323+
parser.parseExpression("null or false").getValue();
324+
}
325+
326+
@Test(expected = EvaluationException.class)
327+
public void testOrWithNullValueOnRight() {
328+
parser.parseExpression("false or null").getValue();
329+
}
330+
311331
// assignment
312332
@Test
313333
public void testAssignmentToVariables01() {

0 commit comments

Comments
 (0)