TheAlgorithms/C-Sharp · error · ArgumentException

No padding found.

Error message

No padding found.

What it means

TBC (Trailing-Bit-Complement) padding is defined over at least one byte, so GetPaddingCount rejects empty arrays with ArgumentException: there is no last byte whose value could encode the padding. An empty input cannot have TBC padding by definition.

Solutions

  1. Guard against empty arrays before calling GetPaddingCount.
  2. Treat an empty buffer as an upstream error and throw/log where it originated.
  3. Verify the decrypt stage produced at least one block of data.
  4. Skip unpadding for empty payloads if your protocol defines them as valid.

Example fix

// before
var count = padding.GetPaddingCount(chunk); // chunk may be empty
// after
if (chunk.Length == 0) throw new InvalidOperationException("Received empty block");
var count = padding.GetPaddingCount(chunk);
Defensive patterns

Strategy: validation

Validate before calling

if (input is null || input.Length == 0)
    throw new InvalidOperationException("Cannot unpad an empty buffer — upstream produced no data");

Type guard

static bool IsNonEmptyBlock(byte[]? input) => input is { Length: > 0 };

Try / catch

try { count = padding.GetPaddingCount(input); }
catch (ArgumentException) when (input.Length == 0) { /* skip empty chunk */ }

Prevention

When it happens

Trigger: Calling GetPaddingCount(new byte[0]) or Array.Empty<byte>() — e.g. decryption returned an empty buffer, or an empty chunk was passed through the unpadding pipeline.

Common situations: Empty input files or network messages being processed as if they contained a block; a failed decrypt returning an empty array; chunked pipelines forwarding an empty final chunk.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at Algorithms/Crypto/Paddings/TbcPadding.cs:128

    /// <summary>
    /// Returns the number of padding bytes in a byte array according to the Trailing-Bit-Complement padding algorithm.
    /// </summary>
    /// <param name="input">The byte array to check for padding.</param>
    /// <returns>The number of padding bytes in the input array.</returns>
    /// <remarks>
    /// This method assumes that the input array has been padded with either 0x00 or 0xFF bytes, depending on the last
    /// bit of the original data. The method works by iterating backwards from the end of the array and counting the
    /// number of bytes that match the padding code. The method uses bitwise operations to optimize the performance and
    /// avoid branching. If the input array is not padded or has an invalid padding, the method may return incorrect
    /// results.
    /// </remarks>
    public int GetPaddingCount(byte[] input)
    {
        var length = input.Length;

        if (length == 0)
        {
            throw new ArgumentException("No padding found.");
        }

        // Get the value of the last byte as the padding value
        var paddingValue = input[--length] & 0xFF;
        var paddingCount = 1; // Start count at 1 for the last byte
        var countingMask = -1; // Initialize counting mask

        // Check if there is no padding
        if (paddingValue != 0 && paddingValue != 0xFF)
        {
            throw new ArgumentException("No padding found");
        }

        // Loop backwards through the array
        for (var i = length - 1; i >= 0; i--)
        {
            var currentByte = input[i] & 0xFF;

View on GitHub (pinned to 96e2905cab)