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
AddPadding for ISO 10126 computes the pad length as the distance from inputOffset to the end of the array and throws ArgumentException when there is no room (code == 0) or when offset+code exceeds the array. The caller must supply a buffer that already has space reserved for the padding bytes.
Solutions
- Allocate the input array with at least one extra byte/block of space beyond the data (e.g. length rounded up to next multiple of block size plus one)
- Ensure inputOffset is a valid non-negative index strictly less than the array length
- Check before calling: if (inputData.Length - inputOffset < 1) grow the array first
Example fix
// before byte[] buf = new byte[data.Length]; // no room for padding when full // after int paddedLen = ((data.Length / blockSize) + 1) * blockSize; byte[] buf = new byte[Math.Max(paddedLen, data.Length + 1)]; Array.Copy(data, buf, data.Length); padding.AddPadding(buf, data.Length);
Defensive patterns
Strategy: validation
Validate before calling
if (inputData == null || inputOffset < 0 || inputOffset >= inputData.Length)
throw new ArgumentException("Need at least one free byte for ISO 10126 padding"); Try / catch
try { padding.AddPadding(buf, offset); }
catch (ArgumentException) { /* resize buf to next block multiple + 1 and retry */ } Prevention
- Always size buffers to the next block boundary plus one byte
- Compute padded length with ((len / block) + 1) * block
- Unit-test the exact-multiple-of-block-size edge case
When it happens
Trigger: Calling AddPadding on an array whose length equals inputOffset (fully full block), or a jagged/corrupted buffer where inputOffset + code exceeds inputData.Length (only reachable via integer overflow of the offset).
Common situations: Encrypting data that is an exact multiple of the block size without allocating an extra block for padding; off-by-one offset calculations when buffering partial blocks.
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 padding length
- Padding block is corrupted
- Not enough space in input array for padding
- Invalid padding
- Pad block corrupted
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/9a9950781b416a6c.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Crypto/Paddings/Iso10126D2Padding.cs:38
{
/// <summary>
/// Adds random padding to the input data array to make it a multiple of the block size according to the
/// ISO10126d2 standard.
/// </summary>
/// <param name="inputData">The input data array that needs to be padded.</param>
/// <param name="inputOffset">The offset in the input data array where the padding should start.</param>
/// <returns>The number of bytes added as padding.</returns>
/// <exception cref="ArgumentException">
/// Thrown when there is not enough space in the input array for padding.
/// </exception>
public int AddPadding(byte[] inputData, int inputOffset)
{
// Calculate how many bytes need to be added to reach the next multiple of block size.
var code = (byte)(inputData.Length - inputOffset);
if (code == 0 || inputOffset + code > inputData.Length)
{
throw new ArgumentException("Not enough space in input array for padding");
}
// Add the padding.
while (inputOffset < (inputData.Length - 1))
{
inputData[inputOffset] = (byte)RandomNumberGenerator.GetInt32(255);
inputOffset++;
}
// Set the last byte of the array to the size of the padding added.
inputData[inputOffset] = code;
return code;
}
/// <summary>
/// Removes the padding from the input data array and returns the original data.
/// </summary>View on GitHub (pinned to 96e2905cab)