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

Compile expression failed with string escape sequence \" in List and Map #315

Open
gaox0326 opened this issue Nov 10, 2022 · 1 comment
Open

Comments

@gaox0326
Copy link

gaox0326 commented Nov 10, 2022

version: 2.4.14.Final

// compile string literals a\"b
MVEL.compileExpression("\"a\\\"b\""); // success
MVEL.compileExpression("[\"a\\\"b\"]"); // failed
MVEL.compileExpression("[\"key\":\"a\\\"b\"]"); // failed

It seems that CollectionParser don't handle string escape

[Error: unterminated string literal]
[Near : {... ["key":"a"b"] ....}]
^
[Line: 1, Column: 14]
at org.mvel2.util.ParseTools.balancedCapture(ParseTools.java:1413)
at org.mvel2.util.CollectionParser.parseCollection(CollectionParser.java:170)
at org.mvel2.util.CollectionParser.parseCollection(CollectionParser.java:93)
at org.mvel2.util.CollectionParser.parseCollection(CollectionParser.java:140)
at org.mvel2.util.CollectionParser.parseCollection(CollectionParser.java:76)
at org.mvel2.ast.InlineCollectionNode.parseGraph(InlineCollectionNode.java:111)
at org.mvel2.ast.InlineCollectionNode.(InlineCollectionNode.java:49)
at org.mvel2.compiler.AbstractParser.nextToken(AbstractParser.java:1268)
at org.mvel2.compiler.ExpressionCompiler._compile(ExpressionCompiler.java:127)
at org.mvel2.compiler.ExpressionCompiler.compile(ExpressionCompiler.java:68)
at org.mvel2.MVEL.compileExpression(MVEL.java:827)
at org.mvel2.MVEL.compileExpression(MVEL.java:836)
at org.mvel2.MVEL.compileExpression(MVEL.java:740)

@ruoyangryan
Copy link
Contributor

ruoyangryan commented Nov 16, 2022

I've taken a look at the method balancedCapture(char[] chars, int start, int end, char type) in ParseTools class, there is a code block like this:

if (type == term) {
  for (start++; start < end; start++) {
    if (chars[start] == type) {
      return start;
    }
  }
}

What it was trying to do is basically finding out the terminate of a String literal. But since it is reading the whole char[] element by element, it cannot identify the '\' and '"' as a combination but rather treats them separately. So it thinks the String "a/"b" terminated as "a/".

I've opened a pull request to fix this problem. Check #317

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

No branches or pull requests

2 participants