Skip to content

Fix NullReferenceException when expanding property functions that return null#6414

Merged
ladipro merged 1 commit intodotnet:vs16.10from
ladipro:6413-fix-null-evaluation-fail
May 6, 2021
Merged

Fix NullReferenceException when expanding property functions that return null#6414
ladipro merged 1 commit intodotnet:vs16.10from
ladipro:6413-fix-null-evaluation-fail

Conversation

@ladipro
Copy link
Copy Markdown
Member

@ladipro ladipro commented May 5, 2021

Fixes #6413

Context

This is a regression introduced in #6128. MSBuild crashes when evaluating a project where a property function returns null and its result is concatenated with another non-empty value.

Changes Made

Add a null check.

Testing

Fixed and extended the relevant test case.

Notes

Enable "Hide whitespace changes" when reviewing this change.

Copy link
Copy Markdown
Member

@rainersigwald rainersigwald left a comment

Choose a reason for hiding this comment

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

LGTM and I agree that we should take this in 16.10. @marcpopMSFT, any objection? The regression was reported by 1ES.


Expander<ProjectPropertyInstance, ProjectItemInstance> expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);

string result = expander.ExpandIntoStringLeaveEscaped("$([System.Convert]::ChangeType(,$(SomeStuff.GetType())))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Was this just a fancy way to get null before, and you're using the easier approach now?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It was not evaluating to null before so not hitting the intended code path. Presumably because the (, argument omission makes us pass an empty string.

Copy link
Copy Markdown
Member

@benvillalobos benvillalobos left a comment

Choose a reason for hiding this comment

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

Took me a sec to realize the "meat" of the change is just making sure propertyValue doesn't get added if null. LGTM

string result = expander.ExpandIntoStringLeaveEscaped("$([System.Convert]::ChangeType(,$(SomeStuff.GetType())))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance);

// The null-returning function is the only thing in the expression.
string result = expander.ExpandIntoStringLeaveEscaped("$([System.Environment]::GetEnvironmentVariable(`_NonExistentVar`))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm now reminded that if an environment var doesn't exist, GetEnvironmentVariable returns null.

@ladipro ladipro merged commit c040391 into dotnet:vs16.10 May 6, 2021
@ladipro ladipro deleted the 6413-fix-null-evaluation-fail branch May 6, 2021 07:49
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.

4 participants