Don't invoke Regex.Replace(...) incorrectly in property function#12924
Merged
YuliiaKovalova merged 1 commit intodotnet:mainfrom Dec 12, 2025
Merged
Don't invoke Regex.Replace(...) incorrectly in property function#12924YuliiaKovalova merged 1 commit intodotnet:mainfrom
YuliiaKovalova merged 1 commit intodotnet:mainfrom
Conversation
Fixes dotnet#12923 There is a small bug in WellKnownFunctions where a "receiverType == typeof(Regex)" was missed, allowing Regex.Replace(...) to be called for different receiver types in certain cases.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in the MSBuild property function evaluation system where Regex.Replace() could be incorrectly invoked on non-Regex types. The issue occurred because the code was missing a receiver type check before allowing the Regex.Replace method to be called, which could lead to unintended behavior when other types had a Replace method with similar signatures.
Key changes:
- Added proper receiver type validation for
Regex.Replace()to ensure it only executes for actual Regex types - Added regression test to verify TimeSpan.Replace (and other types) don't accidentally invoke Regex.Replace
- Minor whitespace cleanup in the surrounding code
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Build/Evaluation/Expander/WellKnownFunctions.cs | Added receiverType == typeof(Regex) check to prevent incorrect invocation of Regex.Replace on non-Regex types, making it consistent with other type-specific handlers |
| src/Build.UnitTests/Evaluation/Expander_Tests.cs | Added regression test verifying that calling Replace on TimeSpan throws an exception rather than incorrectly invoking Regex.Replace |
YuliiaKovalova
approved these changes
Dec 12, 2025
This was referenced Dec 13, 2025
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #12923
There is a small bug in
WellKnownFunctionswhere areceiverType == typeof(Regex)was missed, allowingRegex.Replace(...)to be called for different receiver types in certain cases.