analyzer: fix ICE copying struct [PR 94816]
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 28 Apr 2020 14:52:45 +0000 (10:52 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Tue, 28 Apr 2020 17:26:22 +0000 (13:26 -0400)
PR analyzer/94816 reports an ICE when attempting to copy a struct
containing a field for which add_region_for_type for fails (on
an OFFSET_TYPE): the region for the src field comes from
make_region_for_unexpected_tree_code which gives it a NULL type, and
then the copy calls add_region_for_type which unconditionally
dereferences the NULL type.

This patch fixes the ICE by checking for NULL types in
add_region_for_type.

gcc/analyzer/ChangeLog:
PR analyzer/94816
* engine.cc (impl_region_model_context::on_unexpected_tree_code):
Handle NULL tree.
* region-model.cc (region_model::add_region_for_type): Handle
NULL type.
* region-model.h
(test_region_model_context::on_unexpected_tree_code): Handle NULL
tree.

gcc/testsuite/ChangeLog:
PR analyzer/94816
* g++.dg/analyzer/pr94816.C: New test.

gcc/analyzer/ChangeLog
gcc/analyzer/engine.cc
gcc/analyzer/region-model.cc
gcc/analyzer/region-model.h
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/analyzer/pr94816.C [new file with mode: 0644]

index 3c8f45883a4870042ccb99c5eb163bb121e3ebf2..5cd736385aa75bed519ea3a6239c4bb43928b9ff 100644 (file)
@@ -1,3 +1,14 @@
+2020-04-28  David Malcolm  <dmalcolm@redhat.com>
+
+       PR analyzer/94816
+       * engine.cc (impl_region_model_context::on_unexpected_tree_code):
+       Handle NULL tree.
+       * region-model.cc (region_model::add_region_for_type): Handle
+       NULL type.
+       * region-model.h
+       (test_region_model_context::on_unexpected_tree_code): Handle NULL
+       tree.
+
 2020-04-28  David Malcolm  <dmalcolm@redhat.com>
 
        PR analyzer/94447
index 880e70fb2ba8a8602f2e97eb2e16b3be574a16d2..c73d493a3d87a3de959c3aa8441b783a0ccaa900 100644 (file)
@@ -699,7 +699,7 @@ impl_region_model_context::on_unexpected_tree_code (tree t,
   logger * const logger = get_logger ();
   if (logger)
     logger->log ("unhandled tree code: %qs in %qs at %s:%i",
-                get_tree_code_name (TREE_CODE (t)),
+                t ? get_tree_code_name (TREE_CODE (t)) : "(null)",
                 loc.get_impl_location ().m_function,
                 loc.get_impl_location ().m_file,
                 loc.get_impl_location ().m_line);
index 22049a34d29fc36f0b84dde12d2fc49ba7cea816..0794be9a5831f6ab07db6abe79f61602c6e45fb4 100644 (file)
@@ -6448,10 +6448,13 @@ region_id
 region_model::add_region_for_type (region_id parent_rid, tree type,
                                   region_model_context *ctxt)
 {
-  gcc_assert (TYPE_P (type));
+  if (type)
+    {
+      gcc_assert (TYPE_P (type));
 
-  if (region *new_region = make_region_for_type (parent_rid, type))
-    return add_region (new_region);
+      if (region *new_region = make_region_for_type (parent_rid, type))
+       return add_region (new_region);
+    }
 
   /* If we can't handle TYPE, return a placeholder region, and stop
      exploring this path.  */
index ad3dd1d13ef160025516338e277ec6f27aabd3a6..6d427c4c654c79c858d5f042921efa0f15a57863 100644 (file)
@@ -2205,7 +2205,7 @@ public:
     FINAL OVERRIDE
   {
     internal_error ("unhandled tree code: %qs",
-                   get_tree_code_name (TREE_CODE (t)));
+                   t ? get_tree_code_name (TREE_CODE (t)) : "(null)");
   }
 
 private:
index a1ee87e45232174886c98b8159710fafdee72023..74755cb23a02fd632d95a3b590c991b0a5e38d29 100644 (file)
@@ -1,3 +1,8 @@
+2020-04-28  David Malcolm  <dmalcolm@redhat.com>
+
+       PR analyzer/94816
+       * g++.dg/analyzer/pr94816.C: New test.
+
 2020-04-28  David Malcolm  <dmalcolm@redhat.com>
 
        PR analyzer/94447
diff --git a/gcc/testsuite/g++.dg/analyzer/pr94816.C b/gcc/testsuite/g++.dg/analyzer/pr94816.C
new file mode 100644 (file)
index 0000000..e241a44
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-additional-options "-O" } */
+
+struct jr;
+
+struct ch {
+  int jr::*rx;
+};
+
+ch
+ad ()
+{
+  return ch ();
+}