* 'table-error' of https://github.com/ZekeMedley/zeek:
  Check table yield type on assignment.

Also extended the type checking to include sets as well as the full
table type (yield type as well as index types).
This commit is contained in:
Jon Siwek 2019-05-28 10:48:37 -07:00
commit f2b7764769
5 changed files with 52 additions and 2 deletions

View file

@ -1,4 +1,8 @@
2.6-342 | 2019-05-28 10:48:37 -0700
* GH-168: Improve type-checking for table/set list assignment. (Zeke Medley and Jon Siwek, Corelight)
2.6-340 | 2019-05-24 18:02:43 -0700
* Add support for parsing additional DHCP options (Jay Wren)

View file

@ -1 +1 @@
2.6-340
2.6-342

View file

@ -2554,7 +2554,6 @@ bool AssignExpr::TypeCheck(attr_list* attrs)
if ( bt1 == TYPE_TABLE && op2->Tag() == EXPR_LIST )
{
attr_list* attr_copy = 0;
if ( attrs )
{
attr_copy = new attr_list(attrs->length());
@ -2562,11 +2561,23 @@ bool AssignExpr::TypeCheck(attr_list* attrs)
attr_copy->append((*attrs)[i]);
}
bool empty_list_assignment = (op2->AsListExpr()->Exprs().length() == 0);
if ( op1->Type()->IsSet() )
op2 = new SetConstructorExpr(op2->AsListExpr(), attr_copy);
else
op2 = new TableConstructorExpr(op2->AsListExpr(), attr_copy);
if ( ! empty_list_assignment && ! same_type(op1->Type(), op2->Type()) )
{
if ( op1->Type()->IsSet() )
ExprError("set type mismatch in assignment");
else
ExprError("table type mismatch in assignment");
return false;
}
return true;
}

View file

@ -0,0 +1,3 @@
error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.table-list-assign-type-check/table-list-assign-type-check.zeek, lines 15-20: table type mismatch in assignment (service_table_bad_yield = table(www, 80 = Internal Web Server, dns1, 53 = Internal DNS 1, dns2, 53 = Internal DNS 2, dhcp-for-wifi, 443 = DHCP Management interface for WiFi))
error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.table-list-assign-type-check/table-list-assign-type-check.zeek, lines 23-28: table type mismatch in assignment (service_table_bad_index = table(www, 80 = Internal Web Server, dns1, 53 = Internal DNS 1, dns2, 53 = Internal DNS 2, dhcp-for-wifi, 443 = DHCP Management interface for WiFi))
error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.table-list-assign-type-check/table-list-assign-type-check.zeek, line 31: set type mismatch in assignment (test_set_bad = set(1, 2, 3))

View file

@ -0,0 +1,32 @@
# @TEST-EXEC-FAIL: zeek -b %INPUT >output 2>&1
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff output
event zeek_init()
{
# This assignment should pass type-checking.
local service_table_good: table[string, count] of string = {
["www", 80] = "Internal Web Server",
["dns1", 53] = "Internal DNS 1",
["dns2", 53] = "Internal DNS 2",
["dhcp-for-wifi", 443] = "DHCP Management interface for WiFi"
};
# This assignment should fail type-checking due to yield mismatch.
local service_table_bad_yield: table[string, count] of count = {
["www", 80] = "Internal Web Server",
["dns1", 53] = "Internal DNS 1",
["dns2", 53] = "Internal DNS 2",
["dhcp-for-wifi", 443] = "DHCP Management interface for WiFi"
};
# This assignment should fail type-checking due to index mismatch.
local service_table_bad_index: table[string, count] of string = {
["www", "80"] = "Internal Web Server",
["dns1", "53"] = "Internal DNS 1",
["dns2", "53"] = "Internal DNS 2",
["dhcp-for-wifi", "443"] = "DHCP Management interface for WiFi"
};
local test_set_good: set[string] = {"1", "2", "3"};
local test_set_bad: set[string] = {1, 2, 3};
}