From d421a196912b488ce3fadd3e92ddce505f182098 Mon Sep 17 00:00:00 2001 From: Benjamin Bannier Date: Tue, 18 Feb 2025 13:11:24 +0100 Subject: [PATCH] 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. --- src/zeek.bif | 15 ++++++++++----- testing/btest/Baseline/bifs.to_count/err | 1 - testing/btest/Baseline/bifs.to_count/out | 2 +- testing/btest/Baseline/bifs.to_int/err | 1 - 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/zeek.bif b/src/zeek.bif index 2d6d9b57ec..b96dfc842a 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -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(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(strtoull(s, &end_s, 10)); - if ( s[0] == '\0' || end_s[0] != '\0' ) - { + if ( s[0] == '\0' || std::any_of(static_cast(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); %} diff --git a/testing/btest/Baseline/bifs.to_count/err b/testing/btest/Baseline/bifs.to_count/err index 74d4bfa3f1..1b96f363d0 100644 --- a/testing/btest/Baseline/bifs.to_count/err +++ b/testing/btest/Baseline/bifs.to_count/err @@ -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) diff --git a/testing/btest/Baseline/bifs.to_count/out b/testing/btest/Baseline/bifs.to_count/out index 5175a96dd3..d727a32bbe 100644 --- a/testing/btest/Baseline/bifs.to_count/out +++ b/testing/btest/Baseline/bifs.to_count/out @@ -7,7 +7,7 @@ 0 18446744073709551611 205 -0 +206 0 123 9223372036854775808 and 9223372036854775808 are the same diff --git a/testing/btest/Baseline/bifs.to_int/err b/testing/btest/Baseline/bifs.to_int/err index 167043f7fd..1e9d23dcb2 100644 --- a/testing/btest/Baseline/bifs.to_int/err +++ b/testing/btest/Baseline/bifs.to_int/err @@ -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 )