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

Merged
merged 4 commits into from
Dec 13, 2024

Conversation

kkondaka
Copy link
Collaborator

Description

Fix null pointer exception during exception evaluation.
Modified to check for null objects before evaluating expressions.
Modified to catch exception in conditional expression evaluation and return false instead of throwing the exception

Issues Resolved

Resolves #[Issue number to be closed when this PR is merged]

Check List

  • [ X] New functionality includes testing.
  • New functionality has a documentation issue. Please link to it in this PR.
    • New functionality has javadoc added
  • [X ] Commits are signed with a real name per the DCO

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@@ -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
}

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.

@@ -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.

@@ -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.

Signed-off-by: Krishna Kondaka <[email protected]>
@@ -112,6 +112,18 @@ public void visitErrorNode(ErrorNode node) {
throw new RuntimeException("Hit error node in the parse tree: " + node.getText());
}

private boolean isBooleanOperator(Operator<?> op) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this will scale. We should add this as a method in Operator that needs to be implemented. This way, each implementation is sure to have some value for it.

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 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.

Arguments.of("/status - ", event("{\"status\": 200, \"message\":\"msg\"}")),
Arguments.of("/status / ", event("{\"status\": 200, \"message\":\"msg\"}")),
Arguments.of(" * /status ", event("{\"status\": 200, \"message\":\"msg\"}"))
//Arguments.of("-/message ", event("{\"status\": 200, \"message\":\"msg\"}"))
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove this commented out code.

Arguments.of("/status - ", event("{\"status\": 200, \"message\":\"msg\"}")),
Arguments.of("/status / ", event("{\"status\": 200, \"message\":\"msg\"}")),
Arguments.of(" * /status ", event("{\"status\": 200, \"message\":\"msg\"}")),
//Arguments.of("/status - ", event("{\"status\": 200, \"message\":\"msg\"}")),
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove this commented out code.

* @since 2.11
* Wrapper exception for any exception thrown while evaluating a statement
*/
public class ExpressionParsingException extends RuntimeException {
Copy link
Member

Choose a reason for hiding this comment

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

I like having this specific exception. But, I think we should have a common parent, that parent should probably remain ExpressionEvaluationException.

Suggested change
public class ExpressionParsingException extends RuntimeException {
public class ExpressionParsingException extends ExpressionEvaluationException {

We could possibly add other sub-classes to ExpressionEvaluationException for other cases in the future as well.

return evaluator.evaluate(parseTree, context);
}
catch (final Exception exception) {
throw new ExpressionEvaluationException("Unable to evaluate statement \"" + statement + "\"", exception);
if (parsedSuccessfully) {
Copy link
Member

Choose a reason for hiding this comment

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

You can avoid the boolean. This should also be easier to maintain as we clearly distinguish the steps.

final ParseTree parseTree = parser.parse(statement);
try {
  parseTree = parser.parse(statement);
} catch (final Exception exception) {
  throw new ExpressionParsingException("Unable to parse statement \"" + statement + "\"", exception);
}

try {
  return evaluator.evaluate(parseTree, context);
} catch (final Exception exception) {
  throw new ExpressionEvaluationException("Unable to evaluate statement \"" + statement + "\"", exception);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I thought about this but felt one try catch may be better. Anyways, changed to this.

if (result instanceof Boolean) {
return (Boolean) result;
}
throw new ClassCastException("Unexpected expression return value of " + result);
Copy link
Member

Choose a reason for hiding this comment

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

Since this PR raises the idea of other exceptions, this is a good candidate for a better exception.

Declare:

public class UnexpectedEvaluatedReturnTypeException extends ExpressionEvaluationException {

And then throw:

throw new UnexpectedEvaluatedReturnTypeException("Unexpected expression return value of " + result);

We don't have to do for this PR, but it would be a good improvement.

Signed-off-by: Krishna Kondaka <[email protected]>
@dlvenable dlvenable marked this pull request as ready for review December 12, 2024 22:25
@dlvenable dlvenable merged commit 3a684f0 into opensearch-project:main Dec 13, 2024
43 of 47 checks passed
sb2k16 pushed a commit to sb2k16/data-prepper that referenced this pull request Dec 13, 2024
…oject#5217)

Fix null pointer exception during exception evaluation

Signed-off-by: Krishna Kondaka <[email protected]>
sb2k16 pushed a commit to sb2k16/data-prepper that referenced this pull request Dec 13, 2024
…oject#5217)

Fix null pointer exception during exception evaluation

Signed-off-by: Krishna Kondaka <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants