Add type checking to string_cat arguments

This commit is contained in:
Tim Wojtulewicz 2022-08-26 15:30:11 -07:00
parent 686eb54f95
commit ed4521af60
6 changed files with 37 additions and 0 deletions

View file

@ -64,7 +64,15 @@ function string_cat%(...%): string
%{
int n = 0;
for ( const auto& a : @ARG@ )
{
if ( a->GetType()->Tag() != TYPE_STRING )
{
zeek::reporter->Error("string_cat() requires string arguments");
return val_mgr->EmptyString();
}
n += a->AsString()->Len();
}
u_char* b = new u_char[n+1];
zeek::String* s = new zeek::String(1, b, n);

View file

@ -66,3 +66,6 @@ find_str: -1
find_str: 4
find_str: 4
string_cat
-----------------------------------------------------
string_cat: abc

View file

@ -0,0 +1,6 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error: Fill string passed to ljust() must be a single character
error: Fill string passed to rjust() must be a single character
error: find_str: end position must be greater than start position
error: find_str: end position must be greater than start position
error: string_cat() requires string arguments

View file

@ -1,5 +1,12 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
Justification (input string 'abc')
----------------------------------
find_str/rfind_str (input string 'abcdefghi')
-----------------------------------------------------
find_str: -1
find_str: -1
string_cat
-----------------------------------------------------
string_cat:

View file

@ -78,4 +78,8 @@ event zeek_init()
print fmt("find_str: %d", rfind_str(s3, "efg"));
print fmt("find_str: %d", rfind_str(s3, "efg", 2, 6));
print "";
print fmt("string_cat");
print "-----------------------------------------------------";
print fmt("string_cat: %s", string_cat("a", "b", "c"));
}

View file

@ -4,6 +4,7 @@
#
# @TEST-EXEC: zeek -b %INPUT >out
# @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff .stderr
event zeek_init()
{
@ -12,8 +13,16 @@ event zeek_init()
local s1 : string = "abc";
print fmt("ljust: '%s'", ljust(s1, 2, "--")); # This should return an error
print fmt("rjust: '%s'", rjust(s1, 2, "--")); # This should return an error
print "";
local s3: string = "abcdefghi";
print fmt("find_str/rfind_str (input string '%s')", s3);
print "-----------------------------------------------------";
print fmt("find_str: %d", find_str(s3, "efg", 6, 2));
print fmt("find_str: %d", rfind_str(s3, "efg", 6, 2));
print "";
print fmt("string_cat");
print "-----------------------------------------------------";
print fmt("string_cat: %s", string_cat("a", 1, "c"));
}