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
TbcPadding.AddPadding checks that the input array can hold at least the existing data (input.Length - inputOffset must be >= 0). A negative count means inputOffset exceeds the array length, so the method throws ArgumentException instead of reading or writing out of bounds.
Solutions
- Ensure inputOffset is within [0, input.Length].
- Recompute the offset from the array actually passed.
- Verify you're not passing an empty array while advancing offsets across chunks.
- Clamp or validate offsets at the API boundary.
Example fix
// before
padding.AddPadding(chunk, chunk.Length + 1, ...); // offset past end
// after
if (offset > chunk.Length) throw new InvalidOperationException("bad offset");
padding.AddPadding(chunk, offset, ...); Defensive patterns
Strategy: validation
Validate before calling
if (input is null || inputOffset < 0 || inputOffset > input.Length)
throw new ArgumentException("inputOffset must be within [0, input.Length]"); Type guard
static bool IsValidOffset(byte[] buf, int offset) => buf is not null && offset >= 0 && offset <= buf.Length;
Try / catch
try { padding.AddPadding(input, offset, len); }
catch (ArgumentException) { /* log offset bookkeeping bug with buffer + offset values */ throw; } Prevention
- Recompute offsets from the array actually passed.
- Assert offset invariants in chunk-processing loops.
- Avoid reusing offsets across differently sized buffers.
When it happens
Trigger: Calling AddPadding(input, offset, ...) where inputOffset > input.Length — e.g. offset pointing past a fully populated array, or passing an empty array with a non-zero offset.
Common situations: Off-by-one in offset bookkeeping across chunked writes; passing the wrong array (empty) with an offset computed for the original; reusing offsets from a previous buffer size.
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 block size
- 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/02136900b6983288.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Crypto/Paddings/TbcPadding.cs:34
public class TbcPadding : IBlockCipherPadding
{
/// <summary>
/// Adds padding to the input array according to the TBC standard.
/// </summary>
/// <param name="input">The input array to be padded.</param>
/// <param name="inputOffset">The offset in the input array where the padding starts.</param>
/// <returns>The number of bytes that were added.</returns>
/// <exception cref="ArgumentException">Thrown when the input array does not have enough space for padding.</exception>
public int AddPadding(byte[] input, int inputOffset)
{
// Calculate the number of bytes to be padded.
var count = input.Length - inputOffset;
byte code;
// Check if the input array has enough space for padding.
if (count < 0)
{
throw new ArgumentException("Not enough space in input array for padding");
}
if (inputOffset > 0)
{
// Get the last bit of the previous byte.
var lastBit = input[inputOffset - 1] & 0x01;
// Set the padding code to 0xFF if the last bit is 0, or 0x00 if the last bit is 1.
code = (byte)(lastBit == 0 ? 0xff : 0x00);
}
else
{
// Get the last bit of the last byte in the input array.
var lastBit = input[^1] & 0x01;
// Set the padding code to 0xff if the last bit is 0, or 0x00 if the last bit is 1.
code = (byte)(lastBit == 0 ? 0xff : 0x00);
}View on GitHub (pinned to 96e2905cab)