compiler: reject incorrect unsafe.Offsetof expressions.
authorIan Lance Taylor <ian@gcc.gnu.org>
Wed, 19 Jun 2013 21:24:58 +0000 (21:24 +0000)
committerIan Lance Taylor <ian@gcc.gnu.org>
Wed, 19 Jun 2013 21:24:58 +0000 (21:24 +0000)
The x.Field argument to Offsetof may not involve hidden
dereferences of embedded pointer fields.

Also correct uninitialized implicit_ field.

From-SVN: r200221

gcc/go/gofrontend/expressions.cc
gcc/go/gofrontend/expressions.h

index 9c1efb3..adadfbb 100644 (file)
@@ -6955,6 +6955,26 @@ Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function,
       return Expression::make_error(loc);
     }
 
+  if (this->code_ == BUILTIN_OFFSETOF)
+    {
+      Expression* arg = this->one_arg();
+      Field_reference_expression* farg = arg->field_reference_expression();
+      while (farg != NULL)
+       {
+         if (!farg->implicit())
+           break;
+         // When the selector refers to an embedded field,
+         // it must not be reached through pointer indirections.
+         if (farg->expr()->deref() != farg->expr())
+           {
+             this->report_error(_("argument of Offsetof implies indirection of an embedded field"));
+             return this;
+           }
+         // Go up until we reach the original base.
+         farg = farg->expr()->field_reference_expression();
+       }
+    }
   if (this->is_constant())
     {
       Numeric_constant nc;
index ed3909c..3f4db91 100644 (file)
@@ -1872,7 +1872,7 @@ class Field_reference_expression : public Expression
   Field_reference_expression(Expression* expr, unsigned int field_index,
                             Location location)
     : Expression(EXPRESSION_FIELD_REFERENCE, location),
-      expr_(expr), field_index_(field_index), called_fieldtrack_(false)
+      expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
   { }
 
   // Return the struct expression.