-
Notifications
You must be signed in to change notification settings - Fork 87
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
sarif1008: ContextRegionMustBeProperSupersetOfRegion check #1925
Merged
12 commits merged into
features/sarif-validation-rules
from
users/hakohli/1008-region-superset
Jun 24, 2020
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8b11976
Add ContextRegionMustBeProperSupersetOfRegion check
8538964
rc++
de68307
regen test output
c58f30e
updated with defaults computations and related tests
4e7aee3
pushing changes
2d2c9a9
changing names for testcases
80b4f18
following convention in the validation rules
186aa05
fixing test case output
d359e99
final fixes (hopefully) :/
5d1284f
:(
800861c
Update user-facing strings.
af20b43
Merge branch 'features/sarif-validation-rules' into users/hakohli/100…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,184 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif | ||
{ | ||
public partial class Region | ||
{ | ||
public bool IsBinaryRegion | ||
public bool IsBinaryRegion => this.ByteOffset >= 0; | ||
|
||
public bool IsLineColumnBasedTextRegion => this.StartLine >= 1; | ||
|
||
public bool IsOffsetBasedTextRegion => this.CharOffset >= 0; | ||
|
||
public override string ToString() | ||
{ | ||
return this.FormatForVisualStudio(); | ||
} | ||
|
||
public void PopulateDefaults() | ||
{ | ||
get | ||
if (this.IsLineColumnBasedTextRegion) | ||
{ | ||
this.PopulateLineColumnBasedTextDefaults(); | ||
} | ||
|
||
if (this.IsOffsetBasedTextRegion) | ||
{ | ||
// Is this right? What about an insertion point right after a BOM in a text file?? | ||
// Do we need to just bite the bullet and make these Nullable type so that we have a | ||
// clear indicator of whether the region is binary vs. textual? I tend to think so. | ||
return | ||
this.StartLine == 0 && | ||
this.CharLength == 0 && | ||
this.CharOffset == 0; | ||
this.PopulateOffsetBasedTextDefaults(); | ||
} | ||
|
||
if (this.IsBinaryRegion) | ||
{ | ||
this.PopulateBinaryDefaults(); | ||
} | ||
} | ||
|
||
public override string ToString() | ||
private void PopulateLineColumnBasedTextDefaults() | ||
{ | ||
return this.FormatForVisualStudio(); | ||
if (this.EndLine == 0) | ||
{ | ||
this.EndLine = this.StartLine; | ||
} | ||
|
||
if (this.StartColumn == 0) | ||
{ | ||
this.StartColumn = 1; | ||
} | ||
|
||
if (this.EndColumn == 0) | ||
{ | ||
this.EndColumn = int.MaxValue; | ||
} | ||
} | ||
|
||
private void PopulateOffsetBasedTextDefaults() | ||
{ | ||
if (this.CharLength == -1) | ||
{ | ||
this.CharLength = 0; | ||
} | ||
} | ||
|
||
private void PopulateBinaryDefaults() | ||
{ | ||
if (this.ByteLength == -1) | ||
{ | ||
this.ByteLength = 0; | ||
} | ||
} | ||
|
||
public bool IsProperSupersetOf(Region subRegion) | ||
{ | ||
this.PopulateDefaults(); | ||
subRegion.PopulateDefaults(); | ||
|
||
if (this.IsLineColumnBasedTextRegion && | ||
subRegion.IsLineColumnBasedTextRegion && | ||
!IsLineColumnBasedTextRegionProperSupersetOf(subRegion)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (this.IsOffsetBasedTextRegion && | ||
subRegion.IsOffsetBasedTextRegion && | ||
!IsOffsetBasedTextRegionProperSupetSetOf(subRegion)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (this.IsBinaryRegion && | ||
subRegion.IsBinaryRegion && | ||
!IsBinaryRegionProperSupersetOf(subRegion)) | ||
{ | ||
return false; | ||
} | ||
|
||
// if we reach here, the region and context region have been expressed as different property sets, | ||
// and it is not possible to judge validity without looking at the actual content. | ||
// It is a potential false negative. | ||
return true; | ||
} | ||
|
||
private bool IsLineColumnBasedTextRegionProperSupersetOf(Region subRegion) | ||
{ | ||
if (this.StartLine > subRegion.StartLine || this.EndLine < subRegion.EndLine) | ||
{ | ||
return false; | ||
} | ||
|
||
if (this.StartLine == subRegion.StartLine && this.StartColumn > subRegion.StartColumn) | ||
{ | ||
return false; | ||
} | ||
|
||
if (this.EndLine == subRegion.EndLine && this.EndColumn < subRegion.EndColumn) | ||
{ | ||
return false; | ||
} | ||
|
||
if (this.StartLine == subRegion.StartLine && | ||
this.EndLine == subRegion.EndLine && | ||
this.StartColumn == subRegion.StartColumn && | ||
this.EndColumn == subRegion.EndColumn) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private bool IsBinaryRegionProperSupersetOf(Region subRegion) | ||
{ | ||
if (this.ByteOffset > subRegion.ByteOffset) | ||
{ | ||
return false; | ||
} | ||
|
||
if (GetByteEndOffset(this) < GetByteEndOffset(subRegion)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (this.ByteOffset == subRegion.ByteOffset && this.ByteLength <= subRegion.ByteLength) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private bool IsOffsetBasedTextRegionProperSupetSetOf(Region subRegion) | ||
{ | ||
if (this.CharOffset > subRegion.CharOffset) | ||
{ | ||
return false; | ||
} | ||
|
||
if (GetCharEndOffset(this) < GetCharEndOffset(subRegion)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (this.CharOffset == subRegion.CharOffset && this.CharLength <= subRegion.CharLength) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static int GetCharEndOffset(Region region) | ||
{ | ||
return region.CharOffset + region.CharLength; | ||
} | ||
|
||
private static int GetByteEndOffset(Region region) | ||
{ | ||
return region.ByteOffset + region.ByteLength; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the source of this text? i guessed from Eddy's PR that it was written by himself (not picked from message strings) #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anything you want, and what you wrote is good.
In reply to: 445040282 [](ancestors = 445040282)