Align error handling in to_int with existing behavior of to_count

Previously `to_int` would silently ignore invalid inputs and simply
return `0` while `to_count` would return an error; this patch changes
`to_int` to behave like `to_count`.

This introduces a breaking change in that `to_int` now raises an error
for trailing spaces (but still accepts leading spaces) where it
previously would have silently accepted it. This is consistent with
the behavior of `to_count`, but one could also argue that both of
these should only accept properly trimmed input; I did not go that route
since that would introduce breaking changes for both these functions
instead of for just one of them.
This commit is contained in:
Benjamin Bannier 2025-02-18 10:10:33 +01:00
parent 742f17fb15
commit 55533e12d4
2 changed files with 8 additions and 9 deletions

View file

@ -7,14 +7,15 @@
##! You'll find most of Zeek's built-in functions that aren't protocol-specific
##! in this file.
%%{ // C segment
#include <cmath>
#include <vector>
%%{ // C++ segment
#include <sys/stat.h>
#include <algorithm>
#include <cmath>
#include <sys/stat.h>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <vector>
#include "zeek/digest.h"
#include "zeek/Reporter.h"
@ -2613,12 +2614,8 @@ function to_int%(str: string%): int
zeek_int_t i = strtoll(s, &end_s, 10);
#if 0
// Not clear we should complain. For example, is " 205 "
// a legal conversion?
if ( s[0] == '\0' || end_s[0] != '\0' )
zeek::emit_builtin_error("bad conversion to integer", @ARG@[0]);
#endif
return zeek::val_mgr->Int(i);
%}
@ -2680,7 +2677,7 @@ function to_count%(str: string%): count
const char* s = str->CheckString();
char* end_s;
uint64_t u = (uint64_t) strtoull(s, &end_s, 10);
uint64_t u = static_cast<uint64_t>(strtoull(s, &end_s, 10));
if ( s[0] == '\0' || end_s[0] != '\0' )
{

View file

@ -1 +1,3 @@
### 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 )