NullPointerException in JSONObject(JSONObject, String...) and JSONObject(Object, String...) when names is null
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Facilidade para iniciantes
- 76/100
Direção de pesquisa
Leia os dois construtores afetados em src/main/java/org/json/JSONObject.java, nas linhas vinculadas na issue. Confira como as chamadas explícitas a this(...) tratam um array de nomes null e, em seguida, execute ou adicione testes específicos para as duas sobrecargas do construtor. O trabalho estará concluído quando o comportamento para null tiver sido escolhido de forma consistente, documentado no Javadoc e verificado para os dois construtores.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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
- Linguagem predominante
- Java
- Estrelas
- 4.7k
- Forks
- 2.6k
- Merge médio
- 4d 17h
- PRs com merge (30d)
- 7
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de stleary/JSON-java
-
JSONObject.toString does not use an Enum's `toString()` but its `name()`Talvez livre de novo @XIAYM-gh assumiu há 53 dias e não há nenhum pull request aberto. AbertaFix before the next release
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
-
JSONML.toString() and XML.toString() omit escaping for literal newlines (\n), causing whitespace loss in downstream XML parsers (Regression from #1070)Talvez já em andamento @Lyin-OTTF assumiu há 5 dias. AbertaFix before the next release
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 45/100
-
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 72/100
-
Security: fromJson() performs unrestricted reflective field binding (mass assignment); Javadoc claims a non-existent allowlistTalvez já em andamento @Lyin-OTTF assumiu há 5 dias. AbertaActive discussion
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 35/100
-
JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cyclesTalvez livre de novo Um pull request para esta issue foi fechado sem ser mesclado. AbertaActive discussion
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 56/100
Todas as issues de stleary/JSON-java
Issues semelhantes
-
pdfium-drift
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 88/100
Stirling-Tools/JPDFium#188 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
openremote/openremote#3487 · 1 comentário ·
-
Type/Bug U2
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 90/100
wso2/product-is#28675 ·
Mantenedores costumam responder em até 1 dia
-
a:bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 83/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 87/100
apache/rocketmq-dashboard#7351 ·
Mantenedores costumam responder em até 3 dias