From 0b81bf5ad3ce9e2c18ad76d6099ff6f85359b3c6 Mon Sep 17 00:00:00 2001 From: Mark Overholser Date: Sat, 15 Feb 2025 07:53:38 -0600 Subject: [PATCH 1/6] Fixes #4076; update BIFs to_int() and to_count() to accept optional 'base' argument for more easy conversions of strings representing (for example) hexadecimal, octal, or binary numbers --- src/zeek.bif | 8 ++++---- testing/btest/Baseline/bifs.to_count/out | 3 +++ testing/btest/Baseline/bifs.to_int/out | 3 +++ testing/btest/bifs/to_count.zeek | 3 +++ testing/btest/bifs/to_int.zeek | 3 +++ 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/zeek.bif b/src/zeek.bif index 628ed61aec..af5dbee259 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -2606,12 +2606,12 @@ function enum_to_int%(e: any%): int ## Returns: The :zeek:type:`string` *str* as :zeek:type:`int`. ## ## .. zeek:see:: to_addr to_port to_subnet -function to_int%(str: string%): int +function to_int%(str: string, base: count &default=10%): int %{ const char* s = str->CheckString(); char* end_s; - zeek_int_t i = strtoll(s, &end_s, 10); + zeek_int_t i = strtoll(s, &end_s, base); #if 0 // Not clear we should complain. For example, is " 205 " @@ -2675,12 +2675,12 @@ function double_to_count%(d: double%): count ## an invalid format. ## ## .. zeek:see:: to_addr to_int to_port to_subnet -function to_count%(str: string%): count +function to_count%(str: string, base: count &default=10%): count %{ const char* s = str->CheckString(); char* end_s; - uint64_t u = (uint64_t) strtoull(s, &end_s, 10); + uint64_t u = (uint64_t) strtoull(s, &end_s, base); if ( s[0] == '\0' || end_s[0] != '\0' ) { diff --git a/testing/btest/Baseline/bifs.to_count/out b/testing/btest/Baseline/bifs.to_count/out index 0c99f6e402..da8bb62256 100644 --- a/testing/btest/Baseline/bifs.to_count/out +++ b/testing/btest/Baseline/bifs.to_count/out @@ -6,6 +6,9 @@ 7 0 18446744073709551611 +172 +35 +195 0 123 9223372036854775808 and 9223372036854775808 are the same diff --git a/testing/btest/Baseline/bifs.to_int/out b/testing/btest/Baseline/bifs.to_int/out index c0c6d56ef5..8b989fe623 100644 --- a/testing/btest/Baseline/bifs.to_int/out +++ b/testing/btest/Baseline/bifs.to_int/out @@ -1,6 +1,9 @@ ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. 1 -1 +188 +39 +243 4294967296 0 3 diff --git a/testing/btest/bifs/to_count.zeek b/testing/btest/bifs/to_count.zeek index 7489ca8b79..ae90bc157a 100644 --- a/testing/btest/bifs/to_count.zeek +++ b/testing/btest/bifs/to_count.zeek @@ -19,6 +19,9 @@ event zeek_init() print to_count("7"); print to_count(""); print to_count("-5"); + print to_int("10101100", 2); + print to_int("43", 8); + print to_int("C3", 16); print to_count("not a count"); local e: port = 123/tcp; diff --git a/testing/btest/bifs/to_int.zeek b/testing/btest/bifs/to_int.zeek index 17e433f975..42dea4f619 100644 --- a/testing/btest/bifs/to_int.zeek +++ b/testing/btest/bifs/to_int.zeek @@ -6,6 +6,9 @@ event zeek_init() { print to_int("1"); print to_int("-1"); + print to_int("10111100", 2); + print to_int("47", 8); + print to_int("F3", 16); print to_int("4294967296"); print to_int("not an int"); From df348ff2f2c958623ad4518cb85b6e59dfe6fc43 Mon Sep 17 00:00:00 2001 From: Mark Overholser Date: Sat, 15 Feb 2025 07:53:38 -0600 Subject: [PATCH 2/6] update BIFs to_int() and to_count() to accept optional 'base' argument; allows more more exotic conversions from hex, octal, binary --- testing/btest/Baseline/language.assert-3/out | 2 +- testing/btest/Baseline/language.assert-hook-2/out | 2 +- testing/btest/Baseline/language.assert-misc/out | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/testing/btest/Baseline/language.assert-3/out b/testing/btest/Baseline/language.assert-3/out index 5a58147fa5..5194c63015 100644 --- a/testing/btest/Baseline/language.assert-3/out +++ b/testing/btest/Baseline/language.assert-3/out @@ -1,3 +1,3 @@ ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. -error in <...>/assert.zeek, line 3: assertion failure: (coerce to_count("42") to double) == 42.5 (always failing) +error in <...>/assert.zeek, line 3: assertion failure: (coerce to_count("42", 10) to double) == 42.5 (always failing) fatal error: errors occurred while initializing diff --git a/testing/btest/Baseline/language.assert-hook-2/out b/testing/btest/Baseline/language.assert-hook-2/out index f898a52732..bfd0473f20 100644 --- a/testing/btest/Baseline/language.assert-hook-2/out +++ b/testing/btest/Baseline/language.assert-hook-2/out @@ -1,5 +1,5 @@ ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. -assertion_failure, to_count("5") == 4, 5 is not 4 +assertion_failure, to_count("5", 10) == 4, 5 is not 4 assert <...>/assert-hook.zeek:21 f <...>/assert-hook.zeek:25 g <...>/assert-hook.zeek:26 diff --git a/testing/btest/Baseline/language.assert-misc/out b/testing/btest/Baseline/language.assert-misc/out index 1e529fdcaf..b072ff767c 100644 --- a/testing/btest/Baseline/language.assert-misc/out +++ b/testing/btest/Baseline/language.assert-misc/out @@ -3,9 +3,9 @@ f, lambda_<10820400278317158366>: function() : void { assert 0 < getpid(), fmt("my pid is funny: %s", getpid()); } -g, lambda_<9730512750166342063>: function() : void +g, lambda_<16208386833253569415>: function() : void { -assert to_count("42") == 42; +assert to_count("42", 10) == 42; } test_function, test_function: function() : void { From 8fce9c34f808380f7afe9da75742da309cdacc14 Mon Sep 17 00:00:00 2001 From: Mark Overholser Date: Sun, 23 Feb 2025 16:19:12 -0600 Subject: [PATCH 3/6] Fix tests in to_count to reflect appropriate function; add tests for 0x-formatted hex values for to_count and to_int --- testing/btest/bifs/to_count.zeek | 7 ++++--- testing/btest/bifs/to_int.zeek | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/testing/btest/bifs/to_count.zeek b/testing/btest/bifs/to_count.zeek index ae90bc157a..cda4ab432c 100644 --- a/testing/btest/bifs/to_count.zeek +++ b/testing/btest/bifs/to_count.zeek @@ -19,9 +19,10 @@ event zeek_init() print to_count("7"); print to_count(""); print to_count("-5"); - print to_int("10101100", 2); - print to_int("43", 8); - print to_int("C3", 16); + print to_count("10101100", 2); + print to_count("43", 8); + print to_count("C3", 16); + print to_count("0xC3", 16); print to_count("not a count"); local e: port = 123/tcp; diff --git a/testing/btest/bifs/to_int.zeek b/testing/btest/bifs/to_int.zeek index 42dea4f619..c8df54eefc 100644 --- a/testing/btest/bifs/to_int.zeek +++ b/testing/btest/bifs/to_int.zeek @@ -9,6 +9,7 @@ event zeek_init() print to_int("10111100", 2); print to_int("47", 8); print to_int("F3", 16); + print to_int("0xF3", 16); print to_int("4294967296"); print to_int("not an int"); From eeaeaaeb1617c40aa535d5ccd00c4edd05bea2df Mon Sep 17 00:00:00 2001 From: Mark Overholser Date: Sun, 23 Feb 2025 16:23:31 -0600 Subject: [PATCH 4/6] fix to_count in zeek.bif to resolve conflict from bbanier's adjustment to static casting of the return from strtoull --- src/zeek.bif | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zeek.bif b/src/zeek.bif index af5dbee259..bce9efe005 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -2680,7 +2680,7 @@ function to_count%(str: string, base: count &default=10%): count const char* s = str->CheckString(); char* end_s; - uint64_t u = (uint64_t) strtoull(s, &end_s, base); + uint64_t u = static_cast strtoull(s, &end_s, base); if ( s[0] == '\0' || end_s[0] != '\0' ) { From 362f083351fc29804a859face14addc0b6856804 Mon Sep 17 00:00:00 2001 From: Mark Overholser Date: Sun, 23 Feb 2025 16:34:20 -0600 Subject: [PATCH 5/6] Update baselines after changes to to_count and to_int --- testing/btest/Baseline/bifs.to_count/out | 1 + testing/btest/Baseline/bifs.to_int/out | 1 + 2 files changed, 2 insertions(+) diff --git a/testing/btest/Baseline/bifs.to_count/out b/testing/btest/Baseline/bifs.to_count/out index da8bb62256..1cf556bf52 100644 --- a/testing/btest/Baseline/bifs.to_count/out +++ b/testing/btest/Baseline/bifs.to_count/out @@ -9,6 +9,7 @@ 172 35 195 +195 0 123 9223372036854775808 and 9223372036854775808 are the same diff --git a/testing/btest/Baseline/bifs.to_int/out b/testing/btest/Baseline/bifs.to_int/out index 8b989fe623..1ab88a9d75 100644 --- a/testing/btest/Baseline/bifs.to_int/out +++ b/testing/btest/Baseline/bifs.to_int/out @@ -4,6 +4,7 @@ 188 39 243 +243 4294967296 0 3 From 165e753439a6ee391189591162986e853b3209ed Mon Sep 17 00:00:00 2001 From: Mark Overholser Date: Mon, 24 Feb 2025 11:02:13 -0600 Subject: [PATCH 6/6] Fix incorrect syntax for static_cast in to_count that I introduced in a previous merge --- src/zeek.bif | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zeek.bif b/src/zeek.bif index bce9efe005..beb0a2241d 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -2680,7 +2680,7 @@ function to_count%(str: string, base: count &default=10%): count const char* s = str->CheckString(); char* end_s; - uint64_t u = static_cast strtoull(s, &end_s, base); + uint64_t u = static_cast (strtoull(s, &end_s, base)); if ( s[0] == '\0' || end_s[0] != '\0' ) {