-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
CA1838 Avoid 'StringBuilder' parameters for P/Invokes #7186
Merged
Merged
Changes from 17 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
994b696
CA1838 Avoid 'StringBuilder' parameters for P/Invokes
elachlan 4b519a9
revert ruleset change
elachlan 3a88ed9
rebase
elachlan 638775f
Enable warning on CA1838
elachlan 751fea3
Merge branch 'main' into CA1838
elachlan 661c2c8
CA1838 Avoid 'StringBuilder' parameters for P/Invokes. Consider using…
elachlan 4f0424c
trying again
elachlan e042e66
Changes from review
elachlan 4ddf661
GetGacPath changes
elachlan 8f0c31c
Making sure we are form strings properly
elachlan 04e4a1b
changes from review
elachlan 903f35d
Remove unused field
elachlan 69cf05f
Use ArrayPool in GetRuntimeVersion and fix usage in GetModuleFileName
elachlan 300179b
Refactor GetRuntimeVersion to remove loop and use stackalloc
elachlan c392277
changes from review
elachlan de3618c
Fix conflict and xml doc
elachlan a0178d7
Merge branch 'main' into CA1838
elachlan 54878ac
Fix possible StackOverflow in GetShortPathName/GetLongPathName by mov…
elachlan 567cc08
Merge branch 'CA1838' of github.com:elachlan/msbuild into CA1838
elachlan 8af24e5
remove unneeded unsafe from pinvoke definitions
elachlan 031af51
Added cached GAC Path and changes from review
elachlan b4f6bf7
Add documentation to Pinvoke
elachlan b5f60db
Changes from review
elachlan 53d3da4
Change from review
elachlan 0a97bfd
Add VerifyThrow instead of loop condition
elachlan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -569,30 +569,36 @@ internal static string GetRuntimeVersion(string path) | |
#if FEATURE_MSCOREE | ||
if (NativeMethodsShared.IsWindows) | ||
{ | ||
StringBuilder runtimeVersion; | ||
uint hresult; | ||
#if DEBUG | ||
// Just to make sure and exercise the code that doubles the size | ||
// every time GetRequestedRuntimeInfo fails due to insufficient buffer size. | ||
// Just to make sure and exercise the code that uses dwLength to allocate the buffer | ||
// when GetRequestedRuntimeInfo fails due to insufficient buffer size. | ||
int bufferLength = 1; | ||
#else | ||
int bufferLength = 11; // 11 is the length of a runtime version and null terminator v2.0.50727/0 | ||
elachlan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#endif | ||
do | ||
{ | ||
runtimeVersion = new StringBuilder(bufferLength); | ||
hresult = NativeMethods.GetFileVersion(path, runtimeVersion, bufferLength, out _); | ||
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. That said, I'm not a fan of this code, so I'm glad it's gone 😁 |
||
bufferLength *= 2; | ||
} while (hresult == NativeMethodsShared.ERROR_INSUFFICIENT_BUFFER); | ||
|
||
if (hresult == NativeMethodsShared.S_OK) | ||
{ | ||
return runtimeVersion.ToString(); | ||
} | ||
else | ||
unsafe | ||
{ | ||
return String.Empty; | ||
} | ||
// Allocate an initial buffer | ||
char* runtimeVersionInitial = stackalloc char[bufferLength]; | ||
|
||
// Run GetFileVersion, this should succeed using the initial buffer. | ||
// It also returns the dwLength which is used if there is insufficient buffer. | ||
uint hresult = NativeMethods.GetFileVersion(path, runtimeVersionInitial, bufferLength, out int dwLength); | ||
|
||
if (hresult == NativeMethodsShared.ERROR_INSUFFICIENT_BUFFER) | ||
{ | ||
// Allocate new buffer based on the returned length. | ||
char* runtimeVersion = stackalloc char[dwLength]; | ||
|
||
// Get the RuntimeVersion in this second call. | ||
bufferLength = dwLength; | ||
hresult = NativeMethods.GetFileVersion(path, runtimeVersion, bufferLength, out dwLength); | ||
return hresult == NativeMethodsShared.S_OK ? new string(runtimeVersion, 0, dwLength - 1) : string.Empty; | ||
elachlan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return hresult == NativeMethodsShared.S_OK ? new string(runtimeVersionInitial, 0, dwLength - 1) : string.Empty; | ||
} | ||
} | ||
else | ||
{ | ||
|
@@ -601,14 +607,14 @@ internal static string GetRuntimeVersion(string path) | |
#else | ||
return ManagedRuntimeVersionReader.GetRuntimeVersion(path); | ||
#endif | ||
} | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Import assembly dependencies. | ||
/// </summary> | ||
/// <returns>The array of assembly dependencies.</returns> | ||
private AssemblyNameExtension[] ImportAssemblyDependencies() | ||
/// <summary> | ||
/// Import assembly dependencies. | ||
/// </summary> | ||
/// <returns>The array of assembly dependencies.</returns> | ||
private AssemblyNameExtension[] ImportAssemblyDependencies() | ||
elachlan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
#if !FEATURE_ASSEMBLYLOADCONTEXT | ||
var asmRefs = new List<AssemblyNameExtension>(); | ||
|
@@ -790,7 +796,7 @@ private static AssemblyNameExtension ConstructAssemblyName(IntPtr asmMetaPtr, ch | |
// Construct the assembly name. (Note asmNameLength should/must be > 0.) | ||
var assemblyName = new AssemblyName | ||
{ | ||
Name = new string(asmNameBuf, 0, (int) asmNameLength - 1), | ||
Name = new string(asmNameBuf, 0, (int)asmNameLength - 1), | ||
Version = new Version( | ||
asmMeta.usMajorVersion, | ||
asmMeta.usMinorVersion, | ||
|
@@ -911,7 +917,7 @@ public static string GetRuntimeVersion(string path) | |
// Read the PE header signature | ||
|
||
sr.BaseStream.Position = peHeaderOffset; | ||
if (!ReadBytes(sr, (byte) 'P', (byte) 'E', 0, 0)) | ||
if (!ReadBytes(sr, (byte)'P', (byte)'E', 0, 0)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
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.
This is potentially dangerous. If memory serves, length could be up to ~32,000 characters here, so this could end up requesting 32K of stack space. Depending on what else is going on in the call stack and what OS you're on (some OSes have much smaller stacks by default), this could potentially lead to a stack overflow. We typically try to keep stackalloc maximums closer to around 256 bytes and generally not more than 1K... I think the max we have somewhere in dotnet/runtime is around 2K. Typical pattern is something like this:
or if you're in a context that has [SkipLocalsInit], it's generally better to use a const with the stackalloc, e.g.
If you want a pointer, you can then use
fixed
with the span.