From 0ab6f883e0342da43752779336af13d1f34349d7 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Wed, 19 May 2021 08:56:10 -0700 Subject: [PATCH] [pulse] refactor check whether an address is allocated but not freed Summary: More straightforward (and better asymptotic complexity, not that it matters) that way. Also log when a leak is found in the debug html. Reviewed By: ezgicicek Differential Revision: D28536443 fbshipit-source-id: 08c329100 --- infer/src/pulse/PulseAbductiveDomain.ml | 27 +++++++++++-------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/infer/src/pulse/PulseAbductiveDomain.ml b/infer/src/pulse/PulseAbductiveDomain.ml index 2dbe98ef3..c4a600150 100644 --- a/infer/src/pulse/PulseAbductiveDomain.ml +++ b/infer/src/pulse/PulseAbductiveDomain.ml @@ -603,29 +603,26 @@ let skipped_calls_match_pattern astate = let check_memory_leaks unreachable_addrs astate = - let check_memory_leak attributes = + let check_memory_leak addr attributes = let allocated_not_freed_opt = - Attributes.fold attributes ~init:(None (* allocation trace *), false (* freed *)) - ~f:(fun acc attr -> - match (attr : Attribute.t) with - | Allocated (procname, trace) -> - (Some (procname, trace), snd acc) - | Invalid (CFree, _) -> - (fst acc, true) - | _ -> - acc ) + let allocated = Attributes.get_allocation attributes in + if Option.is_some allocated then + match Attributes.get_invalid attributes with Some (CFree, _) -> None | _ -> allocated + else None in match allocated_not_freed_opt with - | Some (procname, trace), false -> - (* allocated but not freed *) - Error (procname, trace) - | _ -> + | None -> Ok () + | Some (procname, trace) -> + (* allocated but not freed => leak *) + L.d_printfln ~color:Red "LEAK: unreachable address %a was allocated by %a" AbstractValue.pp + addr Procname.pp procname ; + Error (procname, trace) in List.fold_result unreachable_addrs ~init:() ~f:(fun () addr -> match AddressAttributes.find_opt addr astate with | Some unreachable_attrs -> - check_memory_leak unreachable_attrs + check_memory_leak addr unreachable_attrs | None -> Ok () )