NullPointerException in JSONObject(JSONObject, String...) and JSONObject(Object, String...) when names is null
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 76/100
Línea de trabajo
Lee los dos constructores afectados en src/main/java/org/json/JSONObject.java, en las líneas enlazadas en la issue. Comprueba cómo gestionan sus llamadas explícitas a this(...) un array de nombres null y ejecuta o añade pruebas específicas para ambas sobrecargas del constructor. Se considera terminado cuando el comportamiento para null se haya elegido de forma coherente, esté documentado en Javadoc y se haya verificado para ambos constructores.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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
- Lenguaje dominante
- Java
- Estrellas
- 4.7k
- Forks
- 2.6k
- Merge medio
- 4 d 17 h
- PR fusionados (30 d)
- 7
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de stleary/JSON-java
-
JSONObject.toString does not use an Enum's `toString()` but its `name()`Quizá libre de nuevo @XIAYM-gh la tomó hace 52 días y no hay ningún pull request abierto. AbiertoFix before the next release
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
JSONML.toString() and XML.toString() omit escaping for literal newlines (\n), causing whitespace loss in downstream XML parsers (Regression from #1070)Posiblemente ocupada @Lyin-OTTF la tomó hace 5 días. AbiertoFix before the next release
Dificultad 3/5 1-2 días Aptitud para principiantes 45/100
-
`XML.unescape` rejects the character references `XML.escape` produces for control charactersAbierto
Dificultad 3/5 1-2 días Aptitud para principiantes 72/100
-
Security: fromJson() performs unrestricted reflective field binding (mass assignment); Javadoc claims a non-existent allowlistPosiblemente ocupada @Lyin-OTTF la tomó hace 5 días. AbiertoActive discussion
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
-
JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cyclesQuizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. AbiertoActive discussion
Dificultad 4/5 3-5 días Aptitud para principiantes 56/100
Todos los issues de stleary/JSON-java
Issues similares
-
staticAssetsPath follows symlinks outside the directory on WindowsPosiblemente ocupada @ybelMekk la tomó hoy. Abiertobug security findings
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
navikt/mock-oauth2-server#1132 ·
Los mantenedores suelen responder en 1 día
-
agent:working follow-up
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Sanne/incus-spawn#1683 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
checkstyle/checkstyle#22210 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
google/error-prone#6239 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
[Failing Test]: BigqueryClient.queryUnflattened fails BigQuery ITs on a transient 503Posiblemente ocupada @mxtymoshyk la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
Los mantenedores suelen responder en 1 día