cat_sep: Make fully vararg and do explicit runtime type checks

Using positional and vararg arguments for BIFs, it's not possible to do
proper runtime type checking on them as discussed in #2425. The bifcl produced
code unconditionally attempts to convert the positional arguments to StringVals,
but nothing ever type checks them. Instead of improving the vararg support in
Zeek script and bifcl, align cat_sep() with fmt() in making it fully vararg
and do implement type checks by hand.

With this change, passing wrong types for the separator and default argument
isn't a fatal error anymore and the error messages are also more descriptive.

It's a bit of a crutch working around varargs limitations.

Fixes #2425
This commit is contained in:
Arne Welzel 2022-10-27 11:15:49 +02:00
parent 096ff41966
commit a5f04b6270
10 changed files with 83 additions and 13 deletions

View file

@ -4,4 +4,5 @@ foo3T
3T
foo|3|T
<empty>
<empty>|3|T

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/cat_sep_errors.zeek, line 3: cat_sep() takes at least 2 arguments, got 1 (cat_sep(sep))

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/cat_sep_errors.zeek, line 4: expected type string for default, got count (cat_sep(sep, 1))

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/cat_sep_errors.zeek, line 4: expected type string for separator, got count (cat_sep(1, default))

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/cat_sep_errors.zeek, line 3: expected type string for separator, got record (cat_sep([$a=1], default))

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/cat_sep_errors.zeek, line 6: cat_sep() takes at least 2 arguments, got 0 (cat_sep())

View file

@ -17,6 +17,8 @@ event zeek_init()
print cat_sep("|", "<empty>", a, b, c);
print cat_sep("|", "<empty>");
print cat_sep("|", "<empty>", "");
print cat_sep("|", "<empty>", "", b, c);
}

View file

@ -0,0 +1,33 @@
# @TEST-DOC: Runtime errors calling cat_sep()
# @TEST-EXEC: zeek -b %INPUT
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
event zeek_init()
{
print cat_sep();
}
@TEST-START-NEXT
event zeek_init()
{
print cat_sep("sep");
}
@TEST-START-NEXT
# bad types 1
event zeek_init()
{
print cat_sep("sep", 1);
}
@TEST-START-NEXT
# bad types 2
event zeek_init()
{
print cat_sep(1, "default");
}
@TEST-START-NEXT
event zeek_init()
{
print cat_sep([$a=1], "default");
}