TableVal: Propagate &ordered through copy()

Copying an &ordered table or set would result in a copy that is not ordered.
This seems rather surprising behavior, so propagate the &ordered attribute.

Closes #2793
This commit is contained in:
Arne Welzel 2023-02-17 12:54:39 +01:00
parent 3de785114b
commit 754831d7b0
4 changed files with 71 additions and 1 deletions

View file

@ -2619,7 +2619,19 @@ double TableVal::CallExpireFunc(ListValPtr idx)
ValPtr TableVal::DoClone(CloneState* state)
{
auto tv = make_intrusive<TableVal>(table_type);
// Propagate the &ordered attribute when cloning.
//
// Some of the attributes are dealt with later, but this one needs to be
// passed explicitly to the TableVal constructor so the underlying PDict
// is initialized ordered.
detail::AttributesPtr init_attrs = nullptr;
if ( auto ordered_attr = GetAttr(detail::ATTR_ORDERED) )
{
init_attrs = zeek::make_intrusive<detail::Attributes>(table_type, false, false);
init_attrs->AddAttr(ordered_attr);
}
auto tv = make_intrusive<TableVal>(table_type, init_attrs);
state->NewClone(this, tv);
const PDict<TableEntryVal>* tbl = AsTable();

View file

@ -0,0 +1 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.

View file

@ -0,0 +1,25 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
sset
2
3
1
4
5
copy(sset)
3
2
1
4
5
ordered_sset
1
2
3
4
5
copy(ordered_sset)
1
2
3
4
5

View file

@ -0,0 +1,32 @@
# @TEST-EXEC: zeek -b %INPUT >out
# @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff .stderr
global sset: set[string];
global ordered_sset: set[string] &ordered;
event zeek_init()
{
local i = 0;
while ( ++i <= 5 )
{
add sset[cat(i)];
add ordered_sset[cat(i)];
}
print "sset";
for ( s in sset )
print s;
print "copy(sset)";
for ( s in copy(sset) )
print s;
print "ordered_sset";
for ( s in ordered_sset )
print s;
print "copy(ordered_sset)";
for ( s in copy(ordered_sset) )
print s;
}