{"record":{"id":"b6d120e57dcb1da9","repo":"baomidou/mybatis-plus","slug":"expectedsize-cannot-be-negative-but-was","errorCode":null,"errorMessage":"expectedSize cannot be negative but was: {}","messagePattern":"expectedSize cannot be negative but was: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/CollectionUtils.java","lineNumber":176,"sourceCode":"            return v;\n        } else {\n            return concurrentHashMap.computeIfAbsent(key, mappingFunction);\n        }\n\n    }\n\n    /**\n     * Returns a capacity that is sufficient to keep the map from being resized as\n     * long as it grows no larger than expectedSize and the load factor is >= its\n     * default (0.75).\n     *\n     * @see com.google.common.collect.Maps#capacity(int)\n     * @since 3.4.0\n     */\n    private 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 < MAX_POWER_OF_TWO) {\n            // This is the calculation used in JDK8 to resize when a putAll\n            // happens; it seems to be the most conservative calculation we\n            // can make.  0.75 is the default load factor.\n            return (int) ((float) expectedSize / 0.75F + 1.0F);\n        }\n        return Integer.MAX_VALUE; // any large value\n    }\n\n    // 提供处理Map多key取值工具方法\n\n    /**\n     * 批量取出Map中的值\n     *\n     * @param map  map","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/CollectionUtils.java#L158-L194","documentation":"CollectionUtils.capacity(expectedSize) computes an initial HashMap capacity for an expected size (mirroring Guava's Maps.capacity). If expectedSize is negative it throws IllegalArgumentException('expectedSize cannot be negative but was: N'). Callers reach it via collection helpers that size a new map from a computed length; a negative computed size is always a bug in the calling arithmetic or corrupt input.","triggerScenarios":"Passing a negative expected size into the internal capacity() path, e.g. a collection helper invoked with a size derived from a subtraction or an array length that came out negative. Since capacity() is private, the practical trigger is a public CollectionUtils method (map-sized helpers introduced in 3.4.0) receiving a negative size from user-supplied data.","commonSituations":"Entity metadata with negative counts (e.g. field count computed by subtraction returning negative); copying mybatis-plus internals into user code and calling capacity with unchecked input; extremely rare in normal use because public APIs usually pass array lengths which cannot be negative.","solutions":["Inspect the stack trace to find which caller computed the negative size and fix that arithmetic (usually an underflow like a.length - b.length).","Validate sizes derived from external/metadata input before passing them into collection helpers.","If you copied this helper into your own code, reject negative sizes at your API boundary instead of deep inside capacity()."],"exampleFix":"// before\nint expected = first.length - skipCount; // skipCount > first.length -> negative\nMap<String, Field> m = new LinkedHashMap<>(CollectionUtils.capacity(expected));\n\n// after\nint expected = Math.max(0, first.length - skipCount);\nMap<String, Field> m = new LinkedHashMap<>(expected);","handlingStrategy":"validation","validationCode":"int expected = computeExpectedSize();\nif (expected < 0) {\n    throw new IllegalArgumentException(\"expectedSize computed negative: \" + expected);\n}\nMap<String, Field> m = new LinkedHashMap<>(expected);","typeGuard":null,"tryCatchPattern":"try {\n    Map<String, Field> m = new LinkedHashMap<>(CollectionUtils.capacity(size));\n} catch (IllegalArgumentException e) {\n    throw new IllegalStateException(\"size computation bug: \" + size, e);\n}","preventionTips":["Never derive map sizes by unchecked subtraction; clamp with Math.max(0, ...).","Treat sizes coming from metadata or external input as untrusted and bound-check them at the boundary."],"tags":["collections","argument-validation","internal-api"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}