Skip to content

Commit

Permalink
Fix copy()/cloning vectors that have holes (indices w/ null values)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiwek committed Apr 20, 2021
1 parent a7b498a commit 180ab31
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Val.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3360,8 +3360,10 @@ ValPtr VectorVal::DoClone(CloneState* state)

for ( unsigned int i = 0; i < val.vector_val->size(); ++i )
{
auto v = (*val.vector_val)[i]->Clone(state);
vv->val.vector_val->push_back(std::move(v));
if ( (*val.vector_val)[i] )
vv->val.vector_val->push_back((*val.vector_val)[i]->Clone(state));
else
vv->val.vector_val->push_back(nullptr);
}

return vv;
Expand Down
2 changes: 2 additions & 0 deletions testing/btest/Baseline/language.vector/out
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ slicing assignment (PASS)
slicing assignment (PASS)
slicing assignment grow (PASS)
slicing assignment shrink (PASS)
copy of a vector with holes, 11, 11, [0, 1, 2, , , , , , , , 10], [0, 1, 2, , , , , , , , 10]
copy of a vector with trailing holes, 6, 6, [0, 1, 2, , , ], [0, 1, 2, , , ]
11 changes: 11 additions & 0 deletions testing/btest/language/vector.zeek
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,15 @@ event zeek_init()
test_case( "slicing assignment grow", all_set(v17 == vector(6, 2, 9, 10, 11, 5)) );
v17[2:5] = vector(9);
test_case( "slicing assignment shrink", all_set(v17 == vector(6, 2, 9, 5)) );

# Test copying of a vector with holes, as this used to crash.
local v18 = vector(0, 1, 2);
v18[10] = 10;
local v19 = copy(v18);
print "copy of a vector with holes", |v18|, |v19|, v18, v19;
# Even after removing some elements at the end, any trailing holes should
# be preserved after copying;
v18[6:] = vector();
local v20 = copy(v18);
print "copy of a vector with trailing holes", |v18|, |v20|, v18, v20;
}

0 comments on commit 180ab31

Please sign in to comment.