From 55533e12d4886e049a70ef8ef616acf00cb009ed Mon Sep 17 00:00:00 2001 From: Benjamin Bannier Date: Tue, 18 Feb 2025 10:10:33 +0100 Subject: [PATCH] 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. --- src/zeek.bif | 15 ++++++--------- testing/btest/Baseline/bifs.to_int/err | 2 ++ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/zeek.bif b/src/zeek.bif index 628ed61aec..2d6d9b57ec 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -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 -#include +%%{ // C++ segment +#include + #include #include -#include +#include #include #include +#include #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(strtoull(s, &end_s, 10)); if ( s[0] == '\0' || end_s[0] != '\0' ) { diff --git a/testing/btest/Baseline/bifs.to_int/err b/testing/btest/Baseline/bifs.to_int/err index 49d861c74c..167043f7fd 100644 --- a/testing/btest/Baseline/bifs.to_int/err +++ b/testing/btest/Baseline/bifs.to_int/err @@ -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 )