From 29aea2d61d9eb5bae31026ab14fc921f83d6dba0 Mon Sep 17 00:00:00 2001 From: Daiva Naudziuniene Date: Mon, 21 Aug 2017 08:16:01 -0700 Subject: [PATCH] Do not show internal SIL variable name in the error message of temporary address escape Summary: Title Reviewed By: jvillard Differential Revision: D5669502 fbshipit-source-id: 295d485 --- infer/src/IR/Localise.ml | 10 +++++++--- infer/src/IR/Localise.mli | 2 +- infer/src/backend/errdesc.ml | 2 +- infer/tests/codetoanalyze/cpp/errors/issues.exp | 1 + .../codetoanalyze/cpp/errors/stack_escape/basic.cpp | 10 ++++++++++ 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/infer/src/IR/Localise.ml b/infer/src/IR/Localise.ml index 77a4ebe55..2a2afbea1 100644 --- a/infer/src/IR/Localise.ml +++ b/infer/src/IR/Localise.ml @@ -913,7 +913,8 @@ let desc_inherently_dangerous_function proc_name = Tags.update tags Tags.value proc_name_str ; {no_desc with descriptions= [MF.monospaced_to_string proc_name_str]; tags= !tags} -let desc_stack_variable_address_escape expr_str addr_dexp_str loc = +let desc_stack_variable_address_escape pvar addr_dexp_str loc = + let expr_str = Pvar.to_string pvar in let tags = Tags.create () in Tags.update tags Tags.value expr_str ; let escape_to_str = @@ -924,9 +925,12 @@ let desc_stack_variable_address_escape expr_str addr_dexp_str loc = | None -> "" in + let variable_str = + if Pvar.is_frontend_tmp pvar then "temporary" + else Format.asprintf "stack variable %a" MF.pp_monospaced expr_str + in let description = - Format.asprintf "Address of stack variable %a escapes %s%s" MF.pp_monospaced expr_str - escape_to_str (at_line tags loc) + Format.asprintf "Address of %s escapes %s%s" variable_str escape_to_str (at_line tags loc) in {no_desc with descriptions= [description]; tags= !tags} diff --git a/infer/src/IR/Localise.mli b/infer/src/IR/Localise.mli index cb791e86e..6e8c4efb3 100644 --- a/infer/src/IR/Localise.mli +++ b/infer/src/IR/Localise.mli @@ -210,7 +210,7 @@ val desc_return_statement_missing : Location.t -> error_desc val desc_return_value_ignored : Typ.Procname.t -> Location.t -> error_desc -val desc_stack_variable_address_escape : string -> string option -> Location.t -> error_desc +val desc_stack_variable_address_escape : Pvar.t -> string option -> Location.t -> error_desc val desc_skip_function : Typ.Procname.t -> error_desc diff --git a/infer/src/backend/errdesc.ml b/infer/src/backend/errdesc.ml index b9f333ff0..4366fcdfc 100644 --- a/infer/src/backend/errdesc.ml +++ b/infer/src/backend/errdesc.ml @@ -1167,7 +1167,7 @@ let explain_stack_variable_address_escape loc pvar addr_dexp_opt = | None -> None in - Localise.desc_stack_variable_address_escape (Pvar.to_string pvar) addr_dexp_str loc + Localise.desc_stack_variable_address_escape pvar addr_dexp_str loc (** explain unary minus applied to unsigned expression *) let explain_unary_minus_applied_to_unsigned_expression tenv exp typ node loc = diff --git a/infer/tests/codetoanalyze/cpp/errors/issues.exp b/infer/tests/codetoanalyze/cpp/errors/issues.exp index 1a2f4a4fe..18d95d05b 100644 --- a/infer/tests/codetoanalyze/cpp/errors/issues.exp +++ b/infer/tests/codetoanalyze/cpp/errors/issues.exp @@ -172,6 +172,7 @@ codetoanalyze/cpp/errors/smart_ptr/weak_ptr.cpp, weak_ptr_observers::shared_stil codetoanalyze/cpp/errors/smart_ptr/weak_ptr.cpp, weak_ptr_observers::use_count_after_reset_bad, 5, use_count after weak_ptr reset is 0, [start of procedure weak_ptr_observers::use_count_after_reset_bad(),Condition is true,return from a call to weak_ptr_observers::use_count_after_reset_bad] codetoanalyze/cpp/errors/smart_ptr/weak_ptr.cpp, weak_ptr_observers::use_count_empty_bad, 5, use_count on empty weak_ptr is 0, [start of procedure weak_ptr_observers::use_count_empty_bad(),Condition is true,return from a call to weak_ptr_observers::use_count_empty_bad] codetoanalyze/cpp/errors/smart_ptr/weak_ptr_compil.cpp, weak_ptr_lock_repro_large::RDC::create::lambda_smart_ptr_weak_ptr_compil.cpp:62:7_operator(), 2, Cannot_star, [start of procedure operator(),Condition is true] +codetoanalyze/cpp/errors/stack_escape/basic.cpp, B_return_ref, 0, STACK_VARIABLE_ADDRESS_ESCAPE, [start of procedure return_ref,start of procedure A,return from a call to A_A,return from a call to B_return_ref] codetoanalyze/cpp/errors/stack_escape/basic.cpp, basic_escape_local_bad, 3, STACK_VARIABLE_ADDRESS_ESCAPE, [start of procedure basic_escape_local_bad(),return from a call to basic_escape_local_bad] codetoanalyze/cpp/errors/stack_escape/basic.cpp, basic_escape_param_bad, 0, STACK_VARIABLE_ADDRESS_ESCAPE, [start of procedure basic_escape_param_bad(),return from a call to basic_escape_param_bad] codetoanalyze/cpp/errors/stack_escape/basic.cpp, escape_local_struct_member_bad, 3, STACK_VARIABLE_ADDRESS_ESCAPE, [start of procedure escape_local_struct_member_bad(),start of procedure EscapeTest,return from a call to EscapeTest_EscapeTest,return from a call to escape_local_struct_member_bad] diff --git a/infer/tests/codetoanalyze/cpp/errors/stack_escape/basic.cpp b/infer/tests/codetoanalyze/cpp/errors/stack_escape/basic.cpp index d5227c67b..b926aeb5a 100644 --- a/infer/tests/codetoanalyze/cpp/errors/stack_escape/basic.cpp +++ b/infer/tests/codetoanalyze/cpp/errors/stack_escape/basic.cpp @@ -6,6 +6,7 @@ * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ + int* basic_escape_local_bad() { int a; return &a; @@ -16,7 +17,16 @@ int* basic_escape_param_bad(int a) { return &a; } struct EscapeTest { int x; }; + int* escape_local_struct_member_bad() { EscapeTest esc; return &(esc.x); } + +struct A { + A() {} +}; + +struct B { + const A& return_ref() { return A(); } +};