Skip to content

Add SpanOfCharAsUtf8StringMarshaller and remove ValueUtf8Converter - #121870

Merged
jkotas merged 7 commits into
mainfrom
copilot/add-utf8spanofchar-marshaller
Nov 21, 2025
Merged

jkotas merged 7 commits into
mainfrom
copilot/add-utf8spanofchar-marshaller

Conversation

Copilot AI commented Nov 21, 2025 •

Copy link
Copy Markdown
Contributor

Make sure that the thread-local P/Invoke last error is not getting corrupted by marshalling post-actions.

Fixes #121868

@github-actions github-actions Bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Nov 21, 2025
Copilot AI changed the title [WIP] Add Utf8SpanOfCharMarshaller implementation Add SpanOfCharAsUtf8StringMarshaller for ReadOnlySpan<char> UTF-8 marshalling Nov 21, 2025
Copilot AI requested a review from jkotas November 21, 2025 05:19
…delete string overloads, and remove ValueUtf8Converter

Co-authored-by: jkotas <[email protected]>
Copilot AI changed the title Add SpanOfCharAsUtf8StringMarshaller for ReadOnlySpan<char> UTF-8 marshalling Add SpanOfCharAsUtf8StringMarshaller and remove ValueUtf8Converter Nov 21, 2025
Copilot AI requested a review from jkotas November 21, 2025 05:54
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@jkotas jkotas removed the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Nov 21, 2025
@jkotas
jkotas requested a review from jozkee November 21, 2025 07:12
@jkotas
jkotas marked this pull request as ready for review November 21, 2025 07:32
Copilot AI review requested due to automatic review settings November 21, 2025 07:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 replaces the manual ValueUtf8Converter helper with a proper custom marshaller SpanOfCharAsUtf8StringMarshaller to fix thread-local P/Invoke last error corruption. The issue occurred because the old pattern called dispose/cleanup operations before capturing the last error from P/Invoke calls, potentially overwriting error codes.

Key changes:

  • Introduces SpanOfCharAsUtf8StringMarshaller as a declarative marshaller for ReadOnlySpan<char> to UTF-8 strings
  • Removes manual UTF-8 conversion code from Interop methods using [MarshalUsing] attribute
  • Fixes operation ordering in FileSystem.Unix.cs to capture last error before cleanup

Reviewed Changes

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

Show a summary per file
File Description
src/libraries/Common/src/System/Runtime/InteropServices/SpanOfCharAsUtf8StringMarshaller.cs New custom marshaller implementation following the standard pattern, with proper memory management and null termination
src/libraries/Common/src/System/Text/ValueUtf8Converter.cs Removed obsolete manual UTF-8 conversion helper
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Stat.Span.cs Simplified to use declarative marshalling instead of manual conversion
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Rename.cs Simplified to use declarative marshalling instead of manual conversion
src/libraries/Common/src/Interop/Unix/System.Native/Interop.ReadLink.cs Simplified to use declarative marshalling instead of manual conversion
src/libraries/Common/src/Interop/Unix/System.Native/Interop.MkDir.cs Simplified to use declarative marshalling instead of manual conversion
src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.Unix.cs Fixed operation order: GetLastError() now called before Dispose() to prevent error corruption; ValueStringBuilder now uses stack allocation
src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems Updated reference from ValueUtf8Converter to SpanOfCharAsUtf8StringMarshaller
src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj Updated reference from ValueUtf8Converter to SpanOfCharAsUtf8StringMarshaller
src/libraries/System.Net.Ping/src/System.Net.Ping.csproj Updated reference from ValueUtf8Converter to SpanOfCharAsUtf8StringMarshaller
src/libraries/System.Net.Ping/tests/FunctionalTests/System.Net.Ping.Functional.Tests.csproj Updated reference from ValueUtf8Converter to SpanOfCharAsUtf8StringMarshaller
src/libraries/System.IO.FileSystem.Watcher/src/System.IO.FileSystem.Watcher.csproj Updated reference from ValueUtf8Converter to SpanOfCharAsUtf8StringMarshaller
src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj Updated reference from ValueUtf8Converter to SpanOfCharAsUtf8StringMarshaller

@jkotas

jkotas commented Nov 21, 2025

Copy link
Copy Markdown
Member

/ba-g deadletter

@jkotas
jkotas merged commit 6eb2b9b into main Nov 21, 2025
@jkotas
jkotas deleted the copilot/add-utf8spanofchar-marshaller branch November 26, 2025 01:42
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 26, 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.

Test failure "System.IO.IOException : Success"

4 participants