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

CompileException on seemingly valid MVEL. Adding two extra semi-colons after closing else if block curly brace resolves exception; one is not enough. #342

Open
ScottCUSA opened this issue Sep 20, 2023 · 1 comment

Comments

@ScottCUSA
Copy link

ScottCUSA commented Sep 20, 2023

MVEL throws a CompileException when passing valid MVEL to org.mvel2.MVEL.evalToString.

port = 'ae1'; 
if (port.contains('-')) {
    port = port.substring(port.indexOf('-') + 1);
} else if (port.startsWith('ae')) {
    port = port.replace('ae', 'lag-');
} 
port = port.toLowerCase();
return port;

This is the exception:

org.mvel2.CompileException: [Error: unexpected token or unknown identifier:port.toLowerCase()]
[Near : {... sWith('ae')) {port = port.replace('ae', 'lag-');} port = port.toLowerCase(); ....}]
                                                               ^
[Line: 1, Column: 156]
	at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:149) ~[jaxws-api-2.3.1.jar:?]
	at org.mvel2.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:58) ~[jaxws-api-2.3.1.jar:?]
	at org.mvel2.MVEL.eval(MVEL.java:170) ~[jaxws-api-2.3.1.jar:?]
	at org.mvel2.MVEL.evalToString(MVEL.java:349) ~[jaxws-api-2.3.1.jar:?]]
        ...

If I add two semi-colons to the end of the if block immediately proceeding the "unexpected token" it works without issue.

port = 'ae1'; 
if (port.contains('-')) {
    port = port.substring(port.indexOf('-') + 1);
} else if (port.startsWith('ae')) {
    port = port.replace('ae', 'lag-');
} ;; /* THESE TWO SEMI-COLONS RESOLVE COMPILE EXCEPTION */
port = port.toLowerCase();
return port;

result = lag-1

@ScottCUSA
Copy link
Author

ScottCUSA commented Sep 29, 2023

I attempted to determine what was going on by debugging the AbstractParser while running on the example code. I don't know enough about how it's supposed to work to determine how to fix it.

From what I can tell it looks like there's a "null" value on the parser stack when it should be empty prior to evaluating the expression port = port.toUpperCase()

When adding the two extra semi-colons it looks like the parser stack is cleared prior to it starting to parse port = port.toLowerCase();

https://github.com/mvel/mvel/blob/85125e1af5f3ec32990b1768b8656bfcf450b489/src/main/java/org/mvel2/compiler/AbstractParser.java

@ScottCUSA ScottCUSA changed the title CompileException on seemingly valid MVEL, adding extra semi-colons resolves exception CompileException seemingly valid MVEL. Adding two extra semi-colons after closing else if block curly brace resolves exception; one is not enough. Jan 9, 2024
@ScottCUSA ScottCUSA changed the title CompileException seemingly valid MVEL. Adding two extra semi-colons after closing else if block curly brace resolves exception; one is not enough. CompileException on seemingly valid MVEL. Adding two extra semi-colons after closing else if block curly brace resolves exception; one is not enough. Jan 9, 2024
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

1 participant