Skip to content
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

Fix ArgumentException when recurse is enabled and two file target specifiers generates the same file paths #2438

Merged

Conversation

eddynaka
Copy link
Collaborator

@eddynaka eddynaka commented Feb 1, 2022

Description

If you pass two targets like c:\path1\1.dll, c:\path1\path2\1.dll and recurse is enabled, the MultithreadedAnalyzeCommandBase would generate the following searches:
c:\path1\ -> search using recurse looking for 1.dll
c:\path1\path2\ -> search using recurse looking for 1.dll

This would generate path duplications, causing an ArgumentException when hashing:

error ERR999.UnhandledEngineException : System.ArgumentException: The key already existed in the dictionary.
at System.Collections.Concurrent.ConcurrentDictionary`2.System.Collections.Generic.IDictionary<TKey,TValue>.Add(TKey key, TValue value)
at Microsoft.CodeAnalysis.Sarif.Driver.MultithreadedAnalyzeCommandBase`2.HashAsync() in /_/src/Sarif.Driver/Sdk/MultithreadedAnalyzeCommandBase.cs:line 494
ArgumentException: The key already existed in the dictionary.
at System.Collections.Concurrent.ConcurrentDictionary`2.System.Collections.Generic.IDictionary<TKey,TValue>.Add(TKey key, TValue value)
at Microsoft.CodeAnalysis.Sarif.Driver.MultithreadedAnalyzeCommandBase`2.HashAsync()

Tests

Created a test where I'm passing two target specifiers that will list the same file. When that happens, no exception should occur since we will start checking if we already added the item to the IDictionary.

@eddynaka eddynaka changed the title Fixing ArgumentException when passing two filePaths that generate duplicated file analysis and recurse is enabled. Fix ArgumentException when recurse is enabled and two file target specifiers generates the same file paths Feb 1, 2022
@@ -1509,7 +1561,7 @@ private static IFileSystem CreateDefaultFileSystemForResultsCaching(IList<string
mockFileSystem.Setup(x => x.FileReadAllText(It.Is<string>(f => f == fullyQualifiedName))).Returns(logFileContents);

mockFileSystem.Setup(x => x.FileOpenRead(It.Is<string>(f => f == fullyQualifiedName)))
.Returns(new MemoryStream(Encoding.UTF8.GetBytes(generateSameInput ? logFileContents : fileNameWithoutExtension)));
.Returns(new NonDisposingDelegatingStream(new MemoryStream(Encoding.UTF8.GetBytes(generateSameInput ? logFileContents : fileNameWithoutExtension))));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NonDisposingDelegatingStream

This will enable us to read over and over during tests without generating an exception where the stream is already closed.

@eddynaka eddynaka marked this pull request as ready for review February 1, 2022 21:53
@@ -6,6 +6,7 @@
* BUGFIX: Adjust Json Serialization field order for ReportingDescriptor and skip emit empty AutomationDetails node. [#2420](https://github.com/microsoft/sarif-sdk/pull/2420)
* BREAKING: Fix `InvalidOperationException` when using PropertiesDictionary in a multithreaded application, and remove `[Serializable]` from it. Now use of BinaryFormatter on it will result in `SerializationException`: Type `PropertiesDictionary` is not marked as serializable. [#2415](https://github.com/microsoft/sarif-sdk/pull/2415)
* BREAKING: `SarifLogger` now emits an artifacts table entry if `artifactLocation` is not null for tool configuration and tool execution notifications. [#2437](https://github.com/microsoft/sarif-sdk/pull/2437)
* BUGFIX: Fix `ArgumentException` when recurse is enabled and two file target specifiers generates the same file path. [#2438](https://github.com/microsoft/sarif-sdk/pull/2438)
Copy link
Member

@michaelcfanning michaelcfanning Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recurse

--recurse #Closed


Action action = () =>
{
foreach (var testCase in testCases)
Copy link
Member

@michaelcfanning michaelcfanning Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreach (var testCase in testCases)

foreach (bool multithreaded in new bool[] { true, false} #Closed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!
thanks for the suggestion!

Copy link
Member

@michaelcfanning michaelcfanning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@eddynaka eddynaka enabled auto-merge (squash) February 1, 2022 21:59
@eddynaka eddynaka enabled auto-merge (squash) February 1, 2022 22:04
@eddynaka eddynaka merged commit 7851441 into main Feb 1, 2022
@eddynaka eddynaka deleted the users/ednakamu/fixing-recurse-issue-targets-specifier branch February 1, 2022 22:12
yongyan-gh pushed a commit that referenced this pull request Feb 8, 2022
…pecifiers generates the same file paths (#2438)

* Fixing ArgumentException when passing two filePaths that generates duplicated file analysis

* Fixing dotnet-format issues and updating releasehistory

* Removing comments

* Addressing PR feedback

* Addressing PR feedback
michaelcfanning added a commit that referenced this pull request Mar 2, 2022
)

* Add new visitor to get deterministic SARIF log by sorting results

* Fix dotnet format issue

* updating format

* remove unnecessary using

Format & minor fixes

* Add Run Comparer to support sorting logs with multiple runs.

* Add command argument unit tests

fix dotnet format

* use ContainsKey to avoid allocating variable

* Fixing `AnalyzeCommandBase` and `MultithreadedAnalyzeCommandBase` artifacts generation (#2433)

* Fixing multithreaded artifacts generation

* Fixing tests and flags

* Loosing precision.

* Applying fix for AnalyzeCommandBase

* Enabling tests

* Updating test case and release history

* Creating const to prevent magical numbers everywhere

* Rebaselining tests

* Creating Artifacts flag to keep previous behavior

* Addressing PR feedback.

* Rollback changes

* Update SARIF2012.ProvideRuleProperties_Invalid.sarif

* updating back

* Ordering deprecated names

* `SarifLogger` now emits an artifacts table entry if `artifactLocation` is not null for tool configuration and tool execution notifications. (#2437)

* Fixing artifacts generation when logging notifications

* Updating release history.

* Updating ReleaseHistory

* Fix `ArgumentException` when recurse is enabled and two file target specifiers generates the same file paths (#2438)

* Fixing ArgumentException when passing two filePaths that generates duplicated file analysis

* Fixing dotnet-format issues and updating releasehistory

* Removing comments

* Addressing PR feedback

* Addressing PR feedback

* Addressing PR review issues

Add suppression support (#2435)

* Add suppression support

* Add incompatibility check and make suppressions non-null

Co-authored-by: Eddy Nakamura <eddynaka@gmail.com>

Update releasehistory

fix couple test cases

* Fix issues in PR review

* Add xml comments

* Fix test issues

* fix dotnet format

* Addressing review feedbacks

* Fix tests

* Update extension methods names

* Change xml doc comments to normal comments

Co-authored-by: Eddy Nakamura <eddynaka@gmail.com>
Co-authored-by: Michael C. Fanning <michael.fanning@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants