From fcf8bf6478029a3e2c52871b9c67a7f310c7466a Mon Sep 17 00:00:00 2001 From: sdmms1 <58692911+sdmms1@users.noreply.github.com> Date: Thu, 4 Nov 2021 16:48:48 +0800 Subject: [PATCH] Fix the issue 629 and add two testcases (#698) --- .../jsonpath/internal/path/PathCompiler.java | 15 +++++++++ .../jsonpath/internal/function/Issue629.java | 32 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue629.java 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:")); + } + } +}