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 于 53 天前认领,目前没有进行中的 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
-
pdfium-drift
难度 1/5 1 小时以内 新手友好度 88/100
Stirling-Tools/JPDFium#188 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
openremote/openremote#3487 · 1 条评论 ·
-
Type/Bug U2
难度 2/5 1-3 小时 新手友好度 90/100
wso2/product-is#28675 ·
维护者通常 1 天内回复
-
a:bug
难度 2/5 1-3 小时 新手友好度 83/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复