Merge pull request #1011 from marilynel/master

more sonarcube fixes
This commit is contained in:
Sean Leary 2025-09-18 20:13:43 -05:00 committed by GitHub
commit aff59d06fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 105 additions and 61 deletions

View File

@ -2041,7 +2041,7 @@ public class JSONObject {
return 1;
}
// since we've already reached the Object class, return -1;
// we've already reached the Object class
Class<?> c = m.getDeclaringClass();
if (c.getSuperclass() == null) {
return -1;
@ -2391,7 +2391,6 @@ public class JSONObject {
char b;
char c = 0;
String hhhh;
int i;
int len = string.length();
@ -2482,7 +2481,7 @@ public class JSONObject {
return false;
}
return checkSimilarEntries(other);
} catch (Throwable exception) {
} catch (Exception e) {
return false;
}
}
@ -2499,14 +2498,20 @@ public class JSONObject {
return false;
}
if (!checkThis(valueThis, valueOther)) {
if (!checkObjectType(valueThis, valueOther)) {
return false;
}
}
return true;
}
private boolean checkThis(Object valueThis, Object valueOther) {
/**
* Convenience function. Compares types of two objects.
* @param valueThis Object whose type is being checked
* @param valueOther Reference object
* @return true if match, else false
*/
private boolean checkObjectType(Object valueThis, Object valueOther) {
if (valueThis instanceof JSONObject) {
return ((JSONObject)valueThis).similar(valueOther);
} else if (valueThis instanceof JSONArray) {
@ -2619,6 +2624,7 @@ public class JSONObject {
try {
return stringToNumber(string);
} catch (Exception ignore) {
// Do nothing
}
}
return string;
@ -2639,6 +2645,58 @@ public class JSONObject {
if ((initial >= '0' && initial <= '9') || initial == '-') {
// decimal representation
if (isDecimalNotation(val)) {
return getNumber(val, initial);
}
// block items like 00 01 etc. Java number parsers treat these as Octal.
checkForInvalidNumberFormat(val, initial);
// integer representation.
// This will narrow any values to the smallest reasonable Object representation
// (Integer, Long, or BigInteger)
// BigInteger down conversion: We use a similar bitLength compare as
// BigInteger#intValueExact uses. Increases GC, but objects hold
// only what they need. i.e. Less runtime overhead if the value is
// long lived.
BigInteger bi = new BigInteger(val);
if(bi.bitLength() <= 31){
return Integer.valueOf(bi.intValue());
}
if(bi.bitLength() <= 63){
return Long.valueOf(bi.longValue());
}
return bi;
}
throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
/**
* Convenience function. Block items like 00 01 etc. Java number parsers treat these as Octal.
* @param val value to convert
* @param initial first char of val
* @throws exceptions if numbers are formatted incorrectly
*/
private static void checkForInvalidNumberFormat(String val, char initial) {
if(initial == '0' && val.length() > 1) {
char at1 = val.charAt(1);
if(at1 >= '0' && at1 <= '9') {
throw new NumberFormatException("val ["+ val +"] is not a valid number.");
}
} else if (initial == '-' && val.length() > 2) {
char at1 = val.charAt(1);
char at2 = val.charAt(2);
if(at1 == '0' && at2 >= '0' && at2 <= '9') {
throw new NumberFormatException("val ["+ val +"] is not a valid number.");
}
}
}
/**
* Convenience function. Handles val if it is a number
* @param val value to convert
* @param initial first char of val
* @return val as a BigDecimal
*/
private static Number getNumber(String val, char initial) {
// Use a BigDecimal all the time so we keep the original
// representation. BigDecimal doesn't support -0.0, ensure we
// keep that by forcing a decimal.
@ -2661,38 +2719,6 @@ public class JSONObject {
}
}
}
// block items like 00 01 etc. Java number parsers treat these as Octal.
if(initial == '0' && val.length() > 1) {
char at1 = val.charAt(1);
if(at1 >= '0' && at1 <= '9') {
throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
} else if (initial == '-' && val.length() > 2) {
char at1 = val.charAt(1);
char at2 = val.charAt(2);
if(at1 == '0' && at2 >= '0' && at2 <= '9') {
throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
}
// integer representation.
// This will narrow any values to the smallest reasonable Object representation
// (Integer, Long, or BigInteger)
// BigInteger down conversion: We use a similar bitLength compare as
// BigInteger#intValueExact uses. Increases GC, but objects hold
// only what they need. i.e. Less runtime overhead if the value is
// long lived.
BigInteger bi = new BigInteger(val);
if(bi.bitLength() <= 31){
return Integer.valueOf(bi.intValue());
}
if(bi.bitLength() <= 63){
return Long.valueOf(bi.longValue());
}
return bi;
}
throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
/**
* Throw an exception if the object is a NaN or infinite number.
@ -3044,6 +3070,29 @@ public class JSONObject {
// might throw an exception
attemptWriteValue(writer, indentFactor, indent, entry, key);
} else if (length != 0) {
writeContent(writer, indentFactor, indent, needsComma);
}
writer.write('}');
return writer;
} catch (IOException exception) {
throw new JSONException(exception);
}
}
/**
* Convenience function. Writer attempts to write formatted content
* @param writer
* Writes the serialized JSON
* @param indentFactor
* The number of spaces to add to each level of indentation.
* @param indent
* The indentation of the top level.
* @param needsComma
* Boolean flag indicating a comma is needed
* @throws IOException
* If something goes wrong
*/
private void writeContent(Writer writer, int indentFactor, int indent, boolean needsComma) throws IOException {
final int newIndent = indent + indentFactor;
for (final Entry<String,?> entry : this.entrySet()) {
if (needsComma) {
@ -3067,12 +3116,6 @@ public class JSONObject {
}
indent(writer, indent);
}
writer.write('}');
return writer;
} catch (IOException exception) {
throw new JSONException(exception);
}
}
/**
* Convenience function. Writer attempts to write a value.

View File

@ -3895,7 +3895,8 @@ public class JSONObjectTest {
}
@Test
public void issue743SerializationMapWith1000Objects() {
public void issue743SerializationMapWith500Objects() {
// TODO: find out why 1000 objects no longer works
HashMap<String, Object> map = buildNestedMap(500);
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(500);
JSONObject object = new JSONObject(map, parserConfiguration);