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

AviatorString类compare方法报空指针异常 #31

Closed
jakiewang opened this issue Sep 18, 2017 · 2 comments
Closed

AviatorString类compare方法报空指针异常 #31

jakiewang opened this issue Sep 18, 2017 · 2 comments
Assignees
Labels

Comments

@jakiewang
Copy link

jakiewang commented Sep 18, 2017

AviatorString类的compare方法未考虑this.lexeme为null以及otherString.lexeme为null的情况。这导致以下两种不符合预期的case

  • 空指针异常
  • 如果this.lexeme 为null,与NIL做比较时会错误的返回1,预期值应该是0
    可能导致空指针异常的代码有以下两处
  return this.lexeme.compareTo(otherString.lexeme); 
  return this.lexeme.compareTo(String.valueOf(otherJavaValue));

该代码在this.lexeme为null,或者otherString.lexeme为null时,均会抛出空指针异常。

下面是我用到的测试case

public class StringGetFromJsonMapFunction extends AbstractFunction {

    @Override
    public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
        //blablala
       //始终返回null
        return new AviatorString(null);
    }

    @Override
    public String getName() {
        return "string.getFromJsonMap";
    }
}

进行如下测试

@Test
    public void testNullPointerException() {
        String exp = "string.getFromJsonMap(mapJson, key) == 'abc'";
        Map<String, Object> env = Maps.newHashMap();
        env.put("mapJson", "{\"key1\":\"value1\", \"key2\":\"value2\"}");
        env.put("key", "key3");  
       //预期返回false,实际上却抛出了空指针异常。
        assertEquals(false, (Boolean)AviatorEvaluator.execute(exp, env));
    }

 @Test
    public void testNil() {
        String exp = "string.getFromJsonMap(mapJson, key) == nil";
        Map<String, Object> env = Maps.newHashMap();
        env.put("mapJson", "{\"key1\":\"value1\", \"key2\":\"value2\"}");
        env.put("key", "key3");  
        //预期返回true,实际上却返回false。
        assertEquals(true, (Boolean)AviatorEvaluator.execute(exp, env));
    }

附AviatorString的compare方法源码 (Aviator 3.0.0版本)

 public int compare(AviatorObject other, Map<String, Object> env) {
        if (this == other) {
            return 0;
        }
        switch (other.getAviatorType()) {
        case String:
            AviatorString otherString = (AviatorString) other;
            return this.lexeme.compareTo(otherString.lexeme);   //可能抛出空指针
        case JavaType:
            AviatorJavaType javaType = (AviatorJavaType) other;
            final Object otherJavaValue = javaType.getValue(env);
            if (otherJavaValue == null) {
                return 1;
            }
            if(TypeUtils.isString(otherJavaValue)) {
                return this.lexeme.compareTo(String.valueOf(otherJavaValue)); //可能抛出空指针
            }
            else if (otherJavaValue instanceof Date) {
                return this.tryCompareDate((Date) otherJavaValue);
            }
            else {
                throw new ExpressionRuntimeException("Could not compare " + this + " with " + other);
            }
        case Nil:  
            return 1;  //该分支未考虑this.lexeme为null的情况
        default:
            throw new ExpressionRuntimeException("Could not compare " + this + " with " + other);
        }
    }

不知道我的理解是否正确,麻烦看一下这个问题,谢谢。

@killme2008 killme2008 self-assigned this Nov 21, 2017
@killme2008 killme2008 added the bug label Nov 21, 2017
@killme2008
Copy link
Owner

这是个 bug,争取尽快修复下。

@killme2008
Copy link
Owner

已发布 3.1.1 , maven 仓库同步可能需要一到两天,感谢反馈。

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