{"record":{"id":"41231d473254d203","repo":"TheAlgorithms/C-Sharp","slug":"message","errorCode":null,"errorMessage":"message","messagePattern":"message","errorType":"exception","errorClass":"DataLengthException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Utils/ValidationUtils.cs","lineNumber":32,"sourceCode":"public static class ValidationUtils\n{\n    /// <summary>\n    /// Validates that the specified offset and length fit within the bounds of the given buffer.\n    /// </summary>\n    /// <param name=\"buffer\">The byte array to validate.</param>\n    /// <param name=\"offset\">The offset into the byte array where validation should start.</param>\n    /// <param name=\"length\">The number of bytes to validate from the specified offset.</param>\n    /// <param name=\"message\">The message that describes the error if the exception is thrown.</param>\n    /// <exception cref=\"DataLengthException\">Thrown if the offset and length exceed the bounds of the buffer.</exception>\n    /// <remarks>\n    /// This method ensures that the specified offset and length fit within the bounds of the buffer. If the offset and length\n    /// go out of bounds, a <see cref=\"DataLengthException\"/> is thrown with the provided error message.\n    /// </remarks>\n    public static void CheckDataLength(byte[] buffer, int offset, int length, string message)\n    {\n        if (offset > (buffer.Length - length))\n        {\n            throw new DataLengthException(message);\n        }\n    }\n\n    /// <summary>\n    /// Throws an <see cref=\"OutputLengthException\"/> if the specified condition is true.\n    /// </summary>\n    /// <param name=\"condition\">A boolean condition indicating whether the exception should be thrown.</param>\n    /// <param name=\"message\">The message that describes the error if the exception is thrown.</param>\n    /// <exception cref=\"OutputLengthException\">Thrown if the condition is true.</exception>\n    /// <remarks>\n    /// This method performs a simple conditional check for output length validation. If the condition is true, an\n    /// <see cref=\"OutputLengthException\"/> is thrown with the provided message.\n    /// </remarks>\n    public static void CheckOutputLength(bool condition, string message)\n    {\n        if (condition)\n        {\n            throw new OutputLengthException(message);","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Utils/ValidationUtils.cs#L14-L50","documentation":"ValidationUtils.CheckDataLength is a guard that throws a DataLengthException carrying the caller-supplied message when offset > buffer.Length - length, i.e. the requested [offset, offset+length) range does not fit inside the buffer. The literal message string is 'message' because the exception text is entirely provided by the calling cipher via the message parameter.","triggerScenarios":"Any cipher/padding API that internally calls CheckDataLength with a buffer whose remaining capacity is smaller than the requested length, e.g. processing an input buffer with an offset+length that overruns it, or passing a too-small input array to a block cipher's ProcessBytes/DoFinal.","commonSituations":"Underestimating the input buffer size (block size not accounted for); passing inLen instead of input.Length - inOff; reusing a small scratch buffer after switching to a cipher with a larger block size; off-by-one offsets.","solutions":["Check offset + length <= buffer.Length before the call, and resize/slice the buffer if not.","Ensure you pass the correct length (buffer.Length - offset), not the total buffer length or a stale length variable.","Read the exception's Message to identify which cipher API raised the guard, and size that buffer to at least one block.","Catch DataLengthException at the crypto facade level and report buffer sizing problems distinctly from crypto failures."],"exampleFix":"// before\nengine.ProcessBytes(input, inOff, input.Length, output, 0); // length overruns buffer\n\n// after\nValidationUtils.CheckDataLength(input, inOff, len, \"input buffer too short\");\nengine.ProcessBytes(input, inOff, len, output, 0);","handlingStrategy":"validation","validationCode":"if (buffer == null || offset < 0 || length < 0 || offset > buffer.Length - length)\n    throw new ArgumentException($\"Buffer too small: need {length} bytes at offset {offset}, capacity {buffer?.Length}\");","typeGuard":null,"tryCatchPattern":"try { engine.ProcessBytes(input, off, len, outBuf, outOff); }\ncatch (DataLengthException ex) { throw new InvalidOperationException(\"Input buffer range invalid\", ex); }","preventionTips":["Always compute length as buffer.Length - offset, not a cached total length","Run the same CheckDataLength guard yourself before invoking cipher APIs","Account for the cipher block size when sizing processing buffers"],"tags":["csharp","cryptography","validation","buffer-size","data-length"],"backgroundTag":"value-out-of-range","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"}