TheAlgorithms/C-Sharp · error · ArgumentNullException
Input cannot be null
Error message
Input cannot be null
What it means
GetPaddingCount rejects input that is null (or lacks valid PKCS7 padding) and reports it as a generic ArgumentException instead of ArgumentNullException, since the method requires a non-null padded buffer to count trailing padding bytes.
Solutions
- Check the array for null before calling GetPaddingCount.
- Ensure the decrypt step always returns a buffer (or throw) instead of null.
- If null means 'no data', skip unpadding entirely rather than calling.
- Fail fast with a clear message at the data-source boundary.
Example fix
// before
var count = padding.GetPaddingCount(decrypted); // decrypted may be null
// after
if (decrypted is null) throw new InvalidOperationException("Decryption produced no data");
var count = padding.GetPaddingCount(decrypted); Defensive patterns
Strategy: type-guard
Validate before calling
if (decrypted is null) throw new InvalidOperationException("Decryption returned null"); Type guard
static bool IsUnpaddable(byte[]? input) => input is not null && input.Length > 0;
Try / catch
try { count = padding.GetPaddingCount(input); }
catch (ArgumentNullException) { count = -1; /* treat as no data */ } Prevention
- Make decrypt methods non-null-returning (throw on failure instead).
- Null-check results from external decrypt/cache/deserialize calls.
- Use nullable reference types to catch null flow at compile time.
When it happens
Trigger: Calling GetPaddingCount(null) — typically when a decryption step returned null (failed decrypt API, null from a cache or deserializer) and the result was passed straight through.
Common situations: A decrypt method that returns null on failure; nullable byte[] from external data pipelines; forgetting to handle an empty/failed read before unpadding.
Related errors
- Invalid block size
- Not enough space in input array for padding
- 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/dec1317364544250.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Crypto/Paddings/Pkcs7Padding.cs:132
return output;
}
/// <summary>
/// Gets the number of padding bytes in the given input data according to the PKCS7 padding scheme.
/// </summary>
/// <param name="input">The input data with PKCS7 padding. Must not be null and must have a valid padding.</param>
/// <returns>The number of padding bytes in the input data.</returns>
/// <exception cref="ArgumentException">
/// Thrown if the input data is null or has an invalid padding.
/// </exception>
/// <remarks>
/// This method uses bitwise operations to avoid branching.
/// </remarks>
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;
// Check if the padding start index is negative or greater than the input length.
// This is done by using bitwise operations to avoid branching.
// If the padding start index is negative, then its most significant bit will be 1.
// If the padding count is greater than the block size, then its most significant bit will be 1.
// By ORing these two cases, we can get a non-zero value rif either of them is true.
// By shifting this value right by 31 bits, we can get either 0 or -1 as the result.
paddingCheckFailed = (paddingStartIndex | (paddingCount - 1)) >> 31;
View on GitHub (pinned to 96e2905cab)