Skip to content

Commit bc10106

Browse files
committed
修复 solon-expression 表达式遇到非法特殊符可能会 oom 的问题(重要)
1 parent 22cf533 commit bc10106

4 files changed

Lines changed: 56 additions & 6 deletions

File tree

UPDATE_LOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11

2+
### v3.10.1
3+
4+
* 修复 solon-expression 表达式遇到非法特殊符可能会 oom 的问题(重要)
5+
6+
7+
### v3.10.0
8+
9+
* 添加 `solon-expression` 添加 `@bean` 表达式支持
10+
11+
示例:
12+
13+
```java
14+
Map<String, Object> vars = new HashMap();
15+
vars.put("a", 1);
16+
17+
EnhanceContext context = new EnhanceContext(vars);
18+
context.forBeans(Solon.context()::getBean);
19+
20+
SnEL.eval("@user.getAge() == a ? true : false", context);
21+
```
22+
23+
24+
225
### v3.9.6
326

427
* 添加 `solon-expression` 添加 `@bean` 表达式支持

solon-expression-test/src/test/java/features/expr/SnelEvaluateParserNewFeatureTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ public void testCustomPropertyMarker2() {
7979

8080
customParser.parse("{test}", false);
8181

82-
// 场景 B: 新的 %{} 应当被识别为 PropertyNode (TemplateNode)
83-
// 只要不报错,说明 isPropertyStart 逻辑生效
8482
Assertions.assertDoesNotThrow(() -> {
83+
customParser.parse("{app.name}", false);
84+
});
85+
86+
Assertions.assertThrows(Throwable.class, () -> {
8587
customParser.parse("%{app.name}", false);
8688
});
8789
}

solon-expression-test/src/test/java/features/expr/issues/Issue_IHPAQ3.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package features.expr.issues;
22

33
import lombok.Data;
4+
import org.junit.jupiter.api.Assertions;
45
import org.junit.jupiter.api.Test;
56
import org.noear.solon.expression.context.EnhanceContext;
7+
import org.noear.solon.expression.exception.CompilationException;
68
import org.noear.solon.expression.snel.SnEL;
79

810
import java.io.Serializable;
@@ -47,7 +49,13 @@ public void test2() {
4749
EnhanceContext context = new EnhanceContext(vars);
4850
context.forBeans(beans::get);
4951

50-
System.out.println(SnEL.eval("@user.bb($uuu)", context));
52+
Assertions.assertThrows(CompilationException.class, () -> {
53+
SnEL.eval("@user.bb($uuu)", context);
54+
});
55+
56+
Assertions.assertThrows(CompilationException.class, () -> {
57+
SnEL.eval("@user.bb(%uuu)", context);
58+
});
5159
}
5260

5361

solon-expression/src/main/java/org/noear/solon/expression/snel/EvaluateParser.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,15 @@ private boolean isPropertyStart(ParserState state) {
172172
* 检查是否是包装表达式起始
173173
*/
174174
private boolean isExpressionStart(ParserState state) {
175-
return (state.getCurrentChar() == parser.MARK_START_EXPRESSION && state.peekNextChar() == parser.MARK_BRACE_OPEN)
176-
|| state.getCurrentChar() == parser.MARK_BRACE_OPEN;
175+
int cur = state.getCurrentChar();
176+
int next = state.peekNextChar();
177+
178+
// 只有明确看到 #{ 或 { 才认为是包装表达式的开始
179+
if (cur == parser.MARK_START_EXPRESSION) {
180+
return next == parser.MARK_BRACE_OPEN;
181+
}
182+
183+
return cur == parser.MARK_BRACE_OPEN;
177184
}
178185

179186
/**
@@ -360,7 +367,12 @@ private Expression parsePrimaryExpression(ParserState state) {
360367
expr = new ConstantNode(null);
361368
} else {
362369
String identifier = parseIdentifier(state);
363-
expr = new VariableNode(identifier);
370+
371+
if(identifier != null && identifier.length() > 0) {
372+
expr = new VariableNode(identifier);
373+
} else {
374+
throw state.error("Expression is invalid");
375+
}
364376
}
365377

366378
return parsePostfix(state, expr);
@@ -740,6 +752,11 @@ public int peekNextChar() {
740752
}
741753
}
742754

755+
public CompilationException error(String message) {
756+
String charDesc = (ch == -1) ? "EOF" : "'" + (char) ch + "'";
757+
return new CompilationException(message + " (found " + charDesc + ") at position " + position);
758+
}
759+
743760
/**
744761
* 跳过空白字符
745762
*/

0 commit comments

Comments
 (0)