From 26f0727a759a7256bf553c2fd66b4ec16e0be1f3 Mon Sep 17 00:00:00 2001 From: nishad shabbir Date: Tue, 25 Aug 2026 20:30:42 +0530 Subject: [PATCH 1/2] lib: strnum - Reject empty string in str_to_float() and str_to_double() strtof() and strtod() perform no conversion for an empty string: they return 0 and leave endp pointing at str. The existing check only looked at *endp, which is NUL in that case, so str_to_float("") and str_to_double("") returned 0 and stored 0 in *num_r. Every other str_to_*() in this file rejects input that isn't a number. Check that endp moved past str as well. The only caller in tree is dict-sql's DICT_SQL_TYPE_DOUBLE, which accepted an empty field value and stored 0.0 for it. The DICT_SQL_TYPE_UINT case next to it rejects the same input, since str_to_int64() validates it. Only the empty string changes behaviour; any other input that strto[fd]() refuses already leaves a non-NUL character behind for the *endp check to catch. Introduced in e8274ded69. --- src/lib/strnum.c | 8 ++++++-- src/lib/test-strnum.c | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib/strnum.c b/src/lib/strnum.c index e97e2fc7193..9479fc8e156 100644 --- a/src/lib/strnum.c +++ b/src/lib/strnum.c @@ -460,7 +460,9 @@ int str_to_float(const char *str, float *num_r) { char *endp; float num = strtof(str, &endp); - if (*endp != '\0') + /* strtof() performs no conversion for an empty string and leaves endp + pointing at str, so the *endp check alone would accept "" as 0. */ + if (endp == str || *endp != '\0') return -1; *num_r = num; @@ -471,7 +473,9 @@ int str_to_double(const char *str, double *num_r) { char *endp; double num = strtod(str, &endp); - if (*endp != '\0') + /* strtod() performs no conversion for an empty string and leaves endp + pointing at str, so the *endp check alone would accept "" as 0. */ + if (endp == str || *endp != '\0') return -1; *num_r = num; diff --git a/src/lib/test-strnum.c b/src/lib/test-strnum.c index 5432af940d5..fd60853b982 100644 --- a/src/lib/test-strnum.c +++ b/src/lib/test-strnum.c @@ -375,6 +375,8 @@ static void test_str_to_float(void) INVALID(0.1x), INVALID(0.1.2), INVALID(bad), + /* strtof()/strtod() consume nothing here, so endp stays at str */ + { "", -1, 0 }, }; test_begin("str_to_int"); for (i = 0; i < N_ELEMENTS(tests); ++i) { From 502d2a80755a35b179cc7fd6ac4d30790aaf4318 Mon Sep 17 00:00:00 2001 From: nishad shabbir Date: Tue, 25 Aug 2026 20:30:42 +0530 Subject: [PATCH 2/2] lib-test: strnum - Fix test name in test_str_to_float() test_str_to_float() called test_begin("str_to_int"), so a failure in it was reported under the name of an unrelated test. --- src/lib/test-strnum.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/test-strnum.c b/src/lib/test-strnum.c index fd60853b982..857857fe60f 100644 --- a/src/lib/test-strnum.c +++ b/src/lib/test-strnum.c @@ -378,7 +378,7 @@ static void test_str_to_float(void) /* strtof()/strtod() consume nothing here, so endp stays at str */ { "", -1, 0 }, }; - test_begin("str_to_int"); + test_begin("str_to_float"); for (i = 0; i < N_ELEMENTS(tests); ++i) { float val = 1234.0; double val2 = 1234.0;