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 predicated analysis within points to analysis #6750

Merged
merged 1 commit into from
Jul 7, 2023

Conversation

mavasani
Copy link
Contributor

@mavasani mavasani commented Jul 7, 2023

Fixes #6695

Fix predicated value flow in points to analysis to handle different types between the targetAnalysisEntity and different analysisEntity that shares the same location but has a different type. We use the conversion info between target type and analysis entity type to ensure the value can be flowed.

This change may likely fix other CA1508 false positives which involve conversions. I am going to try to add tests for such cases and see if they are fixed, and if so, add unit tests for them in this PR. I'll instead create separate test PRs for any such fixed issues.

Fixes dotnet#6695

Fix predicated value flow in points to analysis to handle different types between the targetAnalysisEntity and different analysisEntity that shares the same location but has a different type. We use the conversion info between target type and analysis entity type to ensure the value can be flowed.
@mavasani mavasani requested a review from a team as a code owner July 7, 2023 12:38
@@ -237,38 +237,6 @@ private static void ResetAbstractValueIfTracked(AnalysisEntity analysisEntity, P
}
}

private static void SetAbstractValueFromPredicate(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method was converted to a local function as it had a single reference.

Comment on lines +514 to +536
// Ensure that the predicated 'value' can be flowed from the target type to the analysis entity.
if (!SymbolEqualityComparer.Default.Equals(targetType, analysisEntity.Type))
{
var conversion = compilation.ClassifyCommonConversion(targetType, analysisEntity.Type);
if (!conversion.Exists)
{
// No conversion exists, so we bail out from flowing the predicated value.
return;
}

if (!conversion.IsIdentity && !conversion.IsNumeric && !conversion.IsNullable)
{
// For 'Null' predicated value, there needs to be an explicit conversion
// from targetType to the analysisEntity's type to flow the predicated value.
// For 'NotNull' predicated value, there needs to be an implicit conversion
// from targetType to the analysisEntity's type to flow the predicated value.
if (nullState == NullAbstractValue.Null && conversion.IsImplicit ||
nullState == NullAbstractValue.NotNull && !conversion.IsImplicit)
{
return;
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the core bug fix. Rest of the code in this local function is same as before.

Comment on lines +538 to +541
if (!sourceAnalysisData.TryGetValue(analysisEntity, out var existingValue))
{
existingValue = defaultPointsToValueGenerator.GetOrCreateDefaultValue(analysisEntity);
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand this area well, but wanted to make sure whether we want to add the analysisEntity and existingValue pair to sourceAnalysisData or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method is only designed to modify targetAnalysisData...

@codecov
Copy link

codecov bot commented Jul 7, 2023

Codecov Report

Merging #6750 (a2a2b04) into main (581b145) will increase coverage by 0.00%.
The diff coverage is 97.50%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6750   +/-   ##
=======================================
  Coverage   96.33%   96.33%           
=======================================
  Files        1389     1389           
  Lines      325555   325649   +94     
  Branches    10731    10735    +4     
=======================================
+ Hits       313614   313711   +97     
+ Misses       9228     9226    -2     
+ Partials     2713     2712    -1     

@mavasani
Copy link
Contributor Author

mavasani commented Jul 7, 2023

Thanks @Youssef1313!

@mavasani mavasani merged commit a827d7e into dotnet:main Jul 7, 2023
14 checks passed
@mavasani mavasani deleted the CA1508_Bugs branch July 7, 2023 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CA1508 false positive with ??-pattern
2 participants