diff --git a/src/Expr.cc b/src/Expr.cc index 7995d5d495..f5da47c506 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -4893,9 +4893,16 @@ Val* ListExpr::InitVal(const BroType* t, Val* aggr) const { ListVal* v = new ListVal(TYPE_ANY); + const type_list* tl = type->AsTypeList()->Types(); + if ( exprs.length() != tl->length() ) + { + Error("index mismatch", t); + return 0; + } + loop_over_list(exprs, i) { - Val* vi = exprs[i]->InitVal(t, 0); + Val* vi = exprs[i]->InitVal((*tl)[i], 0); if ( ! vi ) { Unref(v); diff --git a/testing/btest/Baseline/language.table-init-record-idx-2/output b/testing/btest/Baseline/language.table-init-record-idx-2/output new file mode 100644 index 0000000000..1496863177 --- /dev/null +++ b/testing/btest/Baseline/language.table-init-record-idx-2/output @@ -0,0 +1,25 @@ +following should all be true... +T +T +T +T +T +T +T +T +1 +1 +1 +following should all be false... +F +F +F +now here's the foo table... +{ +[[a=foo, b=2], 2] = 2, +[[a=baz, b=6], 6] = 6, +[[a=bar, b=4], 4] = 4, +[[a=baz, b=5], 5] = 5, +[[a=bar, b=3], 3] = 3, +[[a=foo, b=1], 1] = 1 +} diff --git a/testing/btest/Baseline/language.table-init-record-idx-3/output b/testing/btest/Baseline/language.table-init-record-idx-3/output new file mode 100644 index 0000000000..b8d1540d0a --- /dev/null +++ b/testing/btest/Baseline/language.table-init-record-idx-3/output @@ -0,0 +1,25 @@ +following should all be true... +T +T +T +T +T +T +T +T +1 +1 +1 +following should all be false... +F +F +F +now here's the foo table... +{ +[[a=baz, b=5]] = 5, +[[a=foo, b=2]] = 2, +[[a=baz, b=6]] = 6, +[[a=foo, b=1]] = 1, +[[a=bar, b=4]] = 4, +[[a=bar, b=3]] = 3 +} diff --git a/testing/btest/Baseline/language.table-init-record-idx-4/language.table-init-record-idx-2/output b/testing/btest/Baseline/language.table-init-record-idx-4/language.table-init-record-idx-2/output new file mode 100644 index 0000000000..1496863177 --- /dev/null +++ b/testing/btest/Baseline/language.table-init-record-idx-4/language.table-init-record-idx-2/output @@ -0,0 +1,25 @@ +following should all be true... +T +T +T +T +T +T +T +T +1 +1 +1 +following should all be false... +F +F +F +now here's the foo table... +{ +[[a=foo, b=2], 2] = 2, +[[a=baz, b=6], 6] = 6, +[[a=bar, b=4], 4] = 4, +[[a=baz, b=5], 5] = 5, +[[a=bar, b=3], 3] = 3, +[[a=foo, b=1], 1] = 1 +} diff --git a/testing/btest/Baseline/language.table-init-record-idx-4/output b/testing/btest/Baseline/language.table-init-record-idx-4/output new file mode 100644 index 0000000000..1496863177 --- /dev/null +++ b/testing/btest/Baseline/language.table-init-record-idx-4/output @@ -0,0 +1,25 @@ +following should all be true... +T +T +T +T +T +T +T +T +1 +1 +1 +following should all be false... +F +F +F +now here's the foo table... +{ +[[a=foo, b=2], 2] = 2, +[[a=baz, b=6], 6] = 6, +[[a=bar, b=4], 4] = 4, +[[a=baz, b=5], 5] = 5, +[[a=bar, b=3], 3] = 3, +[[a=foo, b=1], 1] = 1 +} diff --git a/testing/btest/Baseline/language.table-init-record-idx/output b/testing/btest/Baseline/language.table-init-record-idx/output new file mode 100644 index 0000000000..b8d1540d0a --- /dev/null +++ b/testing/btest/Baseline/language.table-init-record-idx/output @@ -0,0 +1,25 @@ +following should all be true... +T +T +T +T +T +T +T +T +1 +1 +1 +following should all be false... +F +F +F +now here's the foo table... +{ +[[a=baz, b=5]] = 5, +[[a=foo, b=2]] = 2, +[[a=baz, b=6]] = 6, +[[a=foo, b=1]] = 1, +[[a=bar, b=4]] = 4, +[[a=bar, b=3]] = 3 +} diff --git a/testing/btest/language/table-init-container-ctors.bro b/testing/btest/language/table-init-container-ctors.bro index 4829f41688..1f9e18d848 100644 --- a/testing/btest/language/table-init-container-ctors.bro +++ b/testing/btest/language/table-init-container-ctors.bro @@ -2,7 +2,7 @@ # @TEST-EXEC: btest-diff output # The various container constructor expressions should work in table -# initialization lists. +# initialization lists (as yields). type set_yield: set[string, count]; type vector_yield: vector of count; diff --git a/testing/btest/language/table-init-record-idx.bro b/testing/btest/language/table-init-record-idx.bro new file mode 100644 index 0000000000..db9716dc42 --- /dev/null +++ b/testing/btest/language/table-init-record-idx.bro @@ -0,0 +1,216 @@ +# @TEST-EXEC: bro -b %INPUT >output +# @TEST-EXEC: btest-diff output + +# Record constructors should work in table initializers + +type r: record { + a: string; + b: count; +}; + +global a: r = [$a="foo", $b=1]; +global b: r = [$a="foo", $b=2]; +global c: r = [$a="bar", $b=3]; +global d: r = [$a="bar", $b=4]; +global e: r = [$a="baz", $b=5]; +global f: r = [$a="baz", $b=6]; + +global foo: table[r] of count = { + [a] = 1, + [record($a="foo", $b=2)] = 2, + [[$a="bar", $b=3]] = 3, +}; + +foo[d] = 4; +foo[[$a="baz", $b=5]] = 5; +foo[record($a="baz", $b=6)] = 6; + +print "following should all be true..."; + +print a in foo; +print b in foo; +print c in foo; +print d in foo; +print e in foo; +print f in foo; + +print [$a="foo", $b=1] in foo; +print record($a="foo", $b=1) in foo; + +print foo[a]; +print foo[[$a="foo", $b=1]]; +print foo[record($a="foo", $b=1)]; + +print "following should all be false..."; + +local bah: r = [$a="bah", $b=0]; + +print bah in foo; +print [$a="bah", $b=0] in foo; +print record($a="bah", $b=0) in foo; + +print "now here's the foo table..."; + +print foo; + +# @TEST-START-NEXT + +# They can be part of a compound index type, too... + +type r: record { + a: string; + b: count; +}; + +global a: r = [$a="foo", $b=1]; +global b: r = [$a="foo", $b=2]; +global c: r = [$a="bar", $b=3]; +global d: r = [$a="bar", $b=4]; +global e: r = [$a="baz", $b=5]; +global f: r = [$a="baz", $b=6]; + +global foo: table[r, count] of count = { + [a, 1] = 1, + [record($a="foo", $b=2), 2] = 2, + [[$a="bar", $b=3], 3] = 3, +}; + +foo[d, 4] = 4; +foo[[$a="baz", $b=5], 5] = 5; +foo[record($a="baz", $b=6), 6] = 6; + +print "following should all be true..."; + +print [a, 1] in foo; +print [b, 2] in foo; +print [c, 3] in foo; +print [d, 4] in foo; +print [e, 5] in foo; +print [f, 6] in foo; + +print [[$a="foo", $b=1], 1] in foo; +print [record($a="foo", $b=1), 1] in foo; + +print foo[a, 1]; +print foo[[$a="foo", $b=1], 1]; +print foo[record($a="foo", $b=1), 1]; + +print "following should all be false..."; + +local bah: r = [$a="bah", $b=0]; + +print [bah, 0] in foo; +print [[$a="bah", $b=0], 0] in foo; +print [record($a="bah", $b=0), 0] in foo; + +print "now here's the foo table..."; + +print foo; + +# @TEST-START-NEXT + +# Now checking table() ctor versus { } initializer + +type r: record { + a: string; + b: count; +}; + +global a: r = [$a="foo", $b=1]; +global b: r = [$a="foo", $b=2]; +global c: r = [$a="bar", $b=3]; +global d: r = [$a="bar", $b=4]; +global e: r = [$a="baz", $b=5]; +global f: r = [$a="baz", $b=6]; + +global foo: table[r] of count = table( + [a] = 1, + [record($a="foo", $b=2)] = 2, + [[$a="bar", $b=3]] = 3 +); + +foo[d] = 4; +foo[[$a="baz", $b=5]] = 5; +foo[record($a="baz", $b=6)] = 6; + +print "following should all be true..."; + +print a in foo; +print b in foo; +print c in foo; +print d in foo; +print e in foo; +print f in foo; + +print [$a="foo", $b=1] in foo; +print record($a="foo", $b=1) in foo; + +print foo[a]; +print foo[[$a="foo", $b=1]]; +print foo[record($a="foo", $b=1)]; + +print "following should all be false..."; + +local bah: r = [$a="bah", $b=0]; + +print bah in foo; +print [$a="bah", $b=0] in foo; +print record($a="bah", $b=0) in foo; + +print "now here's the foo table..."; + +print foo; + +# @TEST-START-NEXT + +# Now checking table() ctor versus { } initializer for compound index + +type r: record { + a: string; + b: count; +}; + +global a: r = [$a="foo", $b=1]; +global b: r = [$a="foo", $b=2]; +global c: r = [$a="bar", $b=3]; +global d: r = [$a="bar", $b=4]; +global e: r = [$a="baz", $b=5]; +global f: r = [$a="baz", $b=6]; + +global foo: table[r, count] of count = table( + [a, 1] = 1, + [record($a="foo", $b=2), 2] = 2, + [[$a="bar", $b=3], 3] = 3 +); + +foo[d, 4] = 4; +foo[[$a="baz", $b=5], 5] = 5; +foo[record($a="baz", $b=6), 6] = 6; + +print "following should all be true..."; + +print [a, 1] in foo; +print [b, 2] in foo; +print [c, 3] in foo; +print [d, 4] in foo; +print [e, 5] in foo; +print [f, 6] in foo; + +print [[$a="foo", $b=1], 1] in foo; +print [record($a="foo", $b=1), 1] in foo; + +print foo[a, 1]; +print foo[[$a="foo", $b=1], 1]; +print foo[record($a="foo", $b=1), 1]; + +print "following should all be false..."; + +local bah: r = [$a="bah", $b=0]; + +print [bah, 0] in foo; +print [[$a="bah", $b=0], 0] in foo; +print [record($a="bah", $b=0), 0] in foo; + +print "now here's the foo table..."; + +print foo;