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
ISO 7816-4 AddPadding computes the space between inputOffset and the array end and throws ArgumentException when there is no byte left to write the mandatory 0x80 marker (code == 0) or the bounds check fails. The caller must pre-size the buffer to hold data plus at least one padding byte.
Solutions
- Allocate the array with room for at least one extra byte beyond the data
- Ensure 0 < inputData.Length - inputOffset before calling
- If the buffer is exactly a multiple of the block size, add one extra block for padding
Example fix
// before var buf = new byte[data.Length]; // full — no padding space padding.AddPadding(buf, data.Length); // after var buf = new byte[data.Length + (blockSize - data.Length % blockSize)]; 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 7816-4 padding"); Try / catch
try { padding.AddPadding(buf, offset); }
catch (ArgumentException) { /* grow buffer to next block multiple and retry */ } Prevention
- Reserve one extra byte/block for the 0x80 marker
- Test with inputs exactly one block long
- Keep offset arithmetic in one validated helper
When it happens
Trigger: Calling AddPadding on a completely full buffer (inputOffset == inputData.Length), or with an offset that makes inputOffset + code exceed the array (offset overflow).
Common situations: Encrypting whole blocks of data without reserving an extra block for padding; miscounting buffer offsets when accumulating partial blocks in streaming code.
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
- Not enough space in input array for padding
- Invalid padding length
- Padding block is corrupted
- Invalid padding
- Pad block corrupted
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/f62af03c1e3f588b.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Crypto/Paddings/Iso7816D4Padding.cs:48
{
/// <summary>
/// Adds padding to the input data according to the ISO 7816-4 standard.
/// </summary>
/// <param name="inputData">The input data array that needs padding.</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 or when the input offset is invalid.
/// </exception>
public int AddPadding(byte[] inputData, int inputOffset)
{
// Calculate the number of padding bytes based on the input data length and offset.
var code = (byte)(inputData.Length - inputOffset);
// Check if the padding bytes are valid and fit in the input array.
if (code == 0 || inputOffset + code > inputData.Length)
{
throw new ArgumentException("Not enough space in input array for padding");
}
// Set the first padding byte to 80. This marks the start of padding in the ISO 7816-4 standard.
inputData[inputOffset] = 80;
inputOffset++;
// Set the remaining padding bytes to 0.
while (inputOffset < inputData.Length)
{
inputData[inputOffset] = 0;
inputOffset++;
}
// Return the number of padding bytes.
return code;
}
/// <summary>View on GitHub (pinned to 96e2905cab)