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 NullReferenceException when expanding property functions that return null #6414

Merged
merged 1 commit into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Build.UnitTests/Evaluation/Expander_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2010,13 +2010,16 @@ public void PropertyFunctionNullArgument()
public void PropertyFunctionNullReturn()
{
PropertyDictionary<ProjectPropertyInstance> pg = new PropertyDictionary<ProjectPropertyInstance>();
pg.Set(ProjectPropertyInstance.Create("SomeStuff", "This IS SOME STUff"));

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


// 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
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.

Assert.Equal("", result);

// The result of the null-returning function is concatenated with a non-empty string.
result = expander.ExpandIntoStringLeaveEscaped("prefix_$([System.Environment]::GetEnvironmentVariable(`_NonExistentVar`))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance);
Assert.Equal("prefix_", result);
}

/// <summary>
Expand Down
21 changes: 12 additions & 9 deletions src/Build/Evaluation/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,19 +1200,22 @@ private static class PropertyExpander<T>
propertyValue = LookupProperty(properties, expression, propertyStartIndex + 2, propertyEndIndex - 1, elementLocation, usedUninitializedProperties);
}

if (IsTruncationEnabled(options) && propertyValue != null)
if (propertyValue != null)
{
var value = propertyValue.ToString();
if (value.Length > CharacterLimitPerExpansion)
if (IsTruncationEnabled(options))
{
propertyValue = value.Substring(0, CharacterLimitPerExpansion - 3) + "...";
var value = propertyValue.ToString();
if (value.Length > CharacterLimitPerExpansion)
{
propertyValue = value.Substring(0, CharacterLimitPerExpansion - 3) + "...";
}
}
}

// Record our result, and advance
// our sourceIndex pointer to the character just after the closing
// parenthesis.
results.Add(propertyValue);
// Record our result, and advance
// our sourceIndex pointer to the character just after the closing
// parenthesis.
results.Add(propertyValue);
}
sourceIndex = propertyEndIndex + 1;
}

Expand Down