TheAlgorithms/C-Sharp · error · DataLengthException

message

Error message

message

What it means

ValidationUtils.CheckDataLength is a guard that throws a DataLengthException carrying the caller-supplied message when offset > buffer.Length - length, i.e. the requested [offset, offset+length) range does not fit inside the buffer. The literal message string is 'message' because the exception text is entirely provided by the calling cipher via the message parameter.

Solutions

  1. Check offset + length <= buffer.Length before the call, and resize/slice the buffer if not.
  2. Ensure you pass the correct length (buffer.Length - offset), not the total buffer length or a stale length variable.
  3. Read the exception's Message to identify which cipher API raised the guard, and size that buffer to at least one block.
  4. Catch DataLengthException at the crypto facade level and report buffer sizing problems distinctly from crypto failures.

Example fix

// before
engine.ProcessBytes(input, inOff, input.Length, output, 0); // length overruns buffer

// after
ValidationUtils.CheckDataLength(input, inOff, len, "input buffer too short");
engine.ProcessBytes(input, inOff, len, output, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (buffer == null || offset < 0 || length < 0 || offset > buffer.Length - length)
    throw new ArgumentException($"Buffer too small: need {length} bytes at offset {offset}, capacity {buffer?.Length}");

Try / catch

try { engine.ProcessBytes(input, off, len, outBuf, outOff); }
catch (DataLengthException ex) { throw new InvalidOperationException("Input buffer range invalid", ex); }

Prevention

When it happens

Trigger: Any cipher/padding API that internally calls CheckDataLength with a buffer whose remaining capacity is smaller than the requested length, e.g. processing an input buffer with an offset+length that overruns it, or passing a too-small input array to a block cipher's ProcessBytes/DoFinal.

Common situations: Underestimating the input buffer size (block size not accounted for); passing inLen instead of input.Length - inOff; reusing a small scratch buffer after switching to a cipher with a larger block size; off-by-one offsets.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at Algorithms/Crypto/Utils/ValidationUtils.cs:32

public static class ValidationUtils
{
    /// <summary>
    /// Validates that the specified offset and length fit within the bounds of the given buffer.
    /// </summary>
    /// <param name="buffer">The byte array to validate.</param>
    /// <param name="offset">The offset into the byte array where validation should start.</param>
    /// <param name="length">The number of bytes to validate from the specified offset.</param>
    /// <param name="message">The message that describes the error if the exception is thrown.</param>
    /// <exception cref="DataLengthException">Thrown if the offset and length exceed the bounds of the buffer.</exception>
    /// <remarks>
    /// This method ensures that the specified offset and length fit within the bounds of the buffer. If the offset and length
    /// go out of bounds, a <see cref="DataLengthException"/> is thrown with the provided error message.
    /// </remarks>
    public static void CheckDataLength(byte[] buffer, int offset, int length, string message)
    {
        if (offset > (buffer.Length - length))
        {
            throw new DataLengthException(message);
        }
    }

    /// <summary>
    /// Throws an <see cref="OutputLengthException"/> if the specified condition is true.
    /// </summary>
    /// <param name="condition">A boolean condition indicating whether the exception should be thrown.</param>
    /// <param name="message">The message that describes the error if the exception is thrown.</param>
    /// <exception cref="OutputLengthException">Thrown if the condition is true.</exception>
    /// <remarks>
    /// This method performs a simple conditional check for output length validation. If the condition is true, an
    /// <see cref="OutputLengthException"/> is thrown with the provided message.
    /// </remarks>
    public static void CheckOutputLength(bool condition, string message)
    {
        if (condition)
        {
            throw new OutputLengthException(message);

View on GitHub (pinned to 96e2905cab)