From 38e2beb43a73e27dbd49481bd57b396ed55fdf45 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 6 Jan 2026 00:11:14 +0000 Subject: [PATCH 1/7] Initial plan From a4b14e9c14428342f47e9abca7f8ed5833428d56 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 6 Jan 2026 00:19:38 +0000 Subject: [PATCH 2/7] Add InlineArrayAttribute reference to StructLayoutAttribute docs Co-authored-by: agocke <515774+agocke@users.noreply.github.com> --- .../apply-interop-attributes.md | 2 +- .../customize-struct-marshalling.md | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/docs/standard/native-interop/apply-interop-attributes.md b/docs/standard/native-interop/apply-interop-attributes.md index 6c8b3f663e324..6dc1e86e52cb0 100644 --- a/docs/standard/native-interop/apply-interop-attributes.md +++ b/docs/standard/native-interop/apply-interop-attributes.md @@ -48,7 +48,7 @@ The namespace provides three categories of ||Indicates that the data in a field or parameter must be marshalled from a called object back to its caller.| ||Suppresses the HRESULT or retval signature transformation that normally takes place during interoperation calls. The attribute affects marshalling as well as type library exporting.

COM interop tools can apply this attribute.| ||Specifies the ProgID of a .NET class. Can be used to attribute classes.| -||Controls the physical layout of the fields of a class.

COM interop tools can apply this attribute.| +||Controls the physical layout of the fields of a class. For information about creating fixed-size buffers, see instead.

COM interop tools can apply this attribute.| ## Conversion-Tool Attributes diff --git a/docs/standard/native-interop/customize-struct-marshalling.md b/docs/standard/native-interop/customize-struct-marshalling.md index 4438549c39536..56c154d8e9ec7 100644 --- a/docs/standard/native-interop/customize-struct-marshalling.md +++ b/docs/standard/native-interop/customize-struct-marshalling.md @@ -26,6 +26,61 @@ Sometimes the default marshalling rules for structures aren't exactly what you n ❌ AVOID using `LayoutKind.Explicit` when marshalling structures on non-Windows platforms if you need to target runtimes before .NET Core 3.0. The .NET Core runtime before 3.0 doesn't support passing explicit structures by value to native functions on Intel or AMD 64-bit non-Windows systems. However, the runtime supports passing explicit structures by reference on all platforms. +## Fixed-size buffers and inline arrays + +If you need to define a fixed-size buffer in a structure, don't use the property to allocate extra space. The `Size` property controls the unmanaged layout of the type (the layout used when calling `Marshal.StructureToPtr`) and only affects the managed layout for blittable types. For non-blittable types, the runtime only allocates space based on the actual fields, which can lead to memory corruption if you attempt to use the extra space. + +❌ AVOID using `StructLayoutAttribute.Size` to create fixed-size buffers in managed types. + +✔️ DO use to define fixed-size buffers in modern .NET (C# 12 and later). + +The following example shows the incorrect approach that can lead to memory corruption: + +```csharp +// ❌ DON'T: This is dangerous and can corrupt memory +[StructLayout(LayoutKind.Explicit, Size = 100)] +class UnsafeBuffer +{ + [FieldOffset(0)] + private byte b; + + unsafe void UseBuffer() + { + fixed (byte* p = &b) + { + // This treats p as a 100-byte buffer, but only 1 byte is allocated + // Writing beyond the first byte corrupts memory + } + } +} +``` + +Instead, use `InlineArrayAttribute` to create a properly sized buffer: + +```csharp +// ✔️ DO: Use InlineArrayAttribute for fixed-size buffers +class SafeBuffer +{ + private Buffer100 buffer; + + unsafe void UseBuffer() + { + fixed (byte* p = buffer) + { + // The buffer is guaranteed to be 100 bytes + } + } +} + +[InlineArray(100)] +internal struct Buffer100 +{ + private byte _element0; +} +``` + +For more information about inline arrays, see the [C# language reference](../../csharp/language-reference/builtin-types/struct.md#inline-arrays). + ## Customizing Boolean field marshalling Native code has many different Boolean representations. On Windows alone, there are three ways to represent Boolean values. The runtime doesn't know the native definition of your structure, so the best it can do is make a guess on how to marshal your Boolean values. The .NET runtime provides a way to indicate how to marshal your Boolean field. The following examples show how to marshal .NET `bool` to different native Boolean types. From 29de4dd3f989f8c8dba8f58fcd0b5bf9f2a58e6a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 6 Jan 2026 00:20:45 +0000 Subject: [PATCH 3/7] Change examples from class to struct for consistency Co-authored-by: agocke <515774+agocke@users.noreply.github.com> --- docs/standard/native-interop/customize-struct-marshalling.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/standard/native-interop/customize-struct-marshalling.md b/docs/standard/native-interop/customize-struct-marshalling.md index 56c154d8e9ec7..7589b1fbbc5b5 100644 --- a/docs/standard/native-interop/customize-struct-marshalling.md +++ b/docs/standard/native-interop/customize-struct-marshalling.md @@ -39,7 +39,7 @@ The following example shows the incorrect approach that can lead to memory corru ```csharp // ❌ DON'T: This is dangerous and can corrupt memory [StructLayout(LayoutKind.Explicit, Size = 100)] -class UnsafeBuffer +struct UnsafeBuffer { [FieldOffset(0)] private byte b; @@ -59,7 +59,7 @@ Instead, use `InlineArrayAttribute` to create a properly sized buffer: ```csharp // ✔️ DO: Use InlineArrayAttribute for fixed-size buffers -class SafeBuffer +struct SafeBuffer { private Buffer100 buffer; From 4a6ade2be25ad588edf7e152543fb3d6ce2bb195 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Tue, 7 Jul 2026 11:06:02 -0700 Subject: [PATCH 4/7] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Aaron R Robinson --- docs/standard/native-interop/customize-struct-marshalling.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/standard/native-interop/customize-struct-marshalling.md b/docs/standard/native-interop/customize-struct-marshalling.md index 7589b1fbbc5b5..7a5fa71f79edd 100644 --- a/docs/standard/native-interop/customize-struct-marshalling.md +++ b/docs/standard/native-interop/customize-struct-marshalling.md @@ -49,6 +49,7 @@ struct UnsafeBuffer fixed (byte* p = &b) { // This treats p as a 100-byte buffer, but only 1 byte is allocated + // This may interpret p as a 100-byte buffer, but only 1 byte is allocated // Writing beyond the first byte corrupts memory } } @@ -65,7 +66,7 @@ struct SafeBuffer unsafe void UseBuffer() { - fixed (byte* p = buffer) + fixed (byte* p = &buffer[0]) { // The buffer is guaranteed to be 100 bytes } From eeb4bf17f645f12dc894a976eb5ee9f58334b394 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Tue, 7 Jul 2026 11:06:31 -0700 Subject: [PATCH 5/7] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- docs/standard/native-interop/apply-interop-attributes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard/native-interop/apply-interop-attributes.md b/docs/standard/native-interop/apply-interop-attributes.md index 6dc1e86e52cb0..6ec63fa324b2e 100644 --- a/docs/standard/native-interop/apply-interop-attributes.md +++ b/docs/standard/native-interop/apply-interop-attributes.md @@ -48,7 +48,7 @@ The namespace provides three categories of ||Indicates that the data in a field or parameter must be marshalled from a called object back to its caller.| ||Suppresses the HRESULT or retval signature transformation that normally takes place during interoperation calls. The attribute affects marshalling as well as type library exporting.

COM interop tools can apply this attribute.| ||Specifies the ProgID of a .NET class. Can be used to attribute classes.| -||Controls the physical layout of the fields of a class. For information about creating fixed-size buffers, see instead.

COM interop tools can apply this attribute.| +||Controls the physical layout of the fields of a class. For managed fixed-size buffers, see [Fixed-size buffers and inline arrays](customize-struct-marshalling.md#fixed-size-buffers-and-inline-arrays) and .

COM interop tools can apply this attribute.| ## Conversion-Tool Attributes From 966d51968553d209400319e70c72c12c0e3a1668 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Tue, 7 Jul 2026 11:41:52 -0700 Subject: [PATCH 6/7] Apply suggestion from @AaronRobinsonMSFT --- docs/standard/native-interop/customize-struct-marshalling.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/standard/native-interop/customize-struct-marshalling.md b/docs/standard/native-interop/customize-struct-marshalling.md index 7a5fa71f79edd..428b9119983eb 100644 --- a/docs/standard/native-interop/customize-struct-marshalling.md +++ b/docs/standard/native-interop/customize-struct-marshalling.md @@ -50,6 +50,7 @@ struct UnsafeBuffer { // This treats p as a 100-byte buffer, but only 1 byte is allocated // This may interpret p as a 100-byte buffer, but only 1 byte is allocated + // This may interpret p as a 100-byte buffer, but only 1 byte is allocated // Writing beyond the first byte corrupts memory } } From 0f4feb6ffbb4cd5e4639b1796f7fbd716e85c9e9 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Tue, 7 Jul 2026 11:42:30 -0700 Subject: [PATCH 7/7] Clarify comment about buffer allocation in UseBuffer Updated comment to clarify memory allocation issue. --- docs/standard/native-interop/customize-struct-marshalling.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/standard/native-interop/customize-struct-marshalling.md b/docs/standard/native-interop/customize-struct-marshalling.md index 428b9119983eb..d2f1eb2948498 100644 --- a/docs/standard/native-interop/customize-struct-marshalling.md +++ b/docs/standard/native-interop/customize-struct-marshalling.md @@ -48,8 +48,6 @@ struct UnsafeBuffer { fixed (byte* p = &b) { - // This treats p as a 100-byte buffer, but only 1 byte is allocated - // This may interpret p as a 100-byte buffer, but only 1 byte is allocated // This may interpret p as a 100-byte buffer, but only 1 byte is allocated // Writing beyond the first byte corrupts memory }