{"record":{"id":"8d876e779460e7b4","repo":"apache/dubbo","slug":"illegal-initial-capacity-maxcapacity","errorCode":null,"errorMessage":"Illegal initial capacity: ${maxCapacity}","messagePattern":"Illegal initial capacity: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/utils/LFUCache.java","lineNumber":52,"sourceCode":"\n    private static final float DEFAULT_EVICTION_FACTOR = 0.75f;\n\n    public LFUCache() {\n        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_EVICTION_FACTOR);\n    }\n\n    /**\n     * Constructs and initializes cache with specified capacity and eviction\n     * factor. Unacceptable parameter values followed with\n     * {@link IllegalArgumentException}.\n     *\n     * @param maxCapacity    cache max capacity\n     * @param evictionFactor cache proceedEviction factor\n     */\n    @SuppressWarnings(\"unchecked\")\n    public LFUCache(final int maxCapacity, final float evictionFactor) {\n        if (maxCapacity <= 0) {\n            throw new IllegalArgumentException(\"Illegal initial capacity: \" + maxCapacity);\n        }\n        boolean factorInRange = evictionFactor <= 1 && evictionFactor > 0;\n        if (!factorInRange || Float.isNaN(evictionFactor)) {\n            throw new IllegalArgumentException(\"Illegal eviction factor value:\" + evictionFactor);\n        }\n        this.capacity = maxCapacity;\n        this.evictionCount = (int) (capacity * evictionFactor);\n        this.map = new HashMap<>();\n        this.freqTable = new CacheDeque[capacity + 1];\n        for (int i = 0; i <= capacity; i++) {\n            freqTable[i] = new CacheDeque<>();\n        }\n        for (int i = 0; i < capacity; i++) {\n            freqTable[i].nextDeque = freqTable[i + 1];\n        }\n        freqTable[capacity].nextDeque = freqTable[capacity];\n    }\n","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/LFUCache.java#L34-L70","documentation":"LFUCache constructor validates that maxCapacity is positive; a zero or negative capacity is rejected because the cache cannot operate with no slots (it also sizes the internal freqTable to capacity+1).","triggerScenarios":"Instantiating new LFUCache(capacity, factor) with capacity <= 0, e.g. from a config value that defaulted to 0 or an arithmetic underflow computing the size.","commonSituations":"Cache-size properties that were never set and default to 0; size calculations like (total / divisor) yielding 0 for small inputs; tests that construct with literal 0.","solutions":["Ensure the configured cache capacity is at least 1","Compute capacity defensively and clamp to a minimum, or skip cache creation when the intended size is 0","Validate the config property at load time rather than at cache construction"],"exampleFix":"// before\nLFUCache<String,String> c = new LFUCache<>(0, 0.8f);\n// after\nint cap = Math.max(1, configuredCap);\nLFUCache<String,String> c = new LFUCache<>(cap, 0.8f);","handlingStrategy":"validation","validationCode":"int cap = configuredCapacity;\nif (cap <= 0) throw new IllegalArgumentException(\"cache capacity must be > 0: \" + cap);\nnew LFUCache<>(cap, 0.8f);","typeGuard":"static boolean validCapacity(int c) { return c > 0; }","tryCatchPattern":"try { new LFUCache<>(cap, factor); }\ncatch (IllegalArgumentException e) { /* cap <= 0; supply default */ }","preventionTips":["Clamp capacity to at least 1 at config-load time","Unit-test cache construction with edge-case sizes"],"tags":["cache","config","validation"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}