Skip to content

Commit c4cd526

Browse files
authored
Merge pull request #779 from Madjosz/713_jsonobject_nonfinite
add validity check for JSONObject constructors
2 parents 776b5cc + c93014c commit c4cd526

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ public JSONObject(Map<?, ?> m) {
283283
}
284284
final Object value = e.getValue();
285285
if (value != null) {
286+
testValidity(value);
286287
this.map.put(String.valueOf(e.getKey()), wrap(value));
287288
}
288289
}
@@ -346,6 +347,8 @@ public JSONObject(Map<?, ?> m) {
346347
* @param bean
347348
* An object that has getter methods that should be used to make
348349
* a JSONObject.
350+
* @throws JSONException
351+
* If a getter returned a non-finite number.
349352
*/
350353
public JSONObject(Object bean) {
351354
this();
@@ -1691,6 +1694,8 @@ public String optString(String key, String defaultValue) {
16911694
*
16921695
* @param bean
16931696
* the bean
1697+
* @throws JSONException
1698+
* If a getter returned a non-finite number.
16941699
*/
16951700
private void populateMap(Object bean) {
16961701
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()));
@@ -1726,6 +1731,7 @@ && isValidMethodName(method.getName())) {
17261731

17271732
objectsRecord.add(result);
17281733

1734+
testValidity(result);
17291735
this.map.put(key, wrap(result, objectsRecord));
17301736

17311737
objectsRecord.remove(result);

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.junit.Assert.assertNotEquals;
1010
import static org.junit.Assert.assertNotNull;
1111
import static org.junit.Assert.assertNull;
12+
import static org.junit.Assert.assertThrows;
1213
import static org.junit.Assert.assertTrue;
1314
import static org.junit.Assert.fail;
1415
import static org.mockito.Mockito.mock;
@@ -1972,7 +1973,7 @@ public void jsonObjectToStringIndent() {
19721973
@Test
19731974
public void jsonObjectToStringSuppressWarningOnCastToMap() {
19741975
JSONObject jsonObject = new JSONObject();
1975-
Map<String, String> map = new HashMap();
1976+
Map<String, String> map = new HashMap<>();
19761977
map.put("abc", "def");
19771978
jsonObject.put("key", map);
19781979

@@ -3283,7 +3284,7 @@ public void testSingletonEnumBean() {
32833284
@SuppressWarnings("boxing")
32843285
@Test
32853286
public void testGenericBean() {
3286-
GenericBean<Integer> bean = new GenericBean(42);
3287+
GenericBean<Integer> bean = new GenericBean<>(42);
32873288
final JSONObject jo = new JSONObject(bean);
32883289
assertEquals(jo.keySet().toString(), 8, jo.length());
32893290
assertEquals(42, jo.get("genericValue"));
@@ -3627,4 +3628,25 @@ public String toJSONString() {
36273628
.put("b", 2);
36283629
assertFalse(jo1.similar(jo3));
36293630
}
3631+
3632+
private static final Number[] NON_FINITE_NUMBERS = { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN,
3633+
Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NaN };
3634+
3635+
@Test
3636+
public void issue713MapConstructorWithNonFiniteNumbers() {
3637+
for (Number nonFinite : NON_FINITE_NUMBERS) {
3638+
Map<String, Number> map = new HashMap<>();
3639+
map.put("a", nonFinite);
3640+
3641+
assertThrows(JSONException.class, () -> new JSONObject(map));
3642+
}
3643+
}
3644+
3645+
@Test
3646+
public void issue713BeanConstructorWithNonFiniteNumbers() {
3647+
for (Number nonFinite : NON_FINITE_NUMBERS) {
3648+
GenericBean<Number> bean = new GenericBean<>(nonFinite);
3649+
assertThrows(JSONException.class, () -> new JSONObject(bean));
3650+
}
3651+
}
36303652
}

src/test/java/org/json/junit/data/GenericBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @param <T>
1010
* generic number value
1111
*/
12-
public class GenericBean<T extends Number & Comparable<T>> implements MyBean {
12+
public class GenericBean<T extends Number> implements MyBean {
1313
/**
1414
* @param genericValue
1515
* value to initiate with

0 commit comments

Comments
 (0)