{"record":{"id":"e86fabdd76755a72","repo":"MyCATApache/Mycat-Server","slug":"initial-capacity-must-be-greater-than-0","errorCode":null,"errorMessage":"Initial capacity must be greater than 0","messagePattern":"Initial capacity must be greater than 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/memory/unsafe/map/BytesToBytesMap.java","lineNumber":193,"sourceCode":"\n  public BytesToBytesMap(\n      DataNodeMemoryManager dataNodeMemoryManager,\n      DataNodeDiskManager blockManager,\n      SerializerManager serializerManager,\n      int initialCapacity,\n      double loadFactor,\n      long pageSizeBytes,\n      boolean enablePerfMetrics) {\n    super(dataNodeMemoryManager, pageSizeBytes);\n    this.dataNodeMemoryManager = dataNodeMemoryManager;\n    this.blockManager = blockManager;\n    this.serializerManager = serializerManager;\n    this.loadFactor = loadFactor;\n    this.loc = new Location();\n    this.pageSizeBytes = pageSizeBytes;\n    this.enablePerfMetrics = enablePerfMetrics;\n    if (initialCapacity <= 0) {\n      throw new IllegalArgumentException(\"Initial capacity must be greater than 0\");\n    }\n    if (initialCapacity > MAX_CAPACITY) {\n      throw new IllegalArgumentException(\n        \"Initial capacity \" + initialCapacity + \" exceeds maximum capacity of \" + MAX_CAPACITY);\n    }\n    if (pageSizeBytes > DataNodeMemoryManager.MAXIMUM_PAGE_SIZE_BYTES) {\n      throw new IllegalArgumentException(\"Page size \" + pageSizeBytes + \" cannot exceed \" +\n        DataNodeMemoryManager.MAXIMUM_PAGE_SIZE_BYTES);\n    }\n    allocate(initialCapacity);\n  }\n\n  public BytesToBytesMap(\n      DataNodeMemoryManager dataNodeMemoryManager,\n      int initialCapacity,\n      long pageSizeBytes) {\n    this(dataNodeMemoryManager, initialCapacity, pageSizeBytes, false);\n  }","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/unsafe/map/BytesToBytesMap.java#L175-L211","documentation":"BytesToBytesMap's constructor validates its configuration before allocating pages. If initialCapacity is <= 0 it throws IllegalArgumentException 'Initial capacity must be greater than 0', because the hash map cannot be sized with a non-positive capacity.","triggerScenarios":"Constructing BytesToBytesMap with initialCapacity == 0 or a negative value (e.g. from a computed/configured size that evaluated to 0, integer overflow, or an unconfigured property defaulting to 0).","commonSituations":"Config file missing the initial-capacity entry so a default of 0 is used; computing capacity from a row count of 0; int overflow from multiplying user-supplied numbers.","solutions":["Pass a positive initialCapacity (>= 1) when constructing BytesToBytesMap","Clamp computed capacity: int cap = Math.max(1, requestedCapacity)","Fix the configuration source so the capacity property has a sane positive default","Validate/parse user input to reject 0/negative and overflow-producing values before construction"],"exampleFix":"// before\nmap = new BytesToBytesMap(mgr, serializerManager, 0, pageSize, loadFactor, false);\n// after\nint initialCapacity = Math.max(1, configuredCapacity);\nmap = new BytesToBytesMap(mgr, serializerManager, initialCapacity, pageSize, loadFactor, false);","handlingStrategy":"validation","validationCode":"if (initialCapacity <= 0 || initialCapacity > MAX_CAPACITY) {\n    throw new IllegalArgumentException(\"initialCapacity must be in (0, \" + MAX_CAPACITY + \"]\");\n}\nint cap = Math.max(1, initialCapacity);","typeGuard":null,"tryCatchPattern":"try {\n    map = new BytesToBytesMap(mgr, serMgr, cap, pageSize, loadFactor, metrics);\n} catch (IllegalArgumentException e) {\n    map = new BytesToBytesMap(mgr, serMgr, Math.max(1, cap), pageSize, loadFactor, metrics);\n}","preventionTips":["Clamp computed capacities with Math.max(1, n)","Validate config-file capacity values at load time","Guard against integer overflow when deriving capacity from counts","Also check capacity <= MAX_CAPACITY and pageSize <= MAXIMUM_PAGE_SIZE_BYTES"],"tags":["configuration","illegal-argument","memory-map"],"backgroundTag":"invalid-argument-value","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}