Skip to content

JSONObject and JSONArray initial flag for tokener may be flawed #944

Closed

Description

@stleary

From #927:

The boolean isInitial = x.getPrevious() == 0; check can behave in an unexpected way, for example:
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
JSONTokener tok = new JSONTokener("[]xxx");
// Calling next+back changes "previous"; without these calls the check is performed:
tok.next();
tok.back();
JSONArray a1 = new JSONArray(tok, strict); // No error here, but should be because of extra chars?

This is a far corner case I think, but should be fixed if practical.
Is there a way to capture this in the JSONTokener object?

Activity

  1. Md-Yasir commented on Oct 11, 2025

    @Md-Yasir
    Contributor

    I am looking into this @stleary and this is going to be my first open-source contribution. 😙

  2. stleary commented on Oct 11, 2025

    @stleary
    OwnerAuthor

    @Md-Yasir This might be a tough issue to start with; there may not be a solution that works without breaking existing functionality.

    You are also welcome to start working on some issues that are more approachable. SonarQube has listed > 1000 issues to be addressed. Most of them are in the test files and not a priority. But about 100 issues are in the production code. Most are easy to fix and test. A few will need to be marked as false positive.


    All Open issues

    To help get you started, this is how previous work has gone:

    False positives (Don't work on these type of issues)
    Fixed (How similar issues were fixed)

  3. Md-Yasir commented on Oct 11, 2025

    @Md-Yasir
    Contributor

    Got it, So should I use Github or sonarqube for the updates? I haven't used sonarqube yet. Thanks

  4. stleary commented on Oct 11, 2025

    @stleary
    OwnerAuthor

    All work should be done in GitHub. Just use SonarQube as a reporting tool, to identify the issues to fix.

  5. Md-Yasir commented on Oct 11, 2025

    @Md-Yasir
    Contributor

    Understood.. Here is my first pull req

  6. XIAYM-gh commented on Sep 2, 2026

    @XIAYM-gh
    Contributor

    After viewing the current code in master branch, I found that the isInitial variable is used to determine whether the object/array is the root, and to determine whether to throw the error.

    I have an immature idea: why not just use the current index of the tokener?

    Hmm, I will test this locally. If it passes the test suite, and hopefully, doesn't break the compatibility, maybe I can open a pull request for reviewing.

  7. XIAYM-gh commented on Sep 2, 2026

    @XIAYM-gh
    Contributor

    After viewing the current code in master branch, I found that the isInitial variable is used to determine whether the object/array is the root, and to determine whether to throw the error.

    I have an immature idea: why not just use the current index of the tokener?

    Hmm, I will test this locally. If it passes the test suite, and hopefully, doesn't break the compatibility, maybe I can open a pull request for reviewing.

    Well, no. This changes the internal behaviour!

    I will search for a more reasonable solution...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions