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 break in DirectoryServices SearchResultCollection #113775

Merged
merged 4 commits into from
Mar 25, 2025

Conversation

IDisposable
Copy link
Contributor

@IDisposable IDisposable commented Mar 21, 2025

Added enumeration handling when the internal ArrayList has already been loaded but an enumeration follows. This fixes the issue with 9.x Linq statements against the collection caused by the access of the .Count

Fixes #113265

I am not entirely sure the best way to test this, as we never had tests for this class until now so I created a new DirectorySearcherTests.cs (conditionally run when LDAP is available) using the setup shown in the bug report.

Added enumeration handling when the internal ArrayList has already been loaded but an enumeration follows. This fixes the issue with 9.x Linq statements against the collection caused by the access of the .Count

Fixes dotnet#113265
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 21, 2025
@IDisposable
Copy link
Contributor Author

I think the code is the simplest way to fix this issue without making tons more changes. I'm not sure my test class is useful/valid because I'm not on a LDAP domain-joined machine so it simply bypasses for me in local runs.

:pointup: @BillArmstrong @stephentoub @tarekgh

@tarekgh tarekgh added this to the 10.0.0 milestone Mar 23, 2025
Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @IDisposable

@stephentoub @steveharter could you please have a quick look? Note, this is a candidate for 9.0 service porting .

@IDisposable
Copy link
Contributor Author

IDisposable commented Mar 24, 2025

Pushed PR review updates and improved the unit tests. Can anyone do the /azp magic to trigger the tests for IsLdapConfigurationExist since I can't run those local, nor does the normal build run them?

Switched enumerator to struct
Improved testing actual scenario of the bug.
@IDisposable IDisposable force-pushed the search-result-eager branch from 9b4af68 to d05ec50 Compare March 24, 2025 14:32
private const int ADS_SYSTEMFLAG_CR_NTDS_DOMAIN = 0x2;

[ConditionalFact(nameof(IsLdapConfigurationExist))]
public void DirectorySearch_IteratesCorrectly_SimpleEnumeration()
Copy link
Member

Choose a reason for hiding this comment

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

Did all of these new tests fail prior?

Copy link
Contributor Author

@IDisposable IDisposable Mar 24, 2025

Choose a reason for hiding this comment

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

There were never any tests against this class as far as I could find, so no :)

The two tests come from from @stephentoub's minimal reproduction in this comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, that might have been unclear... the first test would have always succeeded (and I put this test in to ensure I didn't break anything). The second test would have failed without the changes in this PR.

Switched the enumerator doc comments to XML docs.
Resorted the test class in the csproj
Strong type the SearchResultCollection in the test class.
_innerList.Add(enumerator.Current);
eagerList.Add(enumerator.Current);

_innerList = eagerList;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The change to build the inner list in it's entirety and then set the member is to ensure that if an exception in thrown while populating this inner list we do NOT memoize the incomplete list because then we would (if enumerated again) not throw again and would instead silently only yield the entries we previously gathered. Of course anyone attempting _re_enumeration of a list that yielded an error the first time is probably wrong, but why add to the problem?

Made the unit test helper use actual variable types (not var)
@steveharter
Copy link
Member

PTAL @BRDPM @grubioe @jay98014

@steveharter
Copy link
Member

FYI link to coding guidelines: https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/coding-style.md which do discuss the use of "var".

@IDisposable IDisposable deleted the search-result-eager branch March 26, 2025 16:47
IDisposable added a commit to IDisposable/dotnet-runtime that referenced this pull request Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.DirectoryServices community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SearchResultCollection does not work with the IEnumerable extension methods in .Net 9.
4 participants