BIT-1350: improve record coercion type checking.

For a field of the same name in both the target type and the coerced
type, a type mismatch is now reported as an error at parse-time.
This commit is contained in:
Jon Siwek 2015-04-27 16:37:40 -05:00
parent da24fa40a5
commit 48fccb3bce
4 changed files with 42 additions and 8 deletions

View file

@ -4186,6 +4186,9 @@ RecordCoerceExpr::RecordCoerceExpr(Expr* op, RecordType* r)
map[t_i] = i; map[t_i] = i;
} }
if ( IsError() )
return;
for ( i = 0; i < map_size; ++i ) for ( i = 0; i < map_size; ++i )
{ {
if ( map[i] == -1 ) if ( map[i] == -1 )

View file

@ -1971,18 +1971,33 @@ int same_attrs(const Attributes* a1, const Attributes* a2)
return (*a1 == *a2); return (*a1 == *a2);
} }
int record_promotion_compatible(const RecordType* /* super_rec */, int record_promotion_compatible(const RecordType* super_rec,
const RecordType* /* sub_rec */) const RecordType* sub_rec)
{ {
#if 0 for ( int i = 0; i < sub_rec->NumFields(); ++i )
int n = sub_rec->NumFields();
for ( int i = 0; i < n; ++i )
{ {
if ( ! super_rec->HasField(sub_rec->FieldName(i)) ) int o = super_rec->FieldOffset(sub_rec->FieldName(i));
if ( o < 0 )
// Orphaned field.
continue;
BroType* sub_field_type = sub_rec->FieldType(i);
BroType* super_field_type = super_rec->FieldType(o);
if ( same_type(sub_field_type, super_field_type) )
continue;
if ( sub_field_type->Tag() != TYPE_RECORD )
return 0;
if ( super_field_type->Tag() != TYPE_RECORD )
return 0;
if ( ! record_promotion_compatible(super_field_type->AsRecordType(),
sub_field_type->AsRecordType()) )
return 0; return 0;
} }
#endif
return 1; return 1;
} }

View file

@ -0,0 +1 @@
error in /Users/jon/Projects/bro/bro/testing/btest/.tmp/language.record-coerce-clash/record-coerce-clash.bro, line 13: type clash for field "cid" ((coerce [$cid=[$orig_h=1.2.3.4, $orig_p=0/tcp, $resp_h=0.0.0.0, $resp_p=wrong]] to myrec) and record { orig_h:addr; orig_p:port; resp_h:addr; resp_p:string; })

View file

@ -0,0 +1,15 @@
# @TEST-EXEC-FAIL: bro -b %INPUT >out 2>&1
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
# Record coercion attempt should report mismatched field types.
global wrong = "80/tcp";
type myrec: record {
cid: conn_id;
};
event bro_init()
{
local mr: myrec;
mr = [$cid = [$orig_h=1.2.3.4,$orig_p=0/tcp,$resp_h=0.0.0.0,$resp_p=wrong]];
get_port_transport_proto(mr$cid$resp_p);
}