{"record":{"id":"218f800193011936","repo":"NationalSecurityAgency/ghidra","slug":"new-capacity-must-fit-current-contents","errorCode":null,"errorMessage":"New capacity must fit current contents","messagePattern":"New capacity must fit current contents","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/ByteBufferUtils.java","lineNumber":36,"sourceCode":"import java.nio.ByteBuffer;\n\n/**\n * Some utilities for manipulating a {@link ByteBuffer}\n */\npublic interface ByteBufferUtils {\n\t/**\n\t * Resize a write-mode buffer\n\t * \n\t * <p>\n\t * This preserves the buffer contents\n\t * \n\t * @param buf the buffer\n\t * @param capacity the new capacity, greater or equal to the buffer's limit\n\t * @return the new buffer\n\t */\n\tpublic static ByteBuffer resize(ByteBuffer buf, int capacity) {\n\t\tif (capacity < buf.limit()) {\n\t\t\tthrow new IllegalArgumentException(\"New capacity must fit current contents\");\n\t\t}\n\t\tbuf.flip();\n\t\tByteBuffer resized = ByteBuffer.allocate(capacity);\n\t\tresized.put(buf);\n\t\treturn resized;\n\t}\n\n\t/**\n\t * Resize a write-mode buffer to twice its current capacity\n\t * \n\t * <p>\n\t * This preserves the buffer contents\n\t * \n\t * @param buf the buffer\n\t * @return the new buffer\n\t */\n\tpublic static ByteBuffer upsize(ByteBuffer buf) {\n\t\treturn resize(buf, buf.capacity() * 2);","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/ByteBufferUtils.java#L18-L54","documentation":"Thrown (unchecked IllegalArgumentException) by ByteBufferUtils.resize() when the requested capacity is less than the buffer's current limit. resize() must preserve all existing contents (it flips and copies), so a capacity smaller than buf.limit() would silently truncate data — the guard rejects that. Pass a capacity >= buf.limit().","triggerScenarios":"Calling ByteBufferUtils.resize(buf, capacity) with capacity < buf.limit() — e.g. computing a new capacity from a wrong base (position instead of limit), shrinking logic, or a size that ignores how many valid bytes are already in the buffer.","commonSituations":"Dynamic-grow logic that accidentally shrinks; off-by-one or using buf.position() where buf.limit() was intended; sizing a destination buffer from a length field that excludes already-written bytes; reusing a buffer whose limit was set high.","solutions":["Ensure the passed capacity is at least buf.limit() (e.g. Math.max(requested, buf.limit())).","Use the provided resizeDoubling helper (resize to twice current capacity) when you only need growth.","If you genuinely need to shrink, first compact/trim the buffer so limit reflects the data you want to keep.","Re-derive capacity from the correct source — limit (valid content end), not position or capacity."],"exampleFix":"// before\nByteBuffer grown = ByteBufferUtils.resize(buf, need); // throws if need < buf.limit()\n\n// after: clamp to never shrink below current contents\nByteBuffer grown = ByteBufferUtils.resize(buf, Math.max(need, buf.limit()));","handlingStrategy":"validation","validationCode":"// Never shrink below the buffer's current content limit\nint newCap = Math.max(requestedCapacity, buf.limit());\nByteBuffer grown = ByteBufferUtils.resize(buf, newCap);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass capacity >= buf.limit().","Derive new capacity from limit (valid content end), not position or capacity.","Use the doubling resize helper for pure growth."],"tags":["bytebuffer","resize","capacity","illegal-argument","unchecked-exception"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}