|
Meta tags:
description=JSONPointer.readByIndexToken (src/main/java/org/json/JSONPointer.java:226) resolved an array element with Integer.parseInt(indexToken). RFC 6901 section 4 restricts an array index much more narrowly:
array-index = %x30 / ( %x31-39 *(%x30-39) )
i.e. 0 , or a digit string whose first digit is 1-9. Integer.parseInt is more permissive than that grammar, so tokens that are not array indexes were accepted whenever Java could read them as a number.
Reproducer
JSONArray array = new JSONArray( [10,20,30,40,50,60] );
array.query( /0 ); // 10
array.query( /01 ); // 20 - returns a different element instead of failing
array.query( /00 ); // 10
array.query( /+1 ); // 20
array.optQuery( /01 ); // 20
/01 does 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: /0 and /01 name different tokens yet resolve to the same value, and /00//01 silently select element 0/1. +1 is not matched by the grammar either (it is not even a digit string), and a token longer than Integer.MAX_VALUE reported 2147483648 is not an array index for what is really an out-of-range index.
Fix
Added toArrayIndex(), which parses the token only when it matches the array-index production, and used it in readByIndexToken(). Parsing moved out of the try block whose catch (NumberFormatException) was what made Integer.parseInt the de-facto grammar in the first place; the out of range check and its message are unchanged.
/0, /1, ... behave exactly as before.
Out of range indexes keep their existing message (index 6 is out of bounds - the array has 6 elements).
Tokens that are not array indexes raise JSONPointerException (01 is not an array index), which is what JSONArrayTest/JSONPointerTest already expect for non-index tokens in an array (a is not an array index).
Divergence with a readable reference: Jackson s JsonPointer._parseIndex (jackson-core 2.18.2) rejects 01, 00, +1, -1, 1.0, -0, an Arabic-Indic 1 and 0000000000000000001, and accepts 0 and 1. After this change json-java agrees on every one of those tokens.
Member names of a JSONObject are not array indexes and are untouched: /01 still addresses the member named 01 (the library has no equivalent of #-escaping a numeric key for arrays).
Testing
New regression test JSONPointerTest.arrayIndexTokenMustMatchRfc6901Grammar covers the accepted indexes, the rejected spellings, and the optQuery result.
RED on the base commit, with the new test in place but JSONPointer.java reverted:
Tests run: 43, Failures: 1, Errors: 0
JSONPointerTest.arrayIndexTokenMustMatchRfc6901Grammar:325
Expected JSONPointerException for array index token 01 , got 20
GREEN after the change: mvn test → Tests run: 840, Failures: 0, Errors: 0, Skipped: 6, BUILD SUCCESS (JDK 17 locally, same version CI uses for build-17).
No existing test asserted the old acceptance of 01 / 00 ; the full suite is unaffected.;
Headings (most frequently used words):
uh, oh, navigation, jsonpointer, reject, array, index, tokens, that, rfc, 6901, does, not, allow, 1098, footer, diff, view, menu, repository, there, are, no, files, selected, for, viewing, filter, by, extension, sonarqubecloud, sonarcloud, code, analysis,
Text of the page (most frequently used words):
index (33), array (28), the (19), file (19), java (19), not (17), and (17), indextoken (17), github (16), code (16), for (16), this (15), that (14), jsonpointerexception (14), applied (11), new (11), line (10), reload (9), token (9), org (9), json (9), jsonpointer (9), cannot (8), all (8), string (8), show (8), throw (8), currentarr (8), you (7), suggestion (7), suggestions (7), while (7), loading (7), jsonarray (7), does (7), sign (7), changes (6), error (6), section (6), rfc (6), 6901 (6), diff (6), hidden (6), characters (6), unicode (6), open (6), src (6), format (6), length (6), security (5), please (5), commit (5), there (5), was (5), query (5), with (5), tokens (5), number (5), view (5), first (5), your (5), can (4), pull (4), comments (4), page (4), expand (4), catch (4), value (4), try (4), test (4), see (4), rfc6901 (4), x30 (4), bidirectional (4), learn (4), more (4), about (4), return (4), integer (4), position (4), int (4), 1098 (4), main (4), throws (4), out (4), files (4), simpleqt (4), reject (4), allow (4), support (4), copilot (4), community (3), navigation (3), check (3), request (3), from (3), has (3), change (3), add (3), batch (3), one (3), closed (3), retry (3), expected (3), null (3), which (3), failed (3), contains (3), object (3), https (3), are (3), than (3), what (3), review (3), junit (3), jsonpointertest (3), charat (3), issues (3), bounds (3), current (3), hide (3), whitespace (3), filter (3), commits (3), stleary (3), account (3), quality (3), another (3), tab (3), window (3), refresh (3), session (3), developers (3), changelog (3), customer (3), enterprise (3), grade (3), features (3), build (3), contact (2), status (2), privacy (2), terms (2), footer (2), 2026 (2), merge (2), must (2), valid (2), single (2), viewing (2), toggle (2), annotations (2), down (2), asserttrue (2), optquery (2), getmessage (2), message (2), got (2), fail (2), assertequals (2), public (2), void (2), href (2), tools (2), ietf (2), html (2), instead (2), different (2), element (2), into (2), start (2), zero (2), x31 (2), such (2), restricts (2), digit (2), 306 (2), original (2), text (2), may (2), interpreted (2), compiled (2), differently (2), appears (2), below (2), editor (2), reveals (2), desktop (2), delete (2), edit (2), additions (2), deletions (2), parseint (2), reading (2), jsonexception (2), get (2), sonarcloud (2), project (2), aagngq_8o8gen74zqnw1 (2), analysis (2), private (2), static (2), toarrayindex (2), ordinal (2), grammar (2), its (2), valueof (2), elements (2), 224 (2), selected (2), apply (2), split (2), unified (2), load (2), jump (2), extension (2), tree (2), issue (2), insights (2), projects (2), actions (2), repository (2), signed (2), universe (2), training (2), integrations (2), stories (2), center (2), documentation (2), business (2), fixes (2), products (2), secret (2), perform, action, time, share, personal, information, manage, cookies, docs, inc, right, now, back, later, queued, multi, pending, reviews, been, marked, resolved, outdated, existing, order, create, applying, deleted, lines, supported, only, per, subset, invalid, because, were, made, notes, oops, something, went, wrong, coverage, jsonobject, same, rule, applies, reports, 0000000000000000001, arrayindextokenmustmatchrfc6901grammar, them, silently, resolving, therefore, indexes, navigating, tokenlistiscopiedinconstructor, numberformatexception, stleary_json, pullrequest, define, constant, duplicating, literal, times, sonarqubecloud, run, annotation, failure, 255, char, denotes, param, form, they, read, asked, rejected, spellings, accepts, but, converts, readbyindextoken, thrown, conversations, clear, filters, viewed, type, nbsp, nov, 33e9634, select, changed, checks, conversation, wants, fix, master, already, clicking, agree, our, occasionally, send, related, emails, statement, service, free, maintainers, have, question, items, wiki, requests, dismiss, alert, switched, accounts, appearance, settings, search, pricing, event, talks, workshops, next, partners, company, forum, events, webinars, blog, resource, connect, resources, premium, advanced, ons, trust, why, evaluate, industries, government, manufacturing, financial, services, healthcare, industry, nonprofits, startups, small, medium, teams, enterprises, stage, businesses, source, latest, improvements, across, place, developer, articles, sponsors, trending, repos, explore, customize, certifications, scanning, leak, prevention, protection, vulnerability, detection, remediation, secure, cloud, development, environments, codespaces, enforcement, automation, ship, coding, agent, tasks, automated, work, hosting, collaboration, platform, menu, skip, content,
|