Also trim trailing spaces in to_count/to_int inputs

Previously we would already trim leading spaces in inputs to `to_count`
and `to_int`, effectively by just passing the behavior of the low-level
functions used in their implementations to the user. While this was
useful it was also inconsistent in that we did not allow trailing
spaces which we enable with this patch.
This commit is contained in:
Benjamin Bannier 2025-02-18 13:11:24 +01:00
parent 55533e12d4
commit d421a19691
4 changed files with 11 additions and 8 deletions

View file

@ -14,6 +14,7 @@
#include <cmath>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <vector>
@ -2614,8 +2615,11 @@ function to_int%(str: string%): int
zeek_int_t i = strtoll(s, &end_s, 10);
if ( s[0] == '\0' || end_s[0] != '\0' )
if ( s[0] == '\0' || std::any_of(static_cast<const char*>(end_s), s + ::strlen(s),
[](char c) { return ! (c == '\0' || ::isspace(c)); }) )
{
zeek::emit_builtin_error("bad conversion to integer", @ARG@[0]);
}
return zeek::val_mgr->Int(i);
%}
@ -2679,11 +2683,12 @@ function to_count%(str: string%): count
uint64_t u = static_cast<uint64_t>(strtoull(s, &end_s, 10));
if ( s[0] == '\0' || end_s[0] != '\0' )
{
if ( s[0] == '\0' || std::any_of(static_cast<const char*>(end_s), s + ::strlen(s),
[](char c) { return ! (c == '\0' || ::isspace(c)); }) )
{
zeek::emit_builtin_error("bad conversion to count", @ARG@[0]);
u = 0;
}
u = 0;
}
return zeek::val_mgr->Count(u);
%}

View file

@ -1,5 +1,4 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/to_count.zeek, line 8: bad conversion to count (int_to_count(a) and -2)
error in <...>/to_count.zeek, line 20: bad conversion to count (to_count() and )
error in <...>/to_count.zeek, line 24: bad conversion to count (to_count(206 ) and 206 )
error in <...>/to_count.zeek, line 25: bad conversion to count (to_count(not a count) and not a count)

View file

@ -7,7 +7,7 @@
0
18446744073709551611
205
0
206
0
123
9223372036854775808 and 9223372036854775808 are the same

View file

@ -1,3 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/to_int.zeek, line 10: bad conversion to integer (to_int(not an int) and not an int)
error in <...>/to_int.zeek, line 13: bad conversion to integer (to_int(206 ) and 206 )