[committed] analyzer: fix ICE adding note to disabled diagnostic [PR104997]

David Malcolm dmalcolm@redhat.com
Wed Mar 23 12:40:20 GMT 2022


Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r12-7782-g160b095fc9ded4.

gcc/analyzer/ChangeLog:
	PR analyzer/104997
	* diagnostic-manager.cc (diagnostic_manager::add_diagnostic):
	Convert return type from "void" to "bool", reporting success vs
	failure to caller, for both overloads.
	* diagnostic-manager.h (diagnostic_manager::add_diagnostic):
	Likewise.
	* engine.cc (impl_region_model_context::warn): Propagate return
	value from diagnostic_manager::add_diagnostic.

gcc/testsuite/ChangeLog:
	PR analyzer/104997
	* gcc.dg/analyzer/write-to-string-literal-4-disabled.c: New test,
	adapted from write-to-string-literal-4.c.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
---
 gcc/analyzer/diagnostic-manager.cc            | 18 ++++++++----
 gcc/analyzer/diagnostic-manager.h             |  4 +--
 gcc/analyzer/engine.cc                        |  9 ++----
 .../write-to-string-literal-4-disabled.c      | 28 +++++++++++++++++++
 4 files changed, 45 insertions(+), 14 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/analyzer/write-to-string-literal-4-disabled.c

diff --git a/gcc/analyzer/diagnostic-manager.cc b/gcc/analyzer/diagnostic-manager.cc
index d5e5b6926cc..bf7c8fc5147 100644
--- a/gcc/analyzer/diagnostic-manager.cc
+++ b/gcc/analyzer/diagnostic-manager.cc
@@ -874,9 +874,11 @@ diagnostic_manager::diagnostic_manager (logger *logger, engine *eng,
 {
 }
 
-/* Queue pending_diagnostic D at ENODE for later emission.  */
+/* Queue pending_diagnostic D at ENODE for later emission.
+   Return true/false signifying if the diagnostic was actually added.
+   Take ownership of D (or delete it).  */
 
-void
+bool
 diagnostic_manager::add_diagnostic (const state_machine *sm,
 				    exploded_node *enode,
 				    const supernode *snode, const gimple *stmt,
@@ -907,7 +909,7 @@ diagnostic_manager::add_diagnostic (const state_machine *sm,
 				d->get_kind ());
 	  delete d;
 	  m_num_disabled_diagnostics++;
-	  return;
+	  return false;
 	}
     }
 
@@ -920,18 +922,22 @@ diagnostic_manager::add_diagnostic (const state_machine *sm,
     log ("adding saved diagnostic %i at SN %i to EN %i: %qs",
 	 sd->get_index (),
 	 snode->m_index, enode->m_index, d->get_kind ());
+  return true;
 }
 
-/* Queue pending_diagnostic D at ENODE for later emission.  */
+/* Queue pending_diagnostic D at ENODE for later emission.
+   Return true/false signifying if the diagnostic was actually added.
+   Take ownership of D (or delete it).  */
 
-void
+bool
 diagnostic_manager::add_diagnostic (exploded_node *enode,
 				    const supernode *snode, const gimple *stmt,
 				    stmt_finder *finder,
 				    pending_diagnostic *d)
 {
   gcc_assert (enode);
-  add_diagnostic (NULL, enode, snode, stmt, finder, NULL_TREE, NULL, 0, d);
+  return add_diagnostic (NULL, enode, snode, stmt, finder, NULL_TREE,
+			 NULL, 0, d);
 }
 
 /* Add PN to the most recent saved_diagnostic.  */
diff --git a/gcc/analyzer/diagnostic-manager.h b/gcc/analyzer/diagnostic-manager.h
index 34abf56d11f..fc5dc043c78 100644
--- a/gcc/analyzer/diagnostic-manager.h
+++ b/gcc/analyzer/diagnostic-manager.h
@@ -107,7 +107,7 @@ public:
 
   json::object *to_json () const;
 
-  void add_diagnostic (const state_machine *sm,
+  bool add_diagnostic (const state_machine *sm,
 		       exploded_node *enode,
 		       const supernode *snode, const gimple *stmt,
 		       stmt_finder *finder,
@@ -116,7 +116,7 @@ public:
 		       state_machine::state_t state,
 		       pending_diagnostic *d);
 
-  void add_diagnostic (exploded_node *enode,
+  bool add_diagnostic (exploded_node *enode,
 		       const supernode *snode, const gimple *stmt,
 		       stmt_finder *finder,
 		       pending_diagnostic *d);
diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index f911ed4ac39..caa8796b494 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -129,12 +129,9 @@ impl_region_model_context::warn (pending_diagnostic *d)
       return false;
     }
   if (m_eg)
-    {
-      m_eg->get_diagnostic_manager ().add_diagnostic
-	(m_enode_for_diag, m_enode_for_diag->get_supernode (),
-	 m_stmt, m_stmt_finder, d);
-      return true;
-    }
+    return m_eg->get_diagnostic_manager ().add_diagnostic
+      (m_enode_for_diag, m_enode_for_diag->get_supernode (),
+       m_stmt, m_stmt_finder, d);
   else
     {
       delete d;
diff --git a/gcc/testsuite/gcc.dg/analyzer/write-to-string-literal-4-disabled.c b/gcc/testsuite/gcc.dg/analyzer/write-to-string-literal-4-disabled.c
new file mode 100644
index 00000000000..fa21af13341
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/write-to-string-literal-4-disabled.c
@@ -0,0 +1,28 @@
+/* Verify that we can disable warnings that have notes added to them via
+   region_model_context_decorator::add_note.  */
+
+/* { dg-additional-options "-Wno-analyzer-write-to-string-literal" } */
+
+typedef __SIZE_TYPE__ size_t;
+
+int getrandom (void *__buffer, size_t __length, /* { dg-bogus "parameter 1 of 'getrandom' marked with attribute 'access \\(write_only, 1, 2\\)'" } */
+	       unsigned int __flags)
+  __attribute__ ((access (__write_only__, 1, 2)));
+
+#define GRND_RANDOM 0x02
+
+void test (int flag)
+{
+  char *buf;
+
+  if (flag)
+    buf = __builtin_malloc (1024);
+  else
+    buf = (char *)""; /* { dg-bogus "here" } */
+
+  if (getrandom(buf, 16, GRND_RANDOM)) /* { dg-bogus "write to string literal" } */
+    __builtin_printf("%s\n", buf);
+
+  if (flag)
+    __builtin_free (buf);
+}
-- 
2.26.3



More information about the Gcc-patches mailing list