Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null pointer exception during exception evaluation #5217

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ public interface ExpressionEvaluator {
Object evaluate(final String statement, final Event context);

default Boolean evaluateConditional(final String statement, final Event context) {
final Object result = evaluate(statement, context);
if (result instanceof Boolean) {
return (Boolean) result;
} else {
throw new ClassCastException("Unexpected expression return type of " + result.getClass());
Object result;
try {
result = evaluate(statement, context);
if (result == null) {
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this is correct. This indicates a bug with our exception handling because any boolean expression should return non-null. Maybe we need to handle this by throwing a more specific exception. Or perhaps we log this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we cover all the cases in the code below. I think we should still consider this an error. If we hit this point we are experiencing indeterminate behavior which is probably best considered as an error state.

}
if (result instanceof Boolean) {
return (Boolean) result;
}
} catch (ExpressionEvaluationException e) {
return false;
}
throw new ClassCastException("Unexpected expression return type of " + result.getClass());
}

Boolean isValidExpressionStatement(final String statement);
Expand All @@ -42,4 +49,4 @@ default Boolean evaluateConditional(final String statement, final Event context)
List<String> extractDynamicKeysFromFormatExpression(final String format);

List<String> extractDynamicExpressionsFromFormatExpression(final String format);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public void testDefaultEvaluateConditional() {

}

@Test
public void testEvaluateReturningException() {
expressionEvaluator = new TestExpressionEvaluator();
assertThat(expressionEvaluator.evaluateConditional("/status > 300", event("{\"nostatus\":true}")), equalTo(false));

}

@Test
public void testDefaultEvaluateConditionalThrows() {
expressionEvaluator = new TestExpressionEvaluator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public Object evaluate(final Object ... args) {
checkArgument(args.length == 2, displayName + " requires operands length needs to be 2.");
final Object leftValue = args[0];
final Object rightValue = args[1];
checkArgument(leftValue != null && rightValue != null, displayName + " requires operands length needs to be non-null.");
final Class<?> leftValueClass = leftValue.getClass();
final Class<?> rightValueClass = rightValue.getClass();
if (leftValue instanceof String && rightValue instanceof String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Number evaluate(final Object ... args) {
checkArgument(args.length == 2, displayName + " requires operands length needs to be 2.");
final Object leftValue = args[0];
final Object rightValue = args[1];
checkArgument(leftValue != null && rightValue != null, displayName + " requires operands length needs to be non-null.");
final Class<?> leftValueClass = leftValue.getClass();
final Class<?> rightValueClass = rightValue.getClass();
if (!operandsToOperationMap.containsKey(leftValueClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public Number evaluate(final Object ... args) {
checkArgument(args.length == 2, displayName + " requires operands length needs to be 2.");
final Object leftValue = args[0];
final Object rightValue = args[1];
checkArgument(leftValue != null && rightValue != null, displayName + " requires operands length needs to be non-null.");
final Class<?> leftValueClass = leftValue.getClass();
final Class<?> rightValueClass = rightValue.getClass();
if (!operandsToOperationMap.containsKey(leftValueClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Boolean evaluate(final Object ... args) {
checkArgument(args.length == 2, displayName + " requires operands length needs to be 2.");
final Object leftValue = args[0];
final Object rightValue = args[1];
checkArgument(leftValue != null && rightValue != null, displayName + " requires operands length needs to be non-null.");
final Class<?> leftValueClass = leftValue.getClass();
final Class<?> rightValueClass = rightValue.getClass();
if (!operandsToOperationMap.containsKey(leftValueClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Object evaluate(ParseTree parseTree, Event event) {
walker.walk(listener, parseTree);
return listener.getResult();
} catch (final Exception e) {
LOG.error("Unable to evaluate event", e);
LOG.error(e.getMessage());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should log the stack trace when catching Exception.

Please add new catch statements for known exceptions (e.g. parse error) and log those without the stack trace.

throw new ExpressionEvaluationException(e.getMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void testGivenEvaluatorThrowsExceptionThenExceptionThrown() {
doReturn(parseTree).when(parser).parse(eq(statement));
doThrow(new RuntimeException()).when(evaluator).evaluate(eq(parseTree), eq(event));

assertThrows(ExpressionEvaluationException.class, () -> statementEvaluator.evaluateConditional(statement, event));
assertThat(statementEvaluator.evaluateConditional(statement, event), equalTo(false));

verify(parser).parse(eq(statement));
verify(evaluator).evaluate(eq(parseTree), eq(event));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ private static Stream<Arguments> exceptionExpressionArguments() {
int testStringLength = random.nextInt(10);
String testString = RandomStringUtils.randomAlphabetic(testStringLength);
return Stream.of(
Arguments.of("/status + /message", event("{\"status\": 200, \"nomessage\":\"msg\"}")),
Arguments.of("/status - /message", event("{\"status\": 200, \"nomessage\":\"msg\"}")),
// Can't mix Numbers and Strings when using operators
Arguments.of("/status + /message", event("{\"status\": 200, \"message\":\"msg\"}")),
Arguments.of("/status / /message", event("{\"status\": 200, \"message\":\"msg\"}")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.params.provider.Arguments.arguments;

class GenericExpressionEvaluator_ConditionalIT {
Expand Down Expand Up @@ -119,7 +118,7 @@ void testGenericExpressionEvaluatorWithMultipleThreads(final String expression,
void testGenericExpressionEvaluatorThrows(final String expression, final Event event) {
final GenericExpressionEvaluator evaluator = applicationContext.getBean(GenericExpressionEvaluator.class);

assertThrows(RuntimeException.class, () -> evaluator.evaluateConditional(expression, event));
assertThat(evaluator.evaluateConditional(expression, event), equalTo(false));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior does not seem correct.

}

private static Stream<Arguments> validExpressionArguments() {
Expand Down Expand Up @@ -151,6 +150,7 @@ private static Stream<Arguments> validExpressionArguments() {
arguments("/status_code == 200", event("{\"status_code\": 200}"), true),
arguments("/status_code == 200", longEvent, true),
arguments("/status_code != 300", event("{\"status_code\": 200}"), true),
arguments("/status_code >= 300", event("{\"status_code_not_present\": 200}"), false),
arguments("/status_code == 200", event("{}"), false),
arguments("/success == /status_code", event("{\"success\": true, \"status_code\": 200}"), false),
arguments("/success != /status_code", event("{\"success\": true, \"status_code\": 200}"), true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.UUID;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -75,7 +76,7 @@ void testGivenParserThrowsExceptionThenExceptionThrown() {

doThrow(new RuntimeException()).when(parser).parse(eq(statement));

assertThrows(ExpressionEvaluationException.class, () -> statementEvaluator.evaluateConditional(statement, null));
assertThat(statementEvaluator.evaluateConditional(statement, null), equalTo(false));

verify(parser).parse(eq(statement));
verify(evaluator, times(0)).evaluate(any(), any());
Expand All @@ -90,7 +91,7 @@ void testGivenEvaluatorThrowsExceptionThenExceptionThrown() {
doReturn(parseTree).when(parser).parse(eq(statement));
doThrow(new RuntimeException()).when(evaluator).evaluate(eq(parseTree), eq(event));

assertThrows(ExpressionEvaluationException.class, () -> statementEvaluator.evaluateConditional(statement, event));
assertThat(statementEvaluator.evaluateConditional(statement, event), equalTo(false));

verify(parser).parse(eq(statement));
verify(evaluator).evaluate(eq(parseTree), eq(event));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public Collection<Record<Event>> doExecute(final Collection<Record<Event>> recor
.addArgument(entry.getValueExpression())
.addArgument(entry.getFormat())
.addArgument(entry.getValue())
.setCause(e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should include the cause whenever we catch Exception. Please add a new catch for the known exception(s) when these errors arise.

e.g.

catch (InvalidExpressionException e) {
  // log without exception
} catch (Exception e) {
  // log with the exception
}

.log();
}
}
Expand Down
Loading