From 4490d9b033f5219975f1a24c38e993ee0b8939ec Mon Sep 17 00:00:00 2001 From: Sam Blackshear Date: Wed, 27 Jul 2016 08:34:49 -0700 Subject: [PATCH] fix recognition of inner class this guarded-by strings Reviewed By: peterogithub Differential Revision: D3620955 fbshipit-source-id: 374d078 --- infer/src/backend/rearrange.ml | 6 ++-- .../java/infer/GuardedByExample.java | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/infer/src/backend/rearrange.ml b/infer/src/backend/rearrange.ml index 7165d2a2c..beb13d615 100644 --- a/infer/src/backend/rearrange.ml +++ b/infer/src/backend/rearrange.ml @@ -634,10 +634,12 @@ let add_guarded_by_constraints prop lexp pdesc = (* programmers write @GuardedBy("MyClass.class") when the field is guarded by the class *) guarded_by_str_is_class guarded_by_str (Procname.java_get_class_name java_pname) | _ -> false in - (** return true if [guarded_by_str] is ".this" *) + (** return true if [guarded_by_str] is as suffix of ".this" *) let guarded_by_str_is_current_class_this guarded_by_str = function | Procname.Java java_pname -> - guarded_by_str = (Printf.sprintf "%s.this" (Procname.java_get_class_name java_pname)) + let fully_qualified_this = + Printf.sprintf "%s.this" (Procname.java_get_class_name java_pname) in + string_is_suffix guarded_by_str fully_qualified_this | _ -> false in let extract_guarded_by_str item_annot = let annot_extract_guarded_by_str (annot, _) = diff --git a/infer/tests/codetoanalyze/java/infer/GuardedByExample.java b/infer/tests/codetoanalyze/java/infer/GuardedByExample.java index b79bde7fb..4875814fd 100644 --- a/infer/tests/codetoanalyze/java/infer/GuardedByExample.java +++ b/infer/tests/codetoanalyze/java/infer/GuardedByExample.java @@ -308,6 +308,36 @@ public class GuardedByExample { nonExpression.toString(); } + // outer class this tests + @GuardedBy("GuardedByExample.this") + Object guardedByOuterThis; + + synchronized void okOuterAccess() { + guardedByOuterThis = null; + } + + // inner class this tests + private class Inner { + @GuardedBy("this") + Object guardedByInnerThis1; + @GuardedBy("Inner.this") + Object guardedByInnerThis2; + @GuardedBy("GuardedByExample$Inner.this") + Object guardedByInnerThis3; + + synchronized void okAccess1() { + guardedByInnerThis1 = null; + } + + synchronized void okAccess2() { + guardedByInnerThis2 = null; + } + + synchronized void okAccess3() { + guardedByInnerThis3 = null; + } + } + // TODO: report on these cases /*