-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Removed CA1838 warning suppressions on p/invokes for StringBuilders. #4751
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 |
|---|---|---|
|
|
@@ -11,7 +11,6 @@ | |
| using System.Globalization; | ||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using static Interop; | ||
| using static Interop.Ole32; | ||
|
|
||
|
|
@@ -1323,40 +1322,43 @@ public unsafe override void SetValue(object component, object value) | |
| } | ||
| else if (errorInfo is null) | ||
| { | ||
| StringBuilder strMessage = new StringBuilder(256); | ||
|
|
||
| uint result = Kernel32.FormatMessageW( | ||
| Kernel32.FormatMessageOptions.FROM_SYSTEM | Kernel32.FormatMessageOptions.IGNORE_INSERTS, | ||
| IntPtr.Zero, | ||
| (uint)hr, | ||
| Kernel32.GetThreadLocale().RawValue, | ||
| strMessage, | ||
| 255, | ||
| IntPtr.Zero); | ||
|
|
||
| if (result == 0) | ||
| Span<char> buffer = stackalloc char[256]; | ||
| fixed (char* valueChars = buffer) | ||
| { | ||
| errorInfo = string.Format(CultureInfo.CurrentCulture, string.Format(SR.DispInvokeFailed, "SetValue", hr)); | ||
| } | ||
| else | ||
| { | ||
| errorInfo = TrimNewline(strMessage); | ||
| uint length = Kernel32.FormatMessageW( | ||
| Kernel32.FormatMessageOptions.FROM_SYSTEM | | ||
| Kernel32.FormatMessageOptions.IGNORE_INSERTS, | ||
| IntPtr.Zero, | ||
| (uint)hr, | ||
| Kernel32.GetThreadLocale().RawValue, | ||
| valueChars, | ||
| buffer.Length, | ||
| IntPtr.Zero); | ||
| if (length == 0) | ||
|
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. Really should test this code to see what it does with insufficient buffer sizes and handle growth if needed. Can't stack alloc in that case. Fine for that to be a follow up issue. |
||
| { | ||
| errorInfo = string.Format(CultureInfo.CurrentCulture, | ||
| string.Format(SR.DispInvokeFailed, "SetValue", hr)); | ||
| } | ||
| else | ||
| { | ||
| errorInfo = TrimNewline(buffer.Slice(0, (int)length).SliceAtFirstNull().ToString()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| throw new ExternalException(errorInfo, (int)hr); | ||
| } | ||
| } | ||
|
|
||
| private static string TrimNewline(StringBuilder errorInfo) | ||
| private static string TrimNewline(string errorInfo) | ||
| { | ||
| int index = errorInfo.Length - 1; | ||
|
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. Much better to have a static array of these chars and just call |
||
| while (index >= 0 && (errorInfo[index] == '\n' || errorInfo[index] == '\r')) | ||
| { | ||
| index--; | ||
| } | ||
|
|
||
| return errorInfo.ToString(0, index + 1); | ||
| return errorInfo.Substring(0, index + 1); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.