{"record":{"id":"badb8adae47fe28a","repo":"apache/dubbo","slug":"expectedsize-cannot-be-negative-but-was-expected","errorCode":null,"errorMessage":"expectedSize cannot be negative but was: {expectedSize}","messagePattern":"expectedSize cannot be negative but was: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java","lineNumber":474,"sourceCode":"\n    public static <K, T> Map<K, T> newConcurrentHashMap(int expectedSize) {\n        if (JRE.JAVA_8.isCurrentVersion()) {\n            return new SafeConcurrentHashMap<>(capacity(expectedSize));\n        }\n        return new ConcurrentHashMap<>(capacity(expectedSize));\n    }\n\n    public static <K, T> Map<K, T> newConcurrentHashMap() {\n        if (JRE.JAVA_8.isCurrentVersion()) {\n            return new SafeConcurrentHashMap<>();\n        }\n        return new ConcurrentHashMap<>();\n    }\n\n    public static int capacity(int expectedSize) {\n        if (expectedSize < 3) {\n            if (expectedSize < 0) {\n                throw new IllegalArgumentException(\"expectedSize cannot be negative but was: \" + expectedSize);\n            }\n            return expectedSize + 1;\n        }\n        if (expectedSize < 1 << (Integer.SIZE - 2)) {\n            return (int) (expectedSize / 0.75F + 1.0F);\n        }\n        return Integer.MAX_VALUE;\n    }\n\n    public static class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {\n        private static final long serialVersionUID = 1L;\n\n        public SafeConcurrentHashMap() {}\n\n        public SafeConcurrentHashMap(int initialCapacity) {\n            super(initialCapacity);\n        }\n","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java#L456-L492","documentation":"Thrown by CollectionUtils.capacity when expectedSize is negative. capacity() computes an optimal HashMap/HashSet initial capacity for a given element count (mirroring Guava's Maps.newHashMapWithExpectedSize sizing). A negative count is meaningless for sizing and is rejected; counts 0..2 return count+1, larger counts apply the 0.75 load-factor formula.","triggerScenarios":"CollectionUtils.capacity(expectedSize) where expectedSize < 0. Typically called internally by newHashMapWithExpectedSize / newConcurrentHashMap helpers, but exposed publicly. A negative input reaches it through a computed size (e.g. a list.size() result miscalculated, or a subtraction underflow).","commonSituations":"Passing a size derived from arithmetic that went negative (overflow, subtraction underflow); feeding an unchecked external value into capacity. Rare in correct code; indicates an upstream size-calculation bug.","solutions":["Clamp expectedSize to 0 before calling: capacity(Math.max(0, expectedSize)).","Fix the upstream calculation that produced a negative size.","Validate input at the boundary if expectedSize comes from external/config data."],"exampleFix":"// before\nint cap = CollectionUtils.capacity(computedSize); // computedSize may be -1\n\n// after\nint cap = CollectionUtils.capacity(Math.max(0, computedSize));","handlingStrategy":"validation","validationCode":"int expectedSize = /* ... */;\nif (expectedSize < 0) expectedSize = 0;\nint cap = CollectionUtils.capacity(expectedSize);","typeGuard":"static boolean isNonNegative(int v) { return v >= 0; }","tryCatchPattern":null,"preventionTips":["Clamp computed sizes to 0 before passing to capacity().","Fix upstream arithmetic that produced a negative size.","Validate external/config size inputs at the boundary."],"tags":["collections","sizing","validation","numeric"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}