diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java b/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java index 14d950f1..d0af7c66 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java @@ -207,6 +207,21 @@ public class PathCompiler { List functionParameters = null; if (isFunction) { + int parenthesis_count = 1; + for(int i = readPosition + 1; i < path.length(); i++){ + if (path.charAt(i) == CLOSE_PARENTHESIS) + parenthesis_count--; + else if (path.charAt(i) == OPEN_PARENTHESIS) + parenthesis_count++; + if (parenthesis_count == 0) + break; + } + + if (parenthesis_count != 0){ + String functionName = path.subSequence(startPosition, endPosition).toString(); + throw new InvalidPathException("Arguments to function: '" + functionName + "' are not closed properly."); + } + if (path.inBounds(readPosition+1)) { // read the next token to determine if we have a simple no-args function call char c = path.charAt(readPosition + 1); diff --git a/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue629.java b/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue629.java new file mode 100644 index 00000000..b522c7d4 --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue629.java @@ -0,0 +1,32 @@ +package com.jayway.jsonpath.internal.function; + +import com.jayway.jsonpath.JsonPath; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertTrue; + +public class Issue629 { + @Test + public void testUncloseParenthesis() throws IOException { + try { + JsonPath jsonPath = JsonPath.compile("$.A.B.C.D("); + assert(false); + } + catch (Exception e) { + assertTrue(e.getMessage().startsWith("Arguments to function:")); + } + } + + @Test + public void testUncloseParenthesisWithNestedCall() throws IOException { + try { + JsonPath jsonPath = JsonPath.compile("$.A.B.C.sum(D()"); + assert(false); + } + catch (Exception e) { + assertTrue(e.getMessage().startsWith("Arguments to function:")); + } + } +}