diff --git a/infer/src/backend/procname.ml b/infer/src/backend/procname.ml index bd711bd21..64c124759 100644 --- a/infer/src/backend/procname.ml +++ b/infer/src/backend/procname.ml @@ -356,7 +356,8 @@ let java_is_vararg = function (** [is_constructor pname] returns true if [pname] is a constructor *) let is_constructor = function | JAVA js -> js.methodname = "" - | C_METHOD name -> Utils.string_is_prefix "init" name.method_name + | C_METHOD name -> + (name.method_name = "new") || Utils.string_is_prefix "init" name.method_name | _ -> false let java_is_close = function diff --git a/infer/src/clang/cFrontend_checkers.ml b/infer/src/clang/cFrontend_checkers.ml index 39b84fd91..d27e79a25 100644 --- a/infer/src/clang/cFrontend_checkers.ml +++ b/infer/src/clang/cFrontend_checkers.ml @@ -47,10 +47,12 @@ let direct_atomic_property_access context stmt_info ivar_name = | Some Sil.Tstruct (flds1, flds2, _, _, _, _, _) -> (* We give the warning when: (1) the property has the atomic attribute and - (2) the access of the ivar is not in a getter or setter method. This condition - avoids false positives *) + (2) the access of the ivar is not in a getter or setter method. + (3) the access of the ivar is not in the init method + Last two conditions avoids false positives *) let condition = (CField_decl.is_ivar_atomic ivar (flds1 @ flds2)) - && not (ObjcProperty_decl.is_getter_setter curr_class mname ivar_name) in + && not (ObjcProperty_decl.is_getter_setter curr_class mname ivar_name) + && not (Procname.is_constructor mname) in let warning_desc = { name = "DIRECT_ATOMIC_PROPERTY_ACCESS"; description = "Direct access to ivar " ^ (Ident.fieldname_to_string ivar) ^ diff --git a/infer/tests/codetoanalyze/objc/warnings/atomic_prop.m b/infer/tests/codetoanalyze/objc/warnings/atomic_prop.m index 88ac2a83f..cd44507d8 100644 --- a/infer/tests/codetoanalyze/objc/warnings/atomic_prop.m +++ b/infer/tests/codetoanalyze/objc/warnings/atomic_prop.m @@ -44,6 +44,34 @@ @synthesize b; +- (A*) init +{ + _p = 0; // Good access + _q = 0; // Good access + _f = 0; // Good access +} + +- (A*) new +{ + _p = 0; // Good access + _q = 0; // Good access + _f = 0; // Good access +} + +- (A*) initWithBla: (int) d +{ + _p = d; // Good access + _q = d; // Good access + _f = d; // Good access +} + +- (A*) initWith: (int) e +{ + _p = e; // Good access + _q = e; // Good access + _f = e; // Good access +} + - (void) writeP: (int)i { _p = i; // Good access