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 writes `code` padding bytes (equal to blockSize, or the remainder) starting at inputOffset within the caller-supplied array. If the array does not have room for those bytes beyond inputOffset, the method throws ArgumentException instead of silently truncating or overflowing the buffer.

Solutions

  1. Allocate the input array with room for at least one extra block: inputLength + blockSize.
  2. Recheck that inputOffset points within the array and leaves room for padding.
  3. Pad into a larger copy of the data before encryption.
  4. Confirm the array length you pass matches the padded length the block cipher expects (multiple of blockSize).

Example fix

// before
var buf = new byte[data.Length]; // no padding room
padding.AddPadding(buf, 0, data.Length);
// after
var buf = new byte[data.Length + blockSize];
Array.Copy(data, buf, data.Length);
padding.AddPadding(buf, 0, data.Length);
Defensive patterns

Strategy: validation

Validate before calling

var paddedLen = dataLength + blockSize;
if (buffer is null || buffer.Length < paddedLen || inputOffset + paddedLen > buffer.Length)
    throw new ArgumentException("Buffer too small for data plus one block of PKCS7 padding");

Type guard

static bool HasPaddingRoom(byte[] buf, int offset, int len, int blockSize) => buf is not null && offset >= 0 && len >= 0 && offset + len + blockSize <= buf.Length;

Try / catch

try { padding.AddPadding(buf, offset, len); }
catch (ArgumentException) { buf = new byte[len + blockSize]; Array.Copy(data, buf, len); padding.AddPadding(buf, 0, len); }

Prevention

When it happens

Trigger: Calling AddPadding(input, inputOffset, inputLength) where inputOffset + paddingBytes > input.Length — e.g. an array sized exactly to the data with no padding room, or a non-zero inputOffset on a fully-sized array.

Common situations: Encrypting in-place without allocating an array with extra space for a full block of padding; an off-by-one when the caller sized the array; reusing a source buffer that was sized for unpadded data.

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


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/6558fcd794bb1db9. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Crypto/Paddings/Pkcs7Padding.cs:62

    /// <remarks>
    /// The padding value is equal to the number of of bytes that are added to the array.
    /// For example, if the input array has a length of 16 and the input offset is 10,
    /// then 6 bytes with the value 6 will be added to the end of the array.
    /// </remarks>
    public int AddPadding(byte[] input, int inputOffset)
    {
        // Calculate how many bytes need to be added to reach the next multiple of block size.
        var code = (byte)((blockSize - (input.Length % blockSize)) % blockSize);

        // If no padding is needed, add a full block of padding.
        if (code == 0)
        {
            code = (byte)blockSize;
        }

        if (inputOffset + code > input.Length)
        {
            throw new ArgumentException("Not enough space in input array for padding");
        }

        // Add the padding
        for (var i = 0; i < code; i++)
        {
            input[inputOffset + i] = code;
        }

        return code;
    }

    /// <summary>
    /// Removes the PKCS7 padding from the given input data.
    /// </summary>
    /// <param name="input">The input data with PKCS7 padding. Must not be null and must have a valid length and padding.</param>
    /// <returns>The input data without the padding as a new byte array.</returns>
    /// <exception cref="ArgumentException">
    /// Thrown if the input data is null, has an invalid length, or has an invalid padding.

View on GitHub (pinned to 96e2905cab)