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

[BUG] 不当的 lambda 表达式会导致 StackOverFlow #184

Closed
Blax11 opened this issue Dec 21, 2019 · 3 comments
Closed

[BUG] 不当的 lambda 表达式会导致 StackOverFlow #184

Blax11 opened this issue Dec 21, 2019 · 3 comments
Assignees
Labels

Comments

@Blax11
Copy link

Blax11 commented Dec 21, 2019

不当的表达式可能会导致应用崩溃
代码

@Data
class Alarm {
    String appName;
    String source;
    String alarmTime;
    Integer times;
    Integer alarmLevel;

    Map<String, Object> env = new HashMap<>();

}
@Test
    public void testLambda02() {
        Alarm alarm = new Alarm();
        alarm.getEnv().put("requestPath", "/test");
        alarm.getEnv().put("status", "404");
        alarm.getEnv().put("appName", "rule-test");
        alarm.getEnv().put("source", "nginx");
        alarm.getEnv().put("times", 100);
        alarm.getEnv().put("alarmLevel", 1);
        // AviatorEvaluatorInstance 多实例
        AviatorEvaluatorInstance instance = AviatorEvaluator.newInstance();
        String expression = "(lambda (x) -> lambda(y) -> lambda(z) -> x + y + z end end end)(appName)";
        System.out.println(instance.execute(expression, alarm.getEnv(), true));

    }

运算结果死循环

java.lang.StackOverflowError
	at com.googlecode.aviator.runtime.type.AviatorObject.toString(AviatorObject.java:39)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at com.googlecode.aviator.runtime.type.AviatorObject.desc(AviatorObject.java:73)
	at com.googlecode.aviator.runtime.type.AviatorObject.toString(AviatorObject.java:39)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at com.googlecode.aviator.runtime.type.AviatorObject.desc(AviatorObject.java:73)
	at com.googlecode.aviator.runtime.type.AviatorObject.toString(AviatorObject.java:39)
	at java.lang.String.valueOf(String.java:2994)
@killme2008 killme2008 added the bug label Dec 23, 2019
@killme2008 killme2008 self-assigned this Dec 23, 2019
@killme2008
Copy link
Owner

比较怪,我调查下

@killme2008
Copy link
Owner

killme2008 commented Dec 23, 2019

这个其实不是表达式的问题,而是因为 AviatorObject#toString 在遇到是函数的情况下递归(desc 和 toString 递归调用)了,会修复下。

遇到返回值是 lambda 的情况,不调用 toString 是不会遇到这个问题的。这里 System.out.println 默认调用了 toString 方法。改成类似:

  Object s = (instance.execute(expression, env, true));

就不会报错了。

@killme2008
Copy link
Owner

发布了 4.2.9(可能要等一天等 maven central 同步)
https://github.com/killme2008/aviator/releases/tag/aviator-4.2.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants