GH-1628: Return an error for duplicate record field names

This commit is contained in:
Tim Wojtulewicz 2022-07-18 16:21:04 -07:00
parent d57ad3e405
commit b41a4bf06d
5 changed files with 28 additions and 3 deletions

View file

@ -3493,8 +3493,6 @@ export {
free_space_start_filtering : int;
## minimum amount of free disk space required to continue document filtering
free_space_threshold : int;
## minimum amount of free disk space required to continue document filtering
free_space_threshold : int;
## default per-user disk quota
delete_quota_threshold : count;
## default per-user disk limit

View file

@ -1039,6 +1039,16 @@ void RecordType::AddField(unsigned int field, const TypeDecl* td)
ASSERT(field == field_inits.size());
ASSERT(field == managed_fields.size());
if ( field_ids.count(td->id) != 0 )
{
reporter->Error("Duplicate field '%s' found in record definition\n", td->id);
return;
}
else
{
field_ids.insert(std::string(td->id));
}
managed_fields.push_back(ZVal::IsManagedType(td->type));
auto init = new FieldInit();
@ -1106,7 +1116,7 @@ void RecordType::AddField(unsigned int field, const TypeDecl* td)
bool RecordType::HasField(const char* field) const
{
return FieldOffset(field) >= 0;
return field_ids.count(field) != 0;
}
ValPtr RecordType::FieldDefault(int field) const

View file

@ -725,6 +725,7 @@ protected:
int num_orig_fields;
type_decl_list* types;
std::set<std::string> field_ids;
};
class FileType final : public Type

View file

@ -0,0 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/record-duplicate-fields.zeek, line 7: Duplicate field 'a' found in record definition

View file

@ -0,0 +1,13 @@
# @TEST-EXEC-FAIL: zeek -b %INPUT
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
type test: record {
a: count;
a: string &optional;
};
event zeek_init()
{
local a = test($a=5);
print a;
}