Skip to content

Commit

Permalink
WW-4062 Further optimisation of OgnlException caching
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Aug 13, 2024
1 parent 8d07694 commit 0fd8551
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
16 changes: 11 additions & 5 deletions core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,8 @@ private Object toTree(String expr) throws OgnlException {
tree = expressionCache.get(expr);
}
if (tree instanceof OgnlException) {
// OgnlException was cached, rethrow it with updated stack trace
OgnlException e = (OgnlException) tree;
e.getCause().fillInStackTrace();
throw e;
// OgnlException was cached, rethrow it with empty stack trace (refilling the stack trace is expensive)
clearStackTraceAndRethrow(tree);
}
if (tree == null) {
try {
Expand All @@ -621,13 +619,21 @@ private Object toTree(String expr) throws OgnlException {
throw (OgnlException) tree;
}
}

if (EXPR_BLOCKED.equals(tree)) {
throw new OgnlException("Expression blocked by OgnlGuard: " + expr);
}
return tree;
}

private void clearStackTraceAndRethrow(Object ognlException) throws OgnlException {
OgnlException e = (OgnlException) ognlException;
e.setStackTrace(new StackTraceElement[0]);
if (e.getCause() != null) {
e.getCause().setStackTrace(new StackTraceElement[0]);
}
throw e;
}

public Object compile(String expression, Map<String, Object> context) throws OgnlException {
Object tree = toTree(expression);
checkEnableEvalExpression(tree, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1650,14 +1650,15 @@ public void testCompilationErrorsCached() throws Exception {
StackTraceElement[] stackTrace = e.getStackTrace();
assertThat(stackTrace).isEmpty();
StackTraceElement[] causeStackTrace = e.getCause().getStackTrace();
assertThat(causeStackTrace).isNotEmpty();

OgnlException e2 = assertThrows(OgnlException.class, () -> ognlUtil.compile(".literal.$something"));
StackTraceElement[] stackTrace2 = e.getStackTrace();
StackTraceElement[] stackTrace2 = e2.getStackTrace();
assertThat(stackTrace2).isEmpty();
StackTraceElement[] causeStackTrace2 = e.getCause().getStackTrace();
StackTraceElement[] causeStackTrace2 = e2.getCause().getStackTrace();

assertThat(causeStackTrace2).isEmpty(); // Stack trace cleared before rethrow
assertSame(e, e2); // Exception is cached
assertThat(causeStackTrace).isNotEqualTo(causeStackTrace2); // Stack trace refreshed
}

/**
Expand Down

0 comments on commit 0fd8551

Please sign in to comment.