NullPointerException in JSONObject(JSONObject, String...) and JSONObject(Object, String...) when names is null
還沒有人認領這個 Issue。
評估
研究方向
閱讀 src/main/java/org/json/JSONObject.java 中受影響的兩個建構子,查看 issue 連結所指向的行。檢查它們明確呼叫的 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 小時
- 30 天內合併 PR
- 7
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
stleary/JSON-java 的其他 Issue
-
JSONObject.toString does not use an Enum's `toString()` but its `name()`可能重新可做 @XIAYM-gh 於 52 天前認領,目前沒有進行中的 PR。 未關閉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 一週以上 新手友好度 35/100
-
JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cycles可能重新可做 關聯的 PR 已關閉且未合併。 未關閉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 天內回覆