TheAlgorithms/C-Sharp · error · ArgumentException
Not enough space in input array for padding
Error message
Not enough space in input array for padding
What it means
X932Padding.AddPadding pads a block in place: it fills bytes from inputOffset to the end of inputData with zero (or random) bytes and writes the padding byte count into the last byte. The library throws this ArgumentException when inputOffset >= inputData.Length, i.e. there is no room left in the array at the given offset for even a single padding byte.
Solutions
- Verify inputOffset < inputData.Length before calling AddPadding; clamp or recompute the offset.
- Ensure the buffer passed to AddPadding is the full block-size array and the offset points at the first free byte, not the end.
- If the buffer is genuinely full (no padding space), copy the data into a larger array (block size + 1 or next block boundary) before padding.
- Wrap the call in a try/catch for ArgumentException to surface a clearer domain error with the offset and length values.
Example fix
// before
int written = buffer.Length;
padding.AddPadding(buffer, written); // offset == length -> throws
// after
int written = dataLength % block.GetSize();
if (written >= buffer.Length) throw new InvalidOperationException("buffer full");
padding.AddPadding(buffer, written); Defensive patterns
Strategy: validation
Validate before calling
if (buffer == null || inputOffset < 0 || inputOffset >= buffer.Length)
throw new ArgumentException($"Need at least one byte of padding space: offset {inputOffset}, length {buffer?.Length}"); Try / catch
try { padding.AddPadding(buffer, offset); }
catch (ArgumentException ex) { throw new InvalidOperationException($"Padding failed: offset={offset}, len={buffer.Length}", ex); } Prevention
- Assert inputOffset < buffer.Length before every padding call
- Keep block buffers allocated at exactly the cipher block size and track free space separately from the offset
- Pass the first free byte index, never buffer.Length or a byte count
When it happens
Trigger: Calling AddPadding with an inputOffset equal to or larger than the array length, e.g. AddPadding(new byte[16], 16), passing an offset from a different (smaller) buffer, or passing a negative-interpretation offset from a failed earlier calculation. Also passing an empty array with offset 0.
Common situations: Cipher block-buffer management bugs where the offset is computed as buffer.Length instead of the free-space start; reusing an offset variable after the buffer was resized; confusing the 'bytes of data' count with the offset; block-mode drivers that already consumed the whole block before padding.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Invalid block size
- Not enough space in input array for padding
- Input length must be a multiple of block size
- Invalid padding length
- Invalid padding
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/efc74b574d82a0ee.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Crypto/Paddings/X932Padding.cs:40
public class X932Padding(bool useRandomPadding) : IBlockCipherPadding
{
private readonly bool useRandomPadding = useRandomPadding;
/// <summary>
/// Adds padding to the input data according to the X9.23 padding scheme.
/// </summary>
/// <param name="inputData">The input data array to be padded.</param>
/// <param name="inputOffset">The offset in the input data array where the padding should start.</param>
/// <returns>The number of padding bytes added.</returns>
/// <exception cref="ArgumentException">
/// Thrown when the input offset is greater than or equal to the input data length.
/// </exception>
public int AddPadding(byte[] inputData, int inputOffset)
{
// Check if the input offset is valid.
if (inputOffset >= inputData.Length)
{
throw new ArgumentException("Not enough space in input array for padding");
}
// Calculate the number of padding bytes needed.
var code = (byte)(inputData.Length - inputOffset);
// Fill the remaining bytes with random or zero bytes
while (inputOffset < inputData.Length - 1)
{
if (!useRandomPadding)
{
// Use zero bytes if random padding is disabled.
inputData[inputOffset] = 0;
}
else
{
// Use random bytes if random padding is enabled.
inputData[inputOffset] = (byte)RandomNumberGenerator.GetInt32(255);
}View on GitHub (pinned to 96e2905cab)