Skip to content

Commit 52b4325

Browse files
dulmarodfacebook-github-bot
authored andcommitted
[cxx-string-captured] Report if a string pointer which is the internal pointer of a cxx string is captured
Summary: Before we were reporting when a local variable of type `std::string` was captured, but this was wrong in many cases as the variable was copied before being captured. What we actually wanted to catch is examples like this ``` example { std::string cstring("abc"); const char* ptr = cstring.c_str(); /// access the ptr in a block that goes out of scope. // fine to capture it in a non escaping block /// we expect the cstring to be destroyed here and ptr to point to dangling address.(use after free) } ``` So now we adapted the checker to catch this case instead. We add a new context_info for captured variables for when a variable is the internal pointer of a local variable of a certain type. Then we do a preanalysis to compute this, and in the actual checker we check if the variable is an internal pointer and report accordingly. Reviewed By: skcho Differential Revision: D64836637 fbshipit-source-id: 967eefb645c1d7f3d412a616e64b327e77384fb0
1 parent 365995c commit 52b4325

File tree

6 files changed

+317
-157
lines changed

6 files changed

+317
-157
lines changed

infer/src/IR/CapturedVar.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ let is_captured_by_ref captured_mode =
2020
type captured_info = {loc: Location.t; is_formal: Procname.t option}
2121
[@@deriving compare, equal, sexp, hash, normalize]
2222

23-
type context_info = {is_checked_for_null: bool} [@@deriving compare, equal, sexp, hash, normalize]
23+
type context_info = {is_checked_for_null: bool; is_internal_pointer_of: Typ.t option}
24+
[@@deriving compare, equal, sexp, hash, normalize]
2425

2526
type t =
2627
{ pvar: Pvar.t
@@ -34,8 +35,10 @@ let pp_captured_info fmt {loc; is_formal} =
3435
F.fprintf fmt "(%a, %a)" (Pp.option Procname.pp) is_formal Location.pp loc
3536

3637

37-
let pp_context_info fmt {is_checked_for_null} =
38-
F.fprintf fmt "is_checked_for_null=%b" is_checked_for_null
38+
let pp_context_info fmt {is_checked_for_null; is_internal_pointer_of} =
39+
F.fprintf fmt "is_checked_for_null=%b, is_internal_pointer=%a" is_checked_for_null
40+
(Pp.option (Typ.pp Pp.text))
41+
is_internal_pointer_of
3942

4043

4144
let pp fmt {pvar; typ; capture_mode; captured_from; context_info} =

infer/src/IR/CapturedVar.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ val is_captured_by_ref : capture_mode -> bool
1717
type captured_info = {loc: Location.t; is_formal: Procname.t option}
1818
[@@deriving compare, equal, sexp, hash, normalize]
1919

20-
type context_info = {is_checked_for_null: bool} [@@deriving compare, equal, sexp, hash, normalize]
20+
type context_info = {is_checked_for_null: bool; is_internal_pointer_of: Typ.t option}
21+
[@@deriving compare, equal, sexp, hash, normalize]
2122

2223
(** captured_from and context_info only set for captured variables in Objective-C blocks *)
2324
type t =

0 commit comments

Comments
 (0)