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 incorrect use of GetOrAdd #10152

Merged
merged 1 commit into from
May 17, 2024
Merged

Fix incorrect use of GetOrAdd #10152

merged 1 commit into from
May 17, 2024

Conversation

jaredpar
Copy link
Member

The method GetOrAdd has two overloads with two arguments:

GetOrAdd(string key, object value);
GetOrAdd(string key, Func<string, object> valueFactory);

The lambda here does not take an argument hence it's not compatible with Func<string, object>. It is compatible with object though because a delegate can convert to object.

This is problematic because there is no guarantee the delegate here is unqiue. The compiler is free to cache instantiations of delegates, particularly ones like this which capture no state. That means different keys could very well be sharing the same value.

Fixes #

Context

Changes Made

Testing

Notes

The method `GetOrAdd` has two overloads with two arguments:

```csharp
GetOrAdd(string key, object value);
GetOrAdd(string key, Func<string, object> valueFactory);
```

The lambda here does not take an argument hence it's not compatible with
`Func<string, object>`. It is compatible with `object` though because a
delegate can convert to `object`.

This is problematic because there is no guarantee the delegate here is
unqiue. The compiler is free to cache instantiations of delegates,
particularly ones like this which capture no state. That means different
keys could very well be sharing the same value.
Copy link
Contributor

@jeffkl jeffkl left a comment

Choose a reason for hiding this comment

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

Is this a coincidence?

NuGet/NuGet.Client#5808

@stephentoub
Copy link
Member

Is this a coincidence?

NuGet/NuGet.Client#5808

No 😉

@jaredpar
Copy link
Member Author

@rainersigwald, @baronfel i'm not authorized to merge this myself

@baronfel baronfel merged commit ffe4ef2 into main May 17, 2024
10 checks passed
@baronfel baronfel deleted the dev/jaredpar/race branch May 17, 2024 21:50
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.

5 participants