From eb56704e68a186f975400e009d28d4e0b5d887ec Mon Sep 17 00:00:00 2001 From: Cleydyr de Albuquerque Date: Thu, 2 Feb 2023 18:15:03 +0100 Subject: [PATCH] fix: set default maximum nesting depth as 512 --- src/main/java/org/json/XMLParserConfiguration.java | 11 ++++++++--- src/test/java/org/json/junit/JSONArrayTest.java | 1 - .../java/org/json/junit/XMLConfigurationTest.java | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index e3311fc..f118a81 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -22,6 +22,11 @@ public class XMLParserConfiguration { */ public static final int UNDEFINED_MAXIMUM_NESTING_DEPTH = -1; + /** + * The default maximum nesting depth when parsing a XML document to JSON. + */ + public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512; + /** Original Configuration of the XML Parser. */ public static final XMLParserConfiguration ORIGINAL = new XMLParserConfiguration(); @@ -64,7 +69,7 @@ public class XMLParserConfiguration { * When parsing the XML into JSON, specifies the tags whose values should be converted * to arrays */ - private int maxNestingDepth = UNDEFINED_MAXIMUM_NESTING_DEPTH; + private int maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH; /** * Default parser configuration. Does not keep strings (tries to implicitly convert @@ -321,8 +326,8 @@ public class XMLParserConfiguration { /** * Defines the maximum nesting depth that the parser will descend before throwing an exception - * when parsing the XML into JSON. The default max nesting depth is undefined, which means the - * parser will go as deep as the maximum call stack size allows. Using any negative value as a + * when parsing the XML into JSON. The default max nesting depth is 512, which means the parser + * will go as deep as the maximum call stack size allows. Using any negative value as a * parameter is equivalent to setting no limit to the nesting depth. * @param maxNestingDepth the maximum nesting depth allowed to the XML parser * @return The existing configuration will not be modified. A new configuration is returned. diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 1a2df7f..aa8657f 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -6,7 +6,6 @@ Public Domain. import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 25b17e2..21a2b59 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -1056,7 +1056,7 @@ public class XMLConfigurationTest { public void testMaxNestingDepthIsSet() { XMLParserConfiguration xmlParserConfiguration = XMLParserConfiguration.ORIGINAL; - assertEquals(xmlParserConfiguration.getMaxNestingDepth(), XMLParserConfiguration.UNDEFINED_MAXIMUM_NESTING_DEPTH); + assertEquals(xmlParserConfiguration.getMaxNestingDepth(), XMLParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); xmlParserConfiguration = xmlParserConfiguration.withMaxNestingDepth(42);