Skip to content

Commit

Permalink
Ádd additional check for invalid character in property
Browse files Browse the repository at this point in the history
  • Loading branch information
mfkl committed Aug 31, 2020
1 parent 652c4ed commit 10fa40d
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 13 deletions.
19 changes: 19 additions & 0 deletions src/Build.UnitTests/Scanner_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,25 @@ public void WhitespaceProperty()
Assert.Equal("IllFormedPropertyWhitespaceInCondition", lexer.GetErrorResource());
}

/// <summary>
/// Tests for invalid element in property
/// </summary>
[Fact]
public void IllFormedPropertyInvalidElement()
{
Scanner lexer = new Scanner("$(x\\)", ParserOptions.AllowProperties);
AdvanceToScannerError(lexer);
Assert.Equal("IllFormedPropertyInvalidElementInCondition", lexer.GetErrorResource());

lexer = new Scanner("$(x{)", ParserOptions.AllowProperties);
AdvanceToScannerError(lexer);
Assert.Equal("IllFormedPropertyInvalidElementInCondition", lexer.GetErrorResource());

lexer = new Scanner("$(x;)", ParserOptions.AllowProperties);
AdvanceToScannerError(lexer);
Assert.Equal("IllFormedPropertyInvalidElementInCondition", lexer.GetErrorResource());
}

/// <summary>
/// Tests the special errors for "@(" and "@x" and similar cases.
/// </summary>
Expand Down
35 changes: 22 additions & 13 deletions src/Build/Evaluation/Conditionals/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ private string ParsePropertyOrItemMetadata()
return null;
}

var result = ScanForPropertyExpressionEnd(_expression, _parsePoint++, out int indexResult);
if (!result)
var errorMessage = ScanForPropertyExpressionEnd(_expression, _parsePoint++, out int indexResult);
if (!string.IsNullOrEmpty(errorMessage))
{
_errorState = true;
_errorPosition = indexResult;
_errorResource = "IllFormedPropertyWhitespaceInCondition";
_errorResource = errorMessage;
_unexpectedlyFound = Convert.ToString(_expression[indexResult], CultureInfo.InvariantCulture);
return null;
}
Expand Down Expand Up @@ -316,11 +316,12 @@ private string ParsePropertyOrItemMetadata()
/// <param name="index">current index to start from</param>
/// <param name="indexResult">If successful, the index corresponds to the end of the property expression.
/// In case of scan failure, it is the error position index.</param>
/// <returns>result indicating whether or not the scan was successful.</returns>
private static bool ScanForPropertyExpressionEnd(string expression, int index, out int indexResult)
/// <returns>Empty string in case of success, specific error message otherwise</returns>
private static string ScanForPropertyExpressionEnd(string expression, int index, out int indexResult)
{
int nestLevel = 0;
bool whitespaceCheck = false;
bool propertyValue = false;
string errorMessage = string.Empty;

unsafe
{
Expand All @@ -332,17 +333,25 @@ private static bool ScanForPropertyExpressionEnd(string expression, int index, o
if (character == '(')
{
nestLevel++;
whitespaceCheck = true;
propertyValue = true;
}
else if (character == ')')
{
nestLevel--;
whitespaceCheck = false;
propertyValue = false;
}
else if (whitespaceCheck && char.IsWhiteSpace(character))
else if (propertyValue)
{
indexResult = index;
return false;
if (char.IsWhiteSpace(character))
{
indexResult = index;
return "IllFormedPropertyWhitespaceInCondition";
}
else if (!XmlUtilities.IsValidSubsequentElementNameCharacter(character))
{
indexResult = index;
return "IllFormedPropertyInvalidElementInCondition";
}
}

// We have reached the end of the parenthesis nesting
Expand All @@ -351,7 +360,7 @@ private static bool ScanForPropertyExpressionEnd(string expression, int index, o
if (nestLevel == 0)
{
indexResult = index;
return true;
return errorMessage;
}
else
{
Expand All @@ -361,7 +370,7 @@ private static bool ScanForPropertyExpressionEnd(string expression, int index, o
}
}
indexResult = index;
return true;
return errorMessage;
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Build/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@
<value>MSB4151: Unexpected whitespace at position "{1}" of condition "{0}". Did you forget to remove a whitespace?</value>
<comment>{StrBegin="MSB4151: "}</comment>
</data>
<data name="IllFormedPropertyInvalidElementInCondition" xml:space="preserve">
<value>MSB4152: Unexpected invalid element at position "{1}" of condition "{0}".</value>
<comment>{StrBegin="MSB4152: "}</comment>
</data>
<data name="IllFormedQuotedStringInCondition" xml:space="preserve">
<value>MSB4101: Expected a closing quote after position {1} in condition "{0}".</value>
<comment>{StrBegin="MSB4101: "}</comment>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.en.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 10fa40d

Please sign in to comment.