Repository navigation
Conversation
JSONPointer.readByIndexToken resolved array elements with Integer.parseInt(indexToken), so tokens which are not array indexes per RFC 6901 section 4 (array-index = %x30 / ( %x31-39 *(%x30-39) )) were accepted whenever Java's number parsing accepted them. On a JSONArray, "/01" therefore returned element 1 and "/00" returned element 0 instead of failing, i.e. a pointer that does not address the element it names silently selected a different one. The token "+1" was accepted as well, and a token longer than Integer.MAX_VALUE reported "2147483648 is not an array index" for what is really an out of range index. Parse the token only when it matches the grammar, in a new toArrayIndex() helper used by readByIndexToken(). Valid indexes such as "/0" and "/1" behave as before, out of range indexes keep their existing message, and non-conforming tokens now raise a JSONPointerException. Member names of a JSONObject are unaffected: they are not array indexes, so "/01" still addresses the member named "01". This matches Jackson's JsonPointer (com.fasterxml.jackson.core) and its JsonPointer._parseIndex(), which rejects exactly the same tokens.
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

JSONPointer.readByIndexToken(src/main/java/org/json/JSONPointer.java:226) resolved an array element withInteger.parseInt(indexToken). RFC 6901 section 4 restricts an array index much more narrowly:i.e.
"0", or a digit string whose first digit is 1-9.Integer.parseIntis more permissive than that grammar, so tokens that are not array indexes were accepted whenever Java could read them as a number.Reproducer
/01does not address the element at index 1 — it does not address any element, since the token is not an array index. Returning 20 hides a bug in the caller's pointer:/0and/01name different tokens yet resolve to the same value, and/00//01silently select element 0/1.+1is not matched by the grammar either (it is not even a digit string), and a token longer thanInteger.MAX_VALUEreported2147483648 is not an array indexfor what is really an out-of-range index.Fix
Added
toArrayIndex(), which parses the token only when it matches thearray-indexproduction, and used it inreadByIndexToken(). Parsing moved out of thetryblock whosecatch (NumberFormatException)was what madeInteger.parseIntthe de-facto grammar in the first place; the out of range check and its message are unchanged./0,/1, ... behave exactly as before.index 6 is out of bounds - the array has 6 elements).JSONPointerException(01 is not an array index), which is whatJSONArrayTest/JSONPointerTestalready expect for non-index tokens in an array (a is not an array index).JsonPointer._parseIndex(jackson-core 2.18.2) rejects01,00,+1,-1,1.0,-0, an Arabic-Indic1and0000000000000000001, and accepts0and1. After this change json-java agrees on every one of those tokens.JSONObjectare not array indexes and are untouched:/01still addresses the member named"01"(the library has no equivalent of#-escaping a numeric key for arrays).Testing
JSONPointerTest.arrayIndexTokenMustMatchRfc6901Grammarcovers the accepted indexes, the rejected spellings, and theoptQueryresult.JSONPointer.javareverted:mvn test→Tests run: 840, Failures: 0, Errors: 0, Skipped: 6,BUILD SUCCESS(JDK 17 locally, same version CI uses forbuild-17)."01"/"00"; the full suite is unaffected.