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 bug in 1012. #1969

Merged
2 commits merged into from
Jul 3, 2020
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
4 changes: 2 additions & 2 deletions docs/Producing effective SARIF.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,13 @@ The SARIF standard was developed over several years, and many intermediate versi

The properties of a result's 'message' property must be consistent with the properties of the rule that the result refers to.

When a result's 'message' object uses the 'id' and 'arguments' properties (which, by the way, is recommended: see SARIF2002.ProvideMessageArguments), it must ensure that the rule actually defines a message string with that id, and that 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{3}', then the 'arguments' array must contain at least 4 elements.
When a result's 'message' object uses the 'id' and 'arguments' properties (which, by the way, is recommended: see SARIF2002.ProvideMessageArguments), it must ensure that the rule actually defines a message string with that id, and that 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{{3}}', then the 'arguments' array must contain at least 4 elements.

#### Messages

##### `SupplyEnoughMessageArguments`: error

{0}: The message with id '{1}' in rule '{2}' requires {3} arguments, but the 'arguments' array in this message object has only {4} elements. When a tool creates a result message that use the 'id' and 'arguments' properties, it must ensure that the 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{{3}}', then the 'arguments' array must contain 4 elements.
{0}: The message with id '{1}' in rule '{2}' requires '{3}' arguments, but the 'arguments' array in this message object has only '{4}' element(s). When a tool creates a result message that use the 'id' and 'arguments' properties, it must ensure that the 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{{3}}', then the 'arguments' array must contain 4 elements.

##### `MessageIdMustExist`: error

Expand Down
2 changes: 1 addition & 1 deletion src/Sarif.Multitool/Rules/RuleResources.Designer.cs

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

4 changes: 2 additions & 2 deletions src/Sarif.Multitool/Rules/RuleResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ Many tools follow a conventional format for the 'reportingDescriptor.id' propert
<value>{0}: This message object refers to the message with id '{1}' in rule '{2}', but that rule does not define a message with that id. When a tool creates a result message that uses the 'id' property, it must ensure that the specified rule actually has a message with that id.</value>
</data>
<data name="SARIF1012_MessageArgumentsMustBeConsistentWithRule_Error_SupplyEnoughMessageArguments_Text" xml:space="preserve">
<value>{0}: The message with id '{1}' in rule '{2}' requires {3} arguments, but the 'arguments' array in this message object has only {4} elements. When a tool creates a result message that use the 'id' and 'arguments' properties, it must ensure that the 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{{3}}', then the 'arguments' array must contain 4 elements.</value>
<value>{0}: The message with id '{1}' in rule '{2}' requires '{3}' arguments, but the 'arguments' array in this message object has only '{4}' element(s). When a tool creates a result message that use the 'id' and 'arguments' properties, it must ensure that the 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{{3}}', then the 'arguments' array must contain 4 elements.</value>
</data>
<data name="SARIF1012_MessageArgumentsMustBeConsistentWithRule_FullDescription_Text" xml:space="preserve">
<value>The properties of a result's 'message' property must be consistent with the properties of the rule that the result refers to.

When a result's 'message' object uses the 'id' and 'arguments' properties (which, by the way, is recommended: see SARIF2002.ProvideMessageArguments), it must ensure that the rule actually defines a message string with that id, and that 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{3}', then the 'arguments' array must contain at least 4 elements.</value>
When a result's 'message' object uses the 'id' and 'arguments' properties (which, by the way, is recommended: see SARIF2002.ProvideMessageArguments), it must ensure that the rule actually defines a message string with that id, and that 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{{3}}', then the 'arguments' array must contain at least 4 elements.</value>
</data>
<data name="SARIF2013_ProvideEmbeddedFileContent_Note_Default_Text" xml:space="preserve">
<value>Placeholder_SARIF2013_ProvideEmbeddedFileContent_Note_Default_Text</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MessageArgumentsMustBeConsistentWithRule : SarifValidationSkimmerBa

public override FailureLevel DefaultLevel => FailureLevel.Error;

private static readonly Regex s_replacementSequenceRegex = new Regex(@"\{(<index>\d+)\}", RegexOptions.Compiled | RegexOptions.CultureInvariant);
private static readonly Regex s_replacementSequenceRegex = new Regex(@"\{(?<index>\d+)\}", RegexOptions.Compiled | RegexOptions.CultureInvariant);
private IList<ReportingDescriptor> currentRules;
private Run run;

Expand Down Expand Up @@ -69,11 +69,12 @@ protected override void Analyze(Result result, string resultPointer)

// A message with the specified key is present in the rule. Check if the result supplied enough arguments.
string messageText = rule.MessageStrings[result.Message.Id].Text;
int placeholderMaxPosition = PlaceholderMaxPosition(messageText);
if (placeholderMaxPosition > (result.Message.Arguments?.Count ?? 0))
int numArgsRequired = GetNumArgsRequired(messageText);
int numArgsPresent = result.Message.Arguments?.Count ?? 0;
if (numArgsRequired > numArgsPresent)
{
// {0}: The message with id '{1}' in rule '{2}' requires {3} arguments, but the
// 'arguments' array in this message object has only {4} elements. When a tool
// {0}: The message with id '{1}' in rule '{2}' requires '{3}' arguments, but the
// 'arguments' array in this message object has only '{4}' element(s). When a tool
// creates a result message that use the 'id' and 'arguments' properties, it must
// ensure that the 'arguments' array has enough elements to provide values for every
// replacement sequence in the message specified by 'id'. For example, if the highest
Expand All @@ -82,16 +83,15 @@ protected override void Analyze(Result result, string resultPointer)
LogResult(
resultPointer,
nameof(RuleResources.SARIF1012_MessageArgumentsMustBeConsistentWithRule_Error_SupplyEnoughMessageArguments_Text),
result.Message.Arguments.Count.ToString(),
result.Message.Id,
result.ResolvedRuleId(run) ?? "null",
placeholderMaxPosition.ToString(),
messageText);
numArgsRequired.ToString(),
result.Message.Arguments.Count.ToString());
}
}
}

private int PlaceholderMaxPosition(string text)
private int GetNumArgsRequired(string text)
{
int max = -1;
foreach (Match match in s_replacementSequenceRegex.Matches(text))
Expand All @@ -100,7 +100,7 @@ private int PlaceholderMaxPosition(string text)
max = Math.Max(max, index);
}

return max++;
return max + 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
"text": "The properties of a result's 'message' property must be consistent with the properties of the rule that the result refers to."
},
"fullDescription": {
"text": "The properties of a result's 'message' property must be consistent with the properties of the rule that the result refers to.\r\n\r\nWhen a result's 'message' object uses the 'id' and 'arguments' properties (which, by the way, is recommended: see SARIF2002.ProvideMessageArguments), it must ensure that the rule actually defines a message string with that id, and that 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{3}', then the 'arguments' array must contain at least 4 elements."
"text": "The properties of a result's 'message' property must be consistent with the properties of the rule that the result refers to.\r\n\r\nWhen a result's 'message' object uses the 'id' and 'arguments' properties (which, by the way, is recommended: see SARIF2002.ProvideMessageArguments), it must ensure that the rule actually defines a message string with that id, and that 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{{3}}', then the 'arguments' array must contain at least 4 elements."
},
"messageStrings": {
"Error_MessageIdMustExist": {
"text": "{0}: This message object refers to the message with id '{1}' in rule '{2}', but that rule does not define a message with that id. When a tool creates a result message that uses the 'id' property, it must ensure that the specified rule actually has a message with that id."
},
"Error_SupplyEnoughMessageArguments": {
"text": "{0}: The message with id '{1}' in rule '{2}' requires {3} arguments, but the 'arguments' array in this message object has only {4} elements. When a tool creates a result message that use the 'id' and 'arguments' properties, it must ensure that the 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{{3}}', then the 'arguments' array must contain 4 elements."
"text": "{0}: The message with id '{1}' in rule '{2}' requires '{3}' arguments, but the 'arguments' array in this message object has only '{4}' element(s). When a tool creates a result message that use the 'id' and 'arguments' properties, it must ensure that the 'arguments' array has enough elements to provide values for every replacement sequence in the message specified by 'id'. For example, if the highest numbered replacement sequence in the specified message string is '{{3}}', then the 'arguments' array must contain 4 elements."
}
},
"helpUri": "http://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html"
Expand Down Expand Up @@ -69,6 +69,34 @@
}
],
"results": [
{
"ruleId": "SARIF1012",
"ruleIndex": 0,
"level": "error",
"message": {
"id": "Error_SupplyEnoughMessageArguments",
"arguments": [
"runs[0].results[0]",
"DoesExist",
"TEST1001",
"2",
"1"
]
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"index": 0
},
"region": {
"startLine": 26,
"startColumn": 9
}
}
}
]
},
{
"ruleId": "SARIF1012",
"ruleIndex": 0,
Expand Down