Patchwork Go patch committed: Don't crash on empty struct due to recursive ref

login
register
mail settings
Submitter Ian Taylor
Date Dec. 15, 2010, 11:40 p.m.
Message ID <mcr4oae1u6o.fsf@google.com>
Download mbox | patch
Permalink /patch/75703/
State New
Headers show

Comments

Ian Taylor - Dec. 15, 2010, 11:40 p.m.
When there is a recursive reference to a struct, such as via a pointer,
the Go frontend will build a RECORD_TYPE node and then fill it in
later.  Sometimes it turns out that the struct has an error, and later
never comes.  This patch avoids a crash for that case when referencing a
field in such a struct.  Bootstrapped and tested on
x86_64-unknown-linux-gnu.  Committed to mainline.

Ian

Patch

diff -r 2e4bc89f0e29 go/expressions.cc
--- a/go/expressions.cc	Wed Dec 15 14:40:54 2010 -0800
+++ b/go/expressions.cc	Wed Dec 15 15:34:46 2010 -0800
@@ -9628,7 +9628,13 @@ 
     return error_mark_node;
   gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
   tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
-  gcc_assert(field != NULL_TREE);
+  if (field == NULL_TREE)
+    {
+      // This can happen for a type which refers to itself indirectly
+      // and then turns out to be erroneous.
+      gcc_assert(saw_errors());
+      return error_mark_node;
+    }
   for (unsigned int i = this->field_index_; i > 0; --i)
     {
       field = DECL_CHAIN(field);