Skip to content

Don't invoke Regex.Replace(...) incorrectly in property function#12924

Merged
YuliiaKovalova merged 1 commit intodotnet:mainfrom
DustinCampbell:fix-replace-issue
Dec 12, 2025
Merged

Don't invoke Regex.Replace(...) incorrectly in property function#12924
YuliiaKovalova merged 1 commit intodotnet:mainfrom
DustinCampbell:fix-replace-issue

Conversation

@DustinCampbell
Copy link
Member

Fixes #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.

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.
Copilot AI review requested due to automatic review settings December 11, 2025 18:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 YuliiaKovalova merged commit 7df7cfd into dotnet:main Dec 12, 2025
16 checks passed
@DustinCampbell DustinCampbell deleted the fix-replace-issue branch December 12, 2025 17:41
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.

A "Replace" property function can call "Regex.Replace(...)" even if the receiver type is not "Regex"

3 participants