NullPointerException in JSONObject(JSONObject, String...) and JSONObject(Object, String...) when names is null
まだ誰も着手していません。
評価
調査の方向性
issue でリンクされている行にある、src/main/java/org/json/JSONObject.java の該当する2つのコンストラクターを読んでください。明示的な this(...) 呼び出しが null の names 配列をどのように扱うかを確認し、両方のコンストラクターオーバーロードを対象としたテストを実行するか追加してください。null に対する動作が一貫して選択され、Javadoc に記載され、両方のコンストラクターで検証されたら完了です。
索引モデルが issue の本文から書いたものです。
説明
Description
Both "subset" constructors dereference names.length in their explicit constructor call (this(names.length)) before any validation, so passing a null array throws a raw NullPointerException instead of a meaningful error (or being treated as "no names").
Affected constructors:
JSONObject(JSONObject jo, String... names)-JSONObject.java:183JSONObject(Object object, String... names)-JSONObject.java:491
Because the dereference happens inside this(...), it cannot be guarded with a null check or try/catch within the constructor body. The try/catch inside the loop of the first constructor does not help either, since the NPE is thrown before the loop is reached.
Steps to reproduce
JSONObject jo = new JSONObject().put("a", 1);
new JSONObject(jo, (String[]) null); // NPE at JSONObject.java:183
new JSONObject(new Object(), (String[]) null); // NPE at JSONObject.
This can happen in practice when the array comes from a variable or a meth returns null (an omitted varargs argument is an empty array, but an explicit null is passed through as null).
Actual behavior
java.lang.NullPointerException: Cannot read the array length because "name at org.json.JSONObject.<init>(JSONObject.java:183)
Expected behavior
One of the following, applied consistently and documented in the Javadoc:
1. Treat null like an empty array and return an empty JSONObject, or
2. Throw an explicit NullPointerException / IllegalArgumentException with message (e.g. "names must not be null").
A possible minimal fix is to avoid dereferencing names in the this(...) call, e.g.:
this(names == null ? 0 : names.length);
if (names == null) {
return;
}
Environment
- org.json:json: 20260814 (also present in earlier versions, e.g. 20250517
- Java: OpenJDK 21.0.8
- OS: Linux
- 主要言語
- Java
- スター
- 4.7k
- フォーク
- 2.6k
- 平均マージ
- 4日 17時間
- マージ済み PR(30日)
- 7
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
stleary/JSON-java のほかの issue
-
JSONObject.toString does not use an Enum's `toString()` but its `name()`再び着手できるかも @XIAYM-gh が 52 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンFix before the next release
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
JSONML.toString() and XML.toString() omit escaping for literal newlines (\n), causing whitespace loss in downstream XML parsers (Regression from #1070)対応中かも @Lyin-OTTF が 5 日前に担当しました。 オープンFix before the next release
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
-
Security: fromJson() performs unrestricted reflective field binding (mass assignment); Javadoc claims a non-existent allowlist対応中かも @Lyin-OTTF が 5 日前に担当しました。 オープンActive discussion
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
-
JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cycles再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンActive discussion
難易度 4/5 3〜5日 初心者へのやさしさ 56/100
stleary/JSON-java の issue をすべて見る
似ている issue
-
staticAssetsPath follows symlinks outside the directory on Windows対応中かも @ybelMekk が今日担当しました。 オープンbug security findings
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
navikt/mock-oauth2-server#1132 ·
メンテナーはふだん 1 日以内に返信
-
agent:working follow-up
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
Sanne/incus-spawn#1683 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
checkstyle/checkstyle#22210 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
google/error-prone#6239 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[Failing Test]: BigqueryClient.queryUnflattened fails BigQuery ITs on a transient 503対応中かも @mxtymoshyk が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
メンテナーはふだん 1 日以内に返信