Fix redef of table index from clearing table. Addresses #1013.

`redef foo["x"] = 1` now acts like `redef foo += { ["x"] = 1 }`
instead of `redef foo = { ["x"] = 1 }`.
This commit is contained in:
Jon Siwek 2013-06-12 15:18:58 -05:00
parent f811e669ff
commit ae5a75bad9
3 changed files with 38 additions and 0 deletions

View file

@ -156,6 +156,12 @@ static void make_var(ID* id, BroType* t, init_class c, Expr* init,
if ( do_init )
{
if ( c == INIT_NONE && dt == VAR_REDEF && t->IsTable() &&
init && init->Tag() == EXPR_ASSIGN )
// e.g. 'redef foo["x"] = 1' is missing an init class, but the
// intention clearly isn't to overwrite entire existing table val.
c = INIT_EXTRA;
if ( (c == INIT_EXTRA && id->FindAttr(ATTR_ADD_FUNC)) ||
(c == INIT_REMOVE && id->FindAttr(ATTR_DEL_FUNC)) )
// Just apply the function.

View file

@ -0,0 +1,6 @@
{
[def] = 99.0,
[neat] = 1.0,
[cool] = 28.0,
[abc] = 8.0
}

View file

@ -0,0 +1,26 @@
# @TEST-EXEC: bro -b %INPUT > out
# @TEST-EXEC: btest-diff out
const foo: table[string] of double &redef;
# full (re)initialization
redef foo = { ["nope"] = 37.0 };
# full (re)initialization, discards "nope" index
redef foo = { ["abc"] = 42.0 };
# add elements
redef foo += { ["def"] = -42.0, ["ghi"] = 7.0 };
# remove elements from LHS based on indices shared with RHS
redef foo -= { ["ghi"] = 0.0 };
# RHS can be a table value
redef foo += table(["cool"] = 5.0, ["neat"] = 1.0);
# Redef at a single index is allowed, same as += when RHS has overlapping index
redef foo["cool"] = 28.0;
redef foo["abc"] = 8.0;
redef foo += { ["def"] = 99.0 };
print foo;