From 74208d393132c74f1406fd5111012eedcd6e0065 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 8 Jul 2020 13:54:40 -0400 Subject: [PATCH 289/315] FIXME: allow for impl_region_model2_context::on_state_leak to not find a path_var for the sval --- gcc/analyzer/engine.cc | 13 ++++++--- gcc/analyzer/sm-malloc.cc | 57 ++++++++++++++++++++++++++++++--------- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc index ca97f2d9efd..72897cad8c2 100644 --- a/gcc/analyzer/engine.cc +++ b/gcc/analyzer/engine.cc @@ -680,7 +680,7 @@ public: logger * const logger = m_eg.get_logger (); LOG_FUNC (logger); - if (TREE_CODE (m_var) == SSA_NAME) + if (m_var && TREE_CODE (m_var) == SSA_NAME) { /* Locate the final write to this SSA name in the path. */ const gimple *def_stmt = SSA_NAME_DEF_STMT (m_var); @@ -1119,12 +1119,17 @@ impl_region_model2_context::on_state_leak (const state_machine &sm, path_var leaked_pv = m_old_state->m_region_model2->get_representative_path_var (sval, &visited); - if (!leaked_pv) - return; + /* This might be NULL; the pending_diagnostic subclasses need to cope + with this. */ tree leaked_tree = leaked_pv.m_tree; if (logger) - logger->log ("best leaked_tree: %qE", leaked_tree); + { + if (leaked_tree) + logger->log ("best leaked_tree: %qE", leaked_tree); + else + logger->log ("best leaked_tree: NULL"); + } leak_stmt_finder stmt_finder (*m_eg, leaked_tree); impl_sm_context sm_ctxt (*m_eg, sm_idx, sm, m_enode_for_diag, diff --git a/gcc/analyzer/sm-malloc.cc b/gcc/analyzer/sm-malloc.cc index b5ad39df792..417fa54ebc3 100644 --- a/gcc/analyzer/sm-malloc.cc +++ b/gcc/analyzer/sm-malloc.cc @@ -130,16 +130,34 @@ public: return label_text::borrow ("allocated here"); if (change.m_old_state == m_sm.m_unchecked && change.m_new_state == m_sm.m_nonnull) - return change.formatted_print ("assuming %qE is non-NULL", - change.m_expr); + { + if (change.m_expr) + return change.formatted_print ("assuming %qE is non-NULL", + change.m_expr); + else + return change.formatted_print ("assuming %qs is non-NULL", + ""); + } if (change.m_new_state == m_sm.m_null) { if (change.m_old_state == m_sm.m_unchecked) - return change.formatted_print ("assuming %qE is NULL", - change.m_expr); + { + if (change.m_expr) + return change.formatted_print ("assuming %qE is NULL", + change.m_expr); + else + return change.formatted_print ("assuming %qs is NULL", + ""); + } else - return change.formatted_print ("%qE is NULL", - change.m_expr); + { + if (change.m_expr) + return change.formatted_print ("%qE is NULL", + change.m_expr); + else + return change.formatted_print ("%qs is NULL", + ""); + } } return label_text (); @@ -483,8 +501,12 @@ public: { diagnostic_metadata m; m.add_cwe (401); - return warning_meta (rich_loc, m, OPT_Wanalyzer_malloc_leak, - "leak of %qE", m_arg); + if (m_arg) + return warning_meta (rich_loc, m, OPT_Wanalyzer_malloc_leak, + "leak of %qE", m_arg); + else + return warning_meta (rich_loc, m, OPT_Wanalyzer_malloc_leak, + "leak of %qs", ""); } label_text describe_state_change (const evdesc::state_change &change) @@ -500,11 +522,22 @@ public: label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE { - if (m_malloc_event.known_p ()) - return ev.formatted_print ("%qE leaks here; was allocated at %@", - ev.m_expr, &m_malloc_event); + if (ev.m_expr) + { + if (m_malloc_event.known_p ()) + return ev.formatted_print ("%qE leaks here; was allocated at %@", + ev.m_expr, &m_malloc_event); + else + return ev.formatted_print ("%qE leaks here", ev.m_expr); + } else - return ev.formatted_print ("%qE leaks here", ev.m_expr); + { + if (m_malloc_event.known_p ()) + return ev.formatted_print ("%qs leaks here; was allocated at %@", + "", &m_malloc_event); + else + return ev.formatted_print ("%qs leaks here", ""); + } } private: -- 2.26.2