NullPointerException in JSONObject(JSONObject, String...) and JSONObject(Object, String...) when names is null
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 76/100
Hướng nghiên cứu
Đọc hai hàm khởi tạo bị ảnh hưởng trong src/main/java/org/json/JSONObject.java, tại các dòng được liên kết trong issue. Kiểm tra cách các lệnh gọi this(...) tường minh xử lý mảng names có giá trị null, sau đó chạy hoặc bổ sung kiểm thử tập trung cho cả hai phiên bản quá tải của hàm khởi tạo. Công việc được xem là hoàn tất khi cách xử lý null được chọn nhất quán, được ghi lại trong Javadoc và được xác minh cho cả hai hàm khởi tạo.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Java
- Star
- 4.7k
- Fork
- 2.6k
- Merge trung bình
- 4 ngày 17 giờ
- Pull request đã merge (30 ngày)
- 7
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của stleary/JSON-java
-
JSONObject.toString does not use an Enum's `toString()` but its `name()`Có thể làm lại được @XIAYM-gh đã nhận 53 ngày trước và không có pull request nào đang mở. Đang mởFix before the next release
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
JSONML.toString() and XML.toString() omit escaping for literal newlines (\n), causing whitespace loss in downstream XML parsers (Regression from #1070)Có thể đã có người làm @Lyin-OTTF đã nhận 5 ngày trước. Đang mởFix before the next release
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
-
`XML.unescape` rejects the character references `XML.escape` produces for control charactersĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
-
Security: fromJson() performs unrestricted reflective field binding (mass assignment); Javadoc claims a non-existent allowlistCó thể đã có người làm @Lyin-OTTF đã nhận 5 ngày trước. Đang mởActive discussion
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
-
JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cyclesCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởActive discussion
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 56/100
Tất cả issue của stleary/JSON-java
Issue tương tự
-
pdfium-drift
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
Stirling-Tools/JPDFium#188 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Manager flashes an empty "errorOccurred" dialog while redirecting a logged-out user to loginĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
openremote/openremote#3487 · 1 bình luận ·
-
Type/Bug U2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 90/100
wso2/product-is#28675 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
a:bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 87/100
apache/rocketmq-dashboard#7351 ·
Maintainer thường phản hồi trong vòng 3 ngày