-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSONObject constructors do not check for non-finite numbers #713
Comments
Shall I work on this? if yes, please assign |
Closing due to lack of activity. Please post here if you think it should be reopened. |
Why not apply my suggested fix instead or at least discuss why it could be bad? |
@Madjosz Thanks for reaching out. After reviewing this issue, I think it is worth fixing. The general rules here are:
Occasionally exceptions are made for trivial changes like updating Javadocs, adding a new API method, or for XML transformation fixes (which is a whole other problem area). In this case, the problem and a potential fix are well-documented, but no one stepped up to fix it. Since Hacktoberfest has just started, I will label the issue accordingly. Someone will probably come along, make the change, and submit the PR. Or, you can do it yourself if you want. |
raised a PR #777 |
* fixes stleary#713 * document JSONException in JavaDoc * remove unused Comparable<T> boundary to reuse GenericBean in test
* fixes stleary#713 * document JSONException in JavaDoc * remove unused Comparable<T> boundary to reuse GenericBean in test
Problem
The constructors
org.json.JSONObject.JSONObject(Map<?, ?>)
andorg.json.JSONObject.JSONObject(Object)
do not check occuring numbers in values for finiteness and thus allow creating of invalid JSON object which then run into strange behaviour when stringifying.The
Map
constructor even mentiones@throws JSONException - If a value in the map is non-finite number.
but this exception can only occur here when a nested call throws it, e.g. a value with aList
containing non-finite numbers.Code example
Both tests fail and instead the constructors return an instance of a
JSONObject
with a key"a"
mapped to a non-finite double.Cascading problems
Calling
toString()
on the returnedJSONObject
will internally throw aJSONException
intoString(0)
while writing the non-finite value and this will be catched resulting in anull
returned from the method.obj.getDouble("a")
on the other hand will return the non-finite double.Idea
While
JSONObject.put(String, Object)
checks for finiteness before putting the value to the mapJSON-java/src/main/java/org/json/JSONObject.java
Lines 1870 to 1871 in 5920eca
JSON-java/src/main/java/org/json/JSONObject.java
Line 291 in 5920eca
JSON-java/src/main/java/org/json/JSONObject.java
Line 1552 in 5920eca
wrap()
the value so it might be a good idea to do the finiteness-check there and document the JSONException.The text was updated successfully, but these errors were encountered: