Skip to content

Conversation

@jtschuster
Copy link
Member

Fixes #118135

Instead of assuming the Span returned from Get(Un)ManagedValuesSource is the same length as the length parameter, use the length of the span returned from the marshaller methods in the for loop condition. This allows the marshaller to recognize native values that have a different length than expected. In particular, if an array parameter is null, allow the ArrayMarshaller to return empty spans.

Also, updates the Array, Span, ReadOnlySpan, and PointerArray marshallers to check for null unmanaged values when creating unmanaged source/destination spans.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a marshalling issue where null arrays with non-zero length parameters could cause crashes or undefined behavior. The fix ensures that marshalling loops use the actual length of the spans returned from marshaller methods rather than assuming they match the provided length parameter.

Key changes:

  • Modified marshalling source generation to use span lengths instead of provided length parameters
  • Updated array and span marshallers to handle null unmanaged values by returning empty spans
  • Added comprehensive tests for null array scenarios across different marshalling contexts

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs Updates code generation to use span.Length instead of numElements parameter in marshalling loops
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/ArrayMarshaller.cs Adds null checks to return empty spans when unmanaged pointers are null
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/PointerArrayMarshaller.cs Adds null checks to return empty spans when unmanaged pointers are null
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/ReadOnlySpanMarshaller.cs Adds null checks to return empty spans when unmanaged pointers are null
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/SpanMarshaller.cs Adds null checks to return empty spans when unmanaged pointers are null
src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/ComInterfaces/INullableOutArray.cs Test interface for nullable array scenarios with COM interop
src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/ComInterfaces/INullArrayCases.cs Comprehensive test interface covering various null array edge cases
src/libraries/System.Runtime.InteropServices/tests/TestAssets/NativeExports/Arrays.cs Native export methods for testing null array scenarios
src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/ArrayTests.cs Unit tests for null array handling in LibraryImportGenerator
src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Tests/INullableOutArrayTests.cs Test for nullable array marshalling in COM scenarios
src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Tests/INullArrayTests.cs Comprehensive tests for null array edge cases in COM interop

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

@jtschuster jtschuster merged commit 636f559 into dotnet:main Aug 4, 2025
147 of 149 checks passed
radekdoulik pushed a commit to radekdoulik/runtime that referenced this pull request Aug 5, 2025
… and unmarshalling loops (dotnet#118190)

Instead of assuming the Span returned from Get(Un)ManagedValuesSource is the same length as the length parameter, use the length of the span returned from the marshaller methods in the for loop condition. This allows the marshaller to recognize native values that have a different length than expected. In particular, if an array parameter is null, allow the ArrayMarshaller to return empty spans.

Also, updates the Array, Span, ReadOnlySpan, and PointerArray marshallers to check for null unmanaged values when creating unmanaged source/destination spans.

Co-authored-by: Aaron Robinson <[email protected]>
@github-actions github-actions bot locked and limited conversation to collaborators Sep 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

COM source generation generates unexpected buffer marshal logic

3 participants