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

gsub's implementation causes stack overflows #314

Open
Jiehong opened this issue Jun 5, 2023 · 0 comments
Open

gsub's implementation causes stack overflows #314

Jiehong opened this issue Jun 5, 2023 · 0 comments

Comments

@Jiehong
Copy link

Jiehong commented Jun 5, 2023

Hello,

Using jackson-jq v0.0.13 (happens in v1.0 too), the implementation of gsub is prone to stack overflows.

Here is a reproducer:

  @Test
  public void gsubStackoverflow() throws IOException {
    final Scope rootScope = Scope.newEmptyScope();
    rootScope.loadFunctions(Thread.currentThread().getContextClassLoader());
    final Scope childScope = Scope.newChildScope(rootScope);

    final String s = IntStream.range(0, 3500).mapToObj(i -> Integer.valueOf(i).toString()).collect(Collectors.joining(","));
    final String json = String.format("\"%s\"", s);
    final JsonQuery query = JsonQuery.compile(". | gsub(\",\";\", \")");
    final JsonNode input = new ObjectMapper().readTree(json);
    query.apply(childScope, input);
  }

(3500 is just the value that triggers it on my machine, but the value can be a lot lower depending on the available memory. This only simulates a long string containing "1,2,3..." and where gsub is used to add a space after each comma)

And the result is:

java.lang.StackOverflowError
	at net.thisptr.jackson.jq.internal.functions._SubImplFunction.concat(_SubImplFunction.java:50)
	at net.thisptr.jackson.jq.internal.functions._SubImplFunction.concat(_SubImplFunction.java:63)
	at net.thisptr.jackson.jq.internal.functions._SubImplFunction.concat(_SubImplFunction.java:63)
	at net.thisptr.jackson.jq.internal.functions._SubImplFunction.concat(_SubImplFunction.java:63)
        (...cut for brievety...)

The code seems to indeed show a stack is used: https://github.com/eiiches/jackson-jq/blob/master/0.x/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/functions/_SubImplFunction.java#L62

In v1, the code hasn't changed much, and should still suffer the same fate: https://github.com/eiiches/jackson-jq/blob/develop/1.x/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/functions/_SubImplFunction.java#L74

In this specific case, there is a workaround: replace gsub(","; ", ") by split(",") | join(", "), but gsub shouldn't blow up like that.

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