max-number-length-config sonarqube fixes

This commit is contained in:
Sean Leary
2026-07-16 13:23:39 -05:00
parent a28328c706
commit 9953b76050
3 changed files with 16 additions and 3 deletions
+13 -1
View File
@@ -1484,6 +1484,18 @@ public class JSONObject {
|| val instanceof Short || val instanceof Byte){ || val instanceof Short || val instanceof Byte){
return BigInteger.valueOf(((Number) val).longValue()); return BigInteger.valueOf(((Number) val).longValue());
} }
return attemptConversionToBigInteger(val, defaultValue, maxNumberLength);
}
/**
* Convenience method to attempt conversion of value to BigInteger.
* Added to reduce complexity of objectToBigInteger()
* @param val the value to be converted
* @param defaultValue the default value to use if conversion is not attempted or fails
* @param maxNumberLength the max length allowed for BigIntegers
* @return the converted value, or the defaultValue
*/
private static BigInteger attemptConversionToBigInteger(Object val, BigInteger defaultValue, int maxNumberLength) {
// don't check if it's a string in case of unchecked Number subclasses // don't check if it's a string in case of unchecked Number subclasses
try { try {
/** /**
@@ -2799,7 +2811,7 @@ public class JSONObject {
jsonParserConfiguration = new JSONParserConfiguration(); jsonParserConfiguration = new JSONParserConfiguration();
} }
// user declines max number checking // user declines max number checking
if (jsonParserConfiguration.getMaxNumberLength() == JSONParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH) { if (jsonParserConfiguration.getMaxNumberLength() == ParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH) {
return stringToNumber(string); return stringToNumber(string);
} }
if (string.length() <= jsonParserConfiguration.getMaxNumberLength()) { if (string.length() <= jsonParserConfiguration.getMaxNumberLength()) {
@@ -58,8 +58,9 @@ public class ParserConfiguration {
* *
* @param keepStrings A boolean indicating whether to preserve strings during parsing. * @param keepStrings A boolean indicating whether to preserve strings during parsing.
* @param maxNestingDepth An integer representing the maximum allowed nesting depth. * @param maxNestingDepth An integer representing the maximum allowed nesting depth.
* @deprecated * @deprecated Use the with*() methods instead
*/ */
@Deprecated
protected ParserConfiguration(final boolean keepStrings, final int maxNestingDepth) { protected ParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
this.keepStrings = keepStrings; this.keepStrings = keepStrings;
this.maxNestingDepth = maxNestingDepth; this.maxNestingDepth = maxNestingDepth;
+1 -1
View File
@@ -667,7 +667,7 @@ public class XML {
if ((initial >= '0' && initial <= '9') || initial == '-') { if ((initial >= '0' && initial <= '9') || initial == '-') {
try { try {
// user declines max number checking // user declines max number checking
if (xmlParserConfiguration.getMaxNumberLength() == JSONParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH) { if (xmlParserConfiguration.getMaxNumberLength() == ParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH) {
return stringToNumber(string); return stringToNumber(string);
} }
if(string.length() <= xmlParserConfiguration.getMaxNumberLength()) { if(string.length() <= xmlParserConfiguration.getMaxNumberLength()) {