{"record":{"id":"efc74b574d82a0ee","repo":"TheAlgorithms/C-Sharp","slug":"not-enough-space-in-input-array-for-padding-x932padding","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/X932Padding.cs","lineNumber":40,"sourceCode":"public class X932Padding(bool useRandomPadding) : IBlockCipherPadding\n{\n    private readonly bool useRandomPadding = useRandomPadding;\n\n    /// <summary>\n    /// Adds padding to the input data according to the X9.23 padding scheme.\n    /// </summary>\n    /// <param name=\"inputData\">The input data array to be padded.</param>\n    /// <param name=\"inputOffset\">The offset in the input data array where the padding should start.</param>\n    /// <returns>The number of padding bytes added.</returns>\n    /// <exception cref=\"ArgumentException\">\n    /// Thrown when the input offset is greater than or equal to the input data length.\n    /// </exception>\n    public int AddPadding(byte[] inputData, int inputOffset)\n    {\n        // Check if the input offset is valid.\n        if (inputOffset >= inputData.Length)\n        {\n            throw new ArgumentException(\"Not enough space in input array for padding\");\n        }\n\n        // Calculate the number of padding bytes needed.\n        var code = (byte)(inputData.Length - inputOffset);\n\n        // Fill the remaining bytes with random or zero bytes\n        while (inputOffset < inputData.Length - 1)\n        {\n            if (!useRandomPadding)\n            {\n                // Use zero bytes if random padding is disabled.\n                inputData[inputOffset] = 0;\n            }\n            else\n            {\n                // Use random bytes if random padding is enabled.\n                inputData[inputOffset] = (byte)RandomNumberGenerator.GetInt32(255);\n            }","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/X932Padding.cs#L22-L58","documentation":"X932Padding.AddPadding pads a block in place: it fills bytes from inputOffset to the end of inputData with zero (or random) bytes and writes the padding byte count into the last byte. The library throws this ArgumentException when inputOffset >= inputData.Length, i.e. there is no room left in the array at the given offset for even a single padding byte.","triggerScenarios":"Calling AddPadding with an inputOffset equal to or larger than the array length, e.g. AddPadding(new byte[16], 16), passing an offset from a different (smaller) buffer, or passing a negative-interpretation offset from a failed earlier calculation. Also passing an empty array with offset 0.","commonSituations":"Cipher block-buffer management bugs where the offset is computed as buffer.Length instead of the free-space start; reusing an offset variable after the buffer was resized; confusing the 'bytes of data' count with the offset; block-mode drivers that already consumed the whole block before padding.","solutions":["Verify inputOffset < inputData.Length before calling AddPadding; clamp or recompute the offset.","Ensure the buffer passed to AddPadding is the full block-size array and the offset points at the first free byte, not the end.","If the buffer is genuinely full (no padding space), copy the data into a larger array (block size + 1 or next block boundary) before padding.","Wrap the call in a try/catch for ArgumentException to surface a clearer domain error with the offset and length values."],"exampleFix":"// before\nint written = buffer.Length;\npadding.AddPadding(buffer, written); // offset == length -> throws\n\n// after\nint written = dataLength % block.GetSize();\nif (written >= buffer.Length) throw new InvalidOperationException(\"buffer full\");\npadding.AddPadding(buffer, written);","handlingStrategy":"validation","validationCode":"if (buffer == null || inputOffset < 0 || inputOffset >= buffer.Length)\n    throw new ArgumentException($\"Need at least one byte of padding space: offset {inputOffset}, length {buffer?.Length}\");","typeGuard":null,"tryCatchPattern":"try { padding.AddPadding(buffer, offset); }\ncatch (ArgumentException ex) { throw new InvalidOperationException($\"Padding failed: offset={offset}, len={buffer.Length}\", ex); }","preventionTips":["Assert inputOffset < buffer.Length before every padding call","Keep block buffers allocated at exactly the cipher block size and track free space separately from the offset","Pass the first free byte index, never buffer.Length or a byte count"],"tags":["csharp","cryptography","padding","argument-out-of-range","buffer-overflow-guard"],"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"}