{"record":{"id":"02136900b6983288","repo":"TheAlgorithms/C-Sharp","slug":"not-enough-space-in-input-array-for-padding-tbcpadding","errorCode":null,"errorMessage":"Not enough space in input array for padding","messagePattern":"Not enough space in input array for padding","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/TbcPadding.cs","lineNumber":34,"sourceCode":"public class TbcPadding : IBlockCipherPadding\n{\n    /// <summary>\n    /// Adds padding to the input array according to the TBC standard.\n    /// </summary>\n    /// <param name=\"input\">The input array to be padded.</param>\n    /// <param name=\"inputOffset\">The offset in the input array where the padding starts.</param>\n    /// <returns>The number of bytes that were added.</returns>\n    /// <exception cref=\"ArgumentException\">Thrown when the input array does not have enough space for padding.</exception>\n    public int AddPadding(byte[] input, int inputOffset)\n    {\n        // Calculate the number of bytes to be padded.\n        var count = input.Length - inputOffset;\n        byte code;\n\n        // Check if the input array has enough space for padding.\n        if (count < 0)\n        {\n            throw new ArgumentException(\"Not enough space in input array for padding\");\n        }\n\n        if (inputOffset > 0)\n        {\n            // Get the last bit of the previous byte.\n            var lastBit = input[inputOffset - 1] & 0x01;\n\n            // Set the padding code to 0xFF if the last bit is 0, or 0x00 if the last bit is 1.\n            code = (byte)(lastBit == 0 ? 0xff : 0x00);\n        }\n        else\n        {\n            // Get the last bit of the last byte in the input array.\n            var lastBit = input[^1] & 0x01;\n\n            // Set the padding code to 0xff if the last bit is 0, or 0x00 if the last bit is 1.\n            code = (byte)(lastBit == 0 ? 0xff : 0x00);\n        }","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/TbcPadding.cs#L16-L52","documentation":"TbcPadding.AddPadding checks that the input array can hold at least the existing data (input.Length - inputOffset must be >= 0). A negative count means inputOffset exceeds the array length, so the method throws ArgumentException instead of reading or writing out of bounds.","triggerScenarios":"Calling AddPadding(input, offset, ...) where inputOffset > input.Length — e.g. offset pointing past a fully populated array, or passing an empty array with a non-zero offset.","commonSituations":"Off-by-one in offset bookkeeping across chunked writes; passing the wrong array (empty) with an offset computed for the original; reusing offsets from a previous buffer size.","solutions":["Ensure inputOffset is within [0, input.Length].","Recompute the offset from the array actually passed.","Verify you're not passing an empty array while advancing offsets across chunks.","Clamp or validate offsets at the API boundary."],"exampleFix":"// before\npadding.AddPadding(chunk, chunk.Length + 1, ...); // offset past end\n// after\nif (offset > chunk.Length) throw new InvalidOperationException(\"bad offset\");\npadding.AddPadding(chunk, offset, ...);","handlingStrategy":"validation","validationCode":"if (input is null || inputOffset < 0 || inputOffset > input.Length)\n    throw new ArgumentException(\"inputOffset must be within [0, input.Length]\");","typeGuard":"static bool IsValidOffset(byte[] buf, int offset) => buf is not null && offset >= 0 && offset <= buf.Length;","tryCatchPattern":"try { padding.AddPadding(input, offset, len); }\ncatch (ArgumentException) { /* log offset bookkeeping bug with buffer + offset values */ throw; }","preventionTips":["Recompute offsets from the array actually passed.","Assert offset invariants in chunk-processing loops.","Avoid reusing offsets across differently sized buffers."],"tags":["csharp","cryptography","buffer-size","padding"],"backgroundTag":"invalid-argument-value","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"}