Skip to content

JsonPointer.head() throws StringIndexOutOfBoundsException for valid JSON Pointers #1425

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

Closed
sergeykad opened this issue Apr 27, 2025 · 3 comments
Assignees
Labels
2.19 Issues planned at earliest for 2.19

Comments

@sergeykad
Copy link

Problem Description:
After upgrading to Jackson 2.19.0, valid JsonPointer strings are causing StringIndexOutOfBoundsException when calling head().
The crash happens due to a negative suffixLength calculated in _constructHead().
This was not an issue in previous versions (2.18.x and earlier).

Caused by: java.lang.StringIndexOutOfBoundsException: begin 0, end 14, length 6
    at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4606)
    at java.base/java.lang.String.substring(String.java:2709)
    at com.fasterxml.jackson.core.JsonPointer._constructHead(JsonPointer.java:853)
    at com.fasterxml.jackson.core.JsonPointer.head(JsonPointer.java:606)

Reproducer:

import com.fasterxml.jackson.core.JsonPointer;

public class JsonPointerBugTest {
    public static void main(String[] args) {
        JsonPointer head = JsonPointer.compile("/a/b/0/qwerty");
        head = head.head();
        head = head.head(); // Exception happens here
        System.out.println(head);
    }
}

Version:
Jackson Core 2.19.0

@pjfanning
Copy link
Member

I've reproduced this issue and it appears to be related to #1354.
The new code looks pretty complicated and I'm not sure that I even understand what it is doing.

@cowtowncoder
Copy link
Member

cowtowncoder commented Apr 28, 2025

Ok, first of all, thank you for reporting @sergeykad.

Too bad this was not found during release-candidate phase. But it is what it is.

FWTW, verified this does not fail on 2.18.3.

@cowtowncoder cowtowncoder added the 2.19 Issues planned at earliest for 2.19 label Apr 28, 2025
@cowtowncoder cowtowncoder changed the title JsonPointer.head() crashes with StringIndexOutOfBoundsException in 2.19.0 for valid JSON Pointers JsonPointer.head() throws StringIndexOutOfBoundsException for valid JSON Pointers Apr 28, 2025
@cowtowncoder cowtowncoder self-assigned this Apr 28, 2025
@cowtowncoder
Copy link
Member

Ok, figured it out. Indeed offset miscalculated, needed to change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.19 Issues planned at earliest for 2.19
Projects
None yet
Development

No branches or pull requests

3 participants