TheAlgorithms/C-Sharp · error · ArgumentNullException

Input cannot be null

Error message

Input cannot be null

What it means

GetPaddingCount requires a non-null byte array and throws ArgumentNullException when input is null. It is a straightforward null-guard on the padding-inspection API used e.g. during constant-time unpadding checks.

Solutions

  1. Null-check the buffer before calling GetPaddingCount
  2. Ensure upstream decryption returns an empty array rather than null on failure
  3. Use a helper that coalesces null to an empty array when null is an expected state

Example fix

// before
int count = padding.GetPaddingCount(buffer); // buffer may be null
// after
if (buffer == null) throw new InvalidOperationException("no decrypted data");
int count = padding.GetPaddingCount(buffer);
Defensive patterns

Strategy: type-guard

Validate before calling

if (input == null)
    throw new InvalidOperationException("No decrypted buffer available");
int count = padding.GetPaddingCount(input);

Type guard

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

Try / catch

try { count = padding.GetPaddingCount(input); }
catch (ArgumentNullException) { count = -1; /* signal missing buffer */ }

Prevention

When it happens

Trigger: Passing a null byte[] to GetPaddingCount, typically when an upstream decrypt call returned null or a nullable buffer was forwarded unchecked.

Common situations: Chaining cipher output into padding utilities where a failure path yields null instead of an empty array.

Related errors


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

Appendix: source

Thrown at Algorithms/Crypto/Paddings/Iso10126D2Padding.cs:97

        // Copy the original data into the new array.
        Array.Copy(inputData, 0, output, 0, output.Length);

        return output;
    }

    /// <summary>
    /// Gets the number of padding bytes from the input data array.
    /// </summary>
    /// <param name="input">The input data array that has been padded.</param>
    /// <returns>The number of padding bytes.</returns>
    /// <exception cref="ArgumentNullException">Thrown when the input is null.</exception>
    /// <exception cref="ArgumentException">Thrown when the padding block is corrupted.</exception>
    public int GetPaddingCount(byte[] input)
    {
        if (input == null)
        {
            throw new ArgumentNullException(nameof(input), "Input cannot be null");
        }

        // Get the last byte of the input data as the padding value.
        var lastByte = input[^1];
        var paddingCount = lastByte & 0xFF;

        // Calculate the index where the padding starts.
        var paddingStartIndex = input.Length - paddingCount;
        var paddingCheckFailed = 0;

        // The paddingCheckFailed will be non-zero under the following circumstances:
        // 1. When paddingStartIndex is negative: This happens when paddingCount (the last byte of the input array) is
        // greater than the length of the input array. In other words, the padding count is claiming that there are more
        // padding bytes than there are bytes in the array, which is not a valid scenario.
        // 2. When paddingCount - 1 is negative: This happens when paddingCount is zero or less. Since paddingCount
        // represents the number of padding bytes and is derived from the last byte of the input array, it should always
        // be a positive number. If it's zero or less, it means that either there's no padding, or an invalid negative
        // padding count has shomehow encoded into the last byte of the input array.

View on GitHub (pinned to 96e2905cab)