Skip to content

FromBase64Transform asserts when destination buffer is too small #122807

@vcsjones

Description

@vcsjones

Description

When calling FromBase64Transform .TransformBlock with a destination buffer that is too small, debug builds of the runtime will assert here:

Release builds of the runtime will do ThrowBase64FormatException, implying the base-64 is malformed, not that the destination buffer is too small. The call to Base64.DecodeFromUtf8 is returning DestinationTooSmall, which hits the assert.

I would expect it handle the destination as too small by doing

ThrowHelper.ThrowArgumentOutOfRange(ThrowHelper.ExceptionArgument.outputBuffer);

Reproduction Steps

A simple test that reproduces the issue:

[Fact]
public static void FromBase64Transform_OutputBufferTooSmall()
{
    using FromBase64Transform transform = new();
    byte[] destination = new byte[0];
    byte[] block = "AAAA"u8.ToArray();
    AssertExtensions.Throws<ArgumentOutOfRangeException>("outputBuffer",
        () => transform.TransformBlock(block, 0, block.Length, destination, 0));
}

Expected behavior

Throw ArgumentOutOfRangeException.

Actual behavior

Assert in debug builds:

Expected: typeof(System.ArgumentOutOfRangeException)
Actual:   typeof(Microsoft.VisualStudio.TestPlatform.TestHost.DebugAssertException)
---- Microsoft.VisualStudio.TestPlatform.TestHost.DebugAssertException : Method Debug.Fail failed with 'status == OperationStatus.InvalidData
', and was translated to Microsoft.VisualStudio.TestPlatform.TestHost.DebugAssertException to avoid terminating the process hosting the test.
  Stack Trace:
     at System.AssertExtensions.Throws[T](String expectedParamName, Func`1 testCode) in /Users/vcsjones/Projects/runtime/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs:line 146
   at System.Security.Cryptography.Tests.Base64TransformsTests.FromBase64Transform_OutputBufferTooSmall() in /Users/vcsjones/Projects/runtime/src/libraries/System.Security.Cryptography/tests/Base64TransformsTests.cs:line 286
   at System.RuntimeMethodHandle.InvokeMethod(ObjectHandleOnStack target, Void** arguments, ObjectHandleOnStack sig, BOOL isConstructor, ObjectHandleOnStack result)
   at System.RuntimeMethodHandle.InvokeMethod(ObjectHandleOnStack target, Void** arguments, ObjectHandleOnStack sig, BOOL isConstructor, ObjectHandleOnStack result)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) in /Users/vcsjones/Projects/runtime/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs:line 1165
   at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args) in /Users/vcsjones/Projects/runtime/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.CoreCLR.cs:line 36
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr) in /Users/vcsjones/Projects/runtime/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.cs:line 57

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions