{"record":{"id":"84a36abff3fcaa07","repo":"TheAlgorithms/C-Sharp","slug":"input-cannot-be-null","errorCode":null,"errorMessage":"Input cannot be null","messagePattern":"Input cannot be null","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/Iso10126D2Padding.cs","lineNumber":97,"sourceCode":"\n        // Copy the original data into the new array.\n        Array.Copy(inputData, 0, output, 0, output.Length);\n\n        return output;\n    }\n\n    /// <summary>\n    /// Gets the number of padding bytes from the input data array.\n    /// </summary>\n    /// <param name=\"input\">The input data array that has been padded.</param>\n    /// <returns>The number of padding bytes.</returns>\n    /// <exception cref=\"ArgumentNullException\">Thrown when the input is null.</exception>\n    /// <exception cref=\"ArgumentException\">Thrown when the padding block is corrupted.</exception>\n    public int GetPaddingCount(byte[] input)\n    {\n        if (input == null)\n        {\n            throw new ArgumentNullException(nameof(input), \"Input cannot be null\");\n        }\n\n        // Get the last byte of the input data as the padding value.\n        var lastByte = input[^1];\n        var paddingCount = lastByte & 0xFF;\n\n        // Calculate the index where the padding starts.\n        var paddingStartIndex = input.Length - paddingCount;\n        var paddingCheckFailed = 0;\n\n        // The paddingCheckFailed will be non-zero under the following circumstances:\n        // 1. When paddingStartIndex is negative: This happens when paddingCount (the last byte of the input array) is\n        // greater than the length of the input array. In other words, the padding count is claiming that there are more\n        // padding bytes than there are bytes in the array, which is not a valid scenario.\n        // 2. When paddingCount - 1 is negative: This happens when paddingCount is zero or less. Since paddingCount\n        // represents the number of padding bytes and is derived from the last byte of the input array, it should always\n        // be a positive number. If it's zero or less, it means that either there's no padding, or an invalid negative\n        // padding count has shomehow encoded into the last byte of the input array.","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/Iso10126D2Padding.cs#L79-L115","documentation":"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.","triggerScenarios":"Passing a null byte[] to GetPaddingCount, typically when an upstream decrypt call returned null or a nullable buffer was forwarded unchecked.","commonSituations":"Chaining cipher output into padding utilities where a failure path yields null instead of an empty array.","solutions":["Null-check the buffer before calling GetPaddingCount","Ensure upstream decryption returns an empty array rather than null on failure","Use a helper that coalesces null to an empty array when null is an expected state"],"exampleFix":"// before\nint count = padding.GetPaddingCount(buffer); // buffer may be null\n// after\nif (buffer == null) throw new InvalidOperationException(\"no decrypted data\");\nint count = padding.GetPaddingCount(buffer);","handlingStrategy":"type-guard","validationCode":"if (input == null)\n    throw new InvalidOperationException(\"No decrypted buffer available\");\nint count = padding.GetPaddingCount(input);","typeGuard":"static bool HasBytes(byte[]? b) => b is { Length: > 0 };","tryCatchPattern":"try { count = padding.GetPaddingCount(input); }\ncatch (ArgumentNullException) { count = -1; /* signal missing buffer */ }","preventionTips":["Null-check buffers returned from decrypt pipelines","Return empty arrays, never null, from failure paths","Enable nullable reference types to catch this at compile time"],"tags":["csharp","crypto","null-check"],"backgroundTag":"null-argument","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}