NationalSecurityAgency/ghidra · error · IllegalArgumentException
New capacity must fit current contents
Error message
New capacity must fit current contents
What it means
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().
Source
Thrown at Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/ByteBufferUtils.java:36
import java.nio.ByteBuffer;
/**
* Some utilities for manipulating a {@link ByteBuffer}
*/
public interface ByteBufferUtils {
/**
* Resize a write-mode buffer
*
* <p>
* This preserves the buffer contents
*
* @param buf the buffer
* @param capacity the new capacity, greater or equal to the buffer's limit
* @return the new buffer
*/
public static ByteBuffer resize(ByteBuffer buf, int capacity) {
if (capacity < buf.limit()) {
throw new IllegalArgumentException("New capacity must fit current contents");
}
buf.flip();
ByteBuffer resized = ByteBuffer.allocate(capacity);
resized.put(buf);
return resized;
}
/**
* Resize a write-mode buffer to twice its current capacity
*
* <p>
* This preserves the buffer contents
*
* @param buf the buffer
* @return the new buffer
*/
public static ByteBuffer upsize(ByteBuffer buf) {
return resize(buf, buf.capacity() * 2);View on GitHub (pinned to d5f144c24d)
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.
Example fix
// before ByteBuffer grown = ByteBufferUtils.resize(buf, need); // throws if need < buf.limit() // after: clamp to never shrink below current contents ByteBuffer grown = ByteBufferUtils.resize(buf, Math.max(need, buf.limit()));
Defensive patterns
Strategy: validation
Validate before calling
// Never shrink below the buffer's current content limit int newCap = Math.max(requestedCapacity, buf.limit()); ByteBuffer grown = ByteBufferUtils.resize(buf, newCap);
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- mask and a must have equal capacities
- {} is not a constant
- Column {} is not indexed
- Column {} is not of type {}! It is {}
- Given field does not apply to given object type
AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14).
Data as JSON: /api/errors/218f800193011936.
Report an issue: GitHub.