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 safety evaluation requiring lhs assignment to be safe unnecessarily #2242

Merged
merged 3 commits into from
May 2, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,20 @@ public static Safety getSafety(Tree tree, VisitorState state) {
// Check the symbol itself:
Symbol treeSymbol = ASTHelpers.getSymbol(tree);
Safety symbolSafety = getSafety(treeSymbol, state);
Type type = tree instanceof ExpressionTree
Type treeType = tree instanceof ExpressionTree
? ASTHelpers.getResultType((ExpressionTree) tree)
: ASTHelpers.getType(tree);

if (type == null) {
return symbolSafety;
} else {
return Safety.mergeAssumingUnknownIsSame(symbolSafety, getSafety(type, state), getSafety(type.tsym, state));
}
Safety treeTypeSafety = treeType == null
? Safety.UNKNOWN
: Safety.mergeAssumingUnknownIsSame(getSafety(treeType, state), getSafety(treeType.tsym, state));
Type symbolType = treeSymbol == null ? null : treeSymbol.type;
// If the type extracted from the symbol matches the type extracted from the tree, avoid duplicate work.
// However, in some cases type parameter information is excluded from one, but not the other.
Safety symbolTypeSafety = symbolType == null
|| (treeType != null && state.getTypes().isSameType(treeType, symbolType))
? Safety.UNKNOWN
: Safety.mergeAssumingUnknownIsSame(getSafety(symbolType, state), getSafety(symbolType.tsym, state));
return Safety.mergeAssumingUnknownIsSame(symbolSafety, treeTypeSafety, symbolTypeSafety);
}

public static Safety getSafety(@Nullable Symbol symbol, VisitorState state) {
Expand Down Expand Up @@ -184,6 +189,10 @@ Safety getSafety(Type type, VisitorState state, @Nullable Set<String> dejaVu) {
}
Safety safety = Safety.SAFE;
List<Type> typeArguments = asSubtype.getTypeArguments();
if (typeArguments.isEmpty()) {
// Type information is not available, not enough data to make a decision
return null;
}
for (Type typeArgument : typeArguments) {
Safety safetyBasedOnType = SafetyAnnotations.getSafetyInternal(typeArgument, state, deJaVuToPass);
Safety safetyBasedOnSymbol = SafetyAnnotations.getSafety(typeArgument.tsym, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,26 @@ public void testFieldAnnotationAssignment() {
.doTest();
}

@Test
public void testThrowableFieldAssignment() {
helper().addSourceLines(
"Test.java",
"import com.palantir.logsafe.*;",
"import java.util.*;",
"class Test<OutputT> {",
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 test exposed the issue, but only when the (unused) type parameter is present. JCTree.type of the field when <OutputT> is present is java.util.Set and without it, JCTree.type is java.util.Set<java.lang.Throwable>!

I've updated the check to both:

  1. avoid returning type-info based safety in the case that there's no type info
  2. check both the symbol type and tree type for safety info

" private volatile Set<Throwable> fieldValue = null;",
" static final class Other {",
" void f(Test obj, Set<Throwable> newValue) {",
" synchronized (obj) {",
" if (obj.fieldValue != newValue)",
" obj.fieldValue = newValue;",
" }",
" }",
" }",
"}")
.doTest();
}

@Test
public void disagreeingSafetyAnnotations() {
helper().addSourceLines(
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2242.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Fix safety evaluation requiring lhs of an assignment to be safe unnecessarily
links:
- https://github.com/palantir/gradle-baseline/pull/2242