compiler: reject incorrect unsafe.Offsetof expressions.

The x.Field argument to Offsetof may not involve hidden
dereferences of embedded pointer fields.

Also correct uninitialized implicit_ field.

From-SVN: r200221
This commit is contained in:
Ian Lance Taylor 2013-06-19 21:24:58 +00:00
parent a09f1a766b
commit 8259d36468
2 changed files with 21 additions and 1 deletions

View 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;

View 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.