Repository navigation
[mono] Fix SizeConst array marshalling overflows - #137082
Merged
Merged
Conversation
For example a type declared as
```
[StructLayout(LayoutKind.Sequential)]
internal struct MyStruct
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public int[] Values;
}
```
can be marshalled with the `Marshal.StructureToPtr(str, buffer, false);` to buffer. Mono had problems if the struct to marshal contained an array that didn't match the `SizeConst` attribute argument. For blittable types, like in the above definition, if the array was smaller, then we would copy more bytes than the array holds. For non-blittable types, if the array was larger, we would write more bytes, overflowing the destination buffer. The fix throws exception at runtime if the array doesn't contain enough elements and limits the copying to the `SizeConst` amount, preventing both source read and destination write overflows.
BrzVlad
requested review from
steveisok,
thaystg and
vitek-karas
as code owners
November 6, 2026 12:39
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/interop-contrib |
lateralusX
approved these changes
Nov 6, 2026
This was referenced Nov 6, 2026
Member
Author
|
/backport to release/11.0 |
Contributor
|
Started backporting to |
Member
Author
|
/backport to release/10.0 |
Contributor
|
Started backporting to |
This was referenced Nov 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For example a type declared as
can be marshalled with the
Marshal.StructureToPtr(str, buffer, false);to buffer. Mono had problems if the struct to marshal contained an array that didn't match theSizeConstattribute argument. For blittable types, like in the above definition, if the array was smaller, then we would copy more bytes than the array holds. For non-blittable types, if the array was larger, we would write more bytes, overflowing the destination buffer. The fix throws exception at runtime if the array doesn't contain enough elements and limits the copying to theSizeConstamount, preventing both source read and destination write overflows.