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 2 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 @@ -184,6 +184,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