-
Notifications
You must be signed in to change notification settings - Fork 5.5k
docs: clarify String.GetPinnableReference() returns a read-only interior pointer #130237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -518,8 +518,25 @@ public static bool IsNullOrWhiteSpace([NotNullWhen(false)] string? value) | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns a reference to the first element of the String. If the string is null, an access will throw a NullReferenceException. | ||
| /// Returns a read-only reference to the first element of the String. If the string is null, an access will throw a NullReferenceException. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The <see cref="string"/> type is immutable: the value of a <see cref="string"/> object | ||
| /// cannot be changed after the object is created. The reference returned by this method is | ||
| /// <c>ref readonly</c> and must only be used for reading characters; writing to | ||
| /// the referenced memory is not permitted and results in undefined behavior, including | ||
| /// potential corruption of the managed heap. | ||
| /// </para> | ||
| /// <para> | ||
| /// When using this method (or the equivalent C++/CLI <c>PtrToStringChars</c> function) to | ||
| /// obtain a pinned interior pointer to the string's character data, the resulting pointer | ||
| /// must be treated as read-only. In C++/CLI, <c>PtrToStringChars</c> exposes the pointer | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not think we want to be talking about C++/CLI specific methods in the documentation for System.String. The discussion of C++/CLI |
||
| /// as <c>const wchar_t*</c> to reflect this constraint. Casting away <c>const</c> | ||
| /// and writing through the pointer will silently corrupt the CLR heap and may cause | ||
| /// crashes or incorrect behavior at an unrelated location later in program execution. | ||
| /// </para> | ||
| /// </remarks> | ||
|
Comment on lines
+523
to
+539
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method already returns a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree - this method alone is the least of the problem since it returns The general issue is covered in https://learn.microsoft.com/en-us/dotnet/standard/unsafe-code/best-practices#17-string-mutations . There are many ways to get the pointer to string characters using unsafe code, it applies to all of them. |
||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| [NonVersionable] | ||
| public ref readonly char GetPinnableReference() => ref _firstChar; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The source of truth for official docs in dotnet-api-docs repo has number of other remarks: https://learn.microsoft.com/en-us/dotnet/api/system.string.getpinnablereference?view=net-10.0
It may be better to do the changes there.