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

Reauthor runtime errors. #2660

Merged
merged 3 commits into from
May 3, 2023
Merged

Reauthor runtime errors. #2660

merged 3 commits into from
May 3, 2023

Conversation

michaelcfanning
Copy link
Member

@michaelcfanning michaelcfanning commented Apr 30, 2023

  • BRK: RuntimeConditions now of type long to permit more flag values. Many literal values have changed for individual members. #2660
  • BRK: RuntimeConditions.OneOrMoreFilesSkippedDueToSize renamed to OneOrMoreFilesSkippedDueToExceedingSizeLimits. #2660
  • BUG: Properly report skipping empty files (rather than reporting file was skipped due to exceeding size limits). #2660
  • NEW: Provide convenience enumerator at the SarifLog level that iterates over all results in all runs in the log. #2660
  • NEW: Provide Notes.LogEmptyFileSkipped helper for reporting zero-byte files skipped at scan time. #2660


//TBD resolve type mismatch
_ignoredFilesCount += (uint)context.TargetsProvider.Skipped.Count;
Notes.LogEmptyFileSkipped(context, artifact.Uri.GetFilePath());
Copy link
Member Author

Choose a reason for hiding this comment

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

LogEmptyFileSkipped

Note that we don't track/report out on skipping 0-byte files, though we do set a non-fatal runtime condition. How interesting is it that we don't scan a 0-byte file?

I suppose we still have a problem, though, there's at least a theoretical case that someone's analysis might be intended to flag all zero-byte files. :) We could allow this lower-bound threshold to be configurable and the core engine code would probably need to be updated.

@@ -302,62 +302,69 @@ private static SarifLog GetSarifLogWithMinimalUniqueData()
}

[Fact]
public async Task SarifLog_Post_WithValidParameters_ShouldNotThrownAnExceptionWhenRequestIsValid()
Copy link
Member Author

Choose a reason for hiding this comment

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

SarifLog_Post_WithValidParameters_ShouldNotThrownAnExceptionWhenRequestIsValid

These tests were poorly factored and as a result made it difficult to locate an underlying test helper bug. It's a good object lesson for why we factor tests the way that we do, it's to lower costs in resolving regressions (in this case introduced by an update to net6.0 from netcoreapp3.1).

@@ -38,7 +38,7 @@ public void Stack_CreateFromException()
{
File.Create(Path.GetInvalidFileNameChars()[0].ToString(), 0);
}
catch (ArgumentException exception)
catch (IOException exception)
Copy link
Member Author

Choose a reason for hiding this comment

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

IOException

Change in behavior from netcoreapp3.1, File.Create now raises an IOException for illegaly chars (which makes sense).

@@ -845,13 +845,13 @@ public void SarifLogger_EnhancesRunWithAdditionalAnalysisTargets()
[Fact]
public void SarifLogger_AcceptsOverrideOfDefaultEncoding()
{
const string Utf8 = "UTF-8";
const string Utf7 = "UTF-7";
Copy link
Member Author

Choose a reason for hiding this comment

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

UTF

UTF7 is entirely deprecated in net6.0 as an insecure format to support.

@@ -20,6 +20,11 @@ public static StringContent AnyContent()
return new StringContent(AnyContentText);
}

public static HttpResponseMessage CreateOKResponse()
Copy link
Member Author

Choose a reason for hiding this comment

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

CreateOKResponse

So, this code illustrates some of the dangers of statics. There is no such thing as a static response, once you read the stream from it, that stream is disposed.

I'm surprised we haven't gotten burned by this before. We need to revisit this test class. I took a small change along the lines of what I think we'll need, helpers that dynamically create new instances of the default data.

Though honestly, a helper that wraps a single line of code may not be worth implementing at all. :)

context.RuntimeExceptions?[0].InnerException.Should().BeNull();
context.RuntimeExceptions?[0].Should().BeNull();

context.RuntimeExceptions?[0].InnerException?.ToString().Should().BeNull();
Copy link
Member Author

Choose a reason for hiding this comment

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

ToString

These are subtle changes that publish more actionable data to the test logs when failures occur. Now we'll get all of ex.ToString() (which is very rich and useful) rather than a limited report that's constrained to the exception type.

Comment on lines +364 to +367
new HttpRequestMessage(HttpMethod.Post, postUri)
{
Content = new StreamContent(CreateSarifLogStream())
},

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning

Disposable 'HttpRequestMessage' is created but not disposed.
/// <returns>A Result enumerator for the SARIF log.</returns>
public IEnumerable<Result> Results()
{
if (this.Runs?.Count > 0 == false) { yield break; }

Check notice

Code scanning / CodeQL

Unnecessarily complex Boolean expression Note

The expression 'A == false' can be simplified to '!A'.

foreach (Run run in this.Runs)
{
if (run.Results?.Count > 0 == false) { continue; }

Check notice

Code scanning / CodeQL

Unnecessarily complex Boolean expression Note

The expression 'A == false' can be simplified to '!A'.

Choose a reason for hiding this comment

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

The > operator indeed returns bool rather than bool?. But if you used the ! operator, then you'd have to add parentheses if (!(run.Results?.Count > 0)), which would look more complex.

I think if (run.Results?.Count ?? 0 == 0) would be quite readable here.


public override string ToString()
{
return $"{ExitReason}: {base.ToString()}";

Check notice

Code scanning / CodeQL

Redundant ToString() call Note

Redundant call to 'ToString' on a String object.

Choose a reason for hiding this comment

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

Does CodeQL expect $"{ExitReason}: {base}"? I don't think C# allows that.

httpMock.Mock(
new HttpRequestMessage(HttpMethod.Post, postUri) { Content = new StreamContent(memoryStream) },
new HttpRequestMessage(HttpMethod.Post, postUri) { Content = new StreamContent(CreateSarifLogStream()) },

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning

Disposable 'HttpRequestMessage' is created but not disposed.
Comment on lines +329 to +332
new HttpRequestMessage(HttpMethod.Post, postUri)
{
Content = new StreamContent(CreateSarifLogStream())
},

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning

Disposable 'HttpRequestMessage' is created but not disposed.
@michaelcfanning michaelcfanning marked this pull request as ready for review May 1, 2023 15:03
Copy link
Contributor

@HulonJenkins HulonJenkins left a comment

Choose a reason for hiding this comment

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

:shipit:

context.MaxFileSizeInKilobytes.ToString()));

context.RuntimeErrors |= RuntimeConditions.OneOrMoreEmptyFilesSkipped;
}
Copy link
Collaborator

@yongyan-gh yongyan-gh May 1, 2023

Choose a reason for hiding this comment

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

nit: missing a empty line after this function

{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

// {0} file(s)s were skipped for analysis due to exceeding size limits
// {0} file(s)s were skipped for analysis due to exceeding size limit
// (currently configured as {1} kilobytes). The 'max-file-size-in-kb'
// command-line argument can be used to increase this threshold.
context.Logger.LogConfigurationNotification(
Copy link
Collaborator

Choose a reason for hiding this comment

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

LogConfigurationNotification

may move the common code into a function and resuse it

Copy link

@schlaman-ms schlaman-ms left a comment

Choose a reason for hiding this comment

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

:shipit:

@@ -197,7 +197,7 @@ public void ValidatesAllTestFiles()
Path.Combine("v2", "ConverterTestData"),
Path.Combine("v2", "SpecExamples"),
Path.Combine("v2", "ObsoleteFormats"),
Path.Combine("..", "..", "Test.UnitTests.Sarif", "netcoreapp3.1", "TestData")
Path.Combine("..", "..", "Test.UnitTests.Sarif", "net6.0", "TestData")
Copy link
Collaborator

Choose a reason for hiding this comment

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

net6.0

fyi Suvam mentioned to me coyote testing does not support 6.0 yet, see if we want to keep in 3.1

Copy link
Collaborator

@shaopeng-gh shaopeng-gh left a comment

Choose a reason for hiding this comment

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

:shipit:

@michaelcfanning michaelcfanning merged commit dc372ae into main May 3, 2023
8 of 9 checks passed
@michaelcfanning michaelcfanning deleted the runtime-errors branch May 3, 2023 15:10
@@ -11,51 +11,98 @@ namespace Microsoft.CodeAnalysis.Sarif
// code paths. These conditions are a combination of fatal
// and non-fatal circumstances
[Flags]
public enum RuntimeConditions : uint
public enum RuntimeConditions : long
Copy link
Collaborator

@shaopeng-gh shaopeng-gh May 4, 2023

Choose a reason for hiding this comment

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

long

@michaelcfanning we changed this to long, should we change the usage of it to use long as well,
e.g. I see the run result was int, should we also change to long?

abstract public int Run(T options);

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.

None yet

6 participants