From: Sean Bright Date: Wed, 15 Jan 2025 16:42:29 +0000 (-0500) Subject: strings.c: Improve numeric detection in `ast_strings_match()`. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=813b774cc1f917f0bf21b80269270e484018b0c0;p=thirdparty%2Fasterisk.git strings.c: Improve numeric detection in `ast_strings_match()`. Essentially, we were treating 1234x1234 and 1234x5678 as 'equal' because we were able to convert the prefix of each of these strings to the same number. Resolves: #1028 --- diff --git a/main/strings.c b/main/strings.c index d40eed6a6c..f370466776 100644 --- a/main/strings.c +++ b/main/strings.c @@ -244,6 +244,19 @@ int ast_strings_equal(const char *str1, const char *str2) return str1 == str2 || !strcmp(str1, str2); } +static int parse_double(const char *input, double *result) +{ + char *endptr; + + errno = 0; + *result = strtod(input, &endptr); + if (*endptr || errno == ERANGE) { + return 0; + } + + return 1; +} + int ast_strings_match(const char *left, const char *op, const char *right) { char *internal_op = (char *)op; @@ -311,7 +324,8 @@ regex: } equals: - scan_numeric = (sscanf(left, "%lf", &left_num) > 0 && sscanf(internal_right, "%lf", &right_num) > 0); + scan_numeric = parse_double(left, &left_num) + && parse_double(internal_right, &right_num); if (internal_op[0] == '=') { if (ast_strlen_zero(left) && ast_strlen_zero(internal_right)) { diff --git a/tests/test_strings.c b/tests/test_strings.c index a121056405..3dfe8ebf74 100644 --- a/tests/test_strings.c +++ b/tests/test_strings.c @@ -717,6 +717,9 @@ AST_TEST_DEFINE(strings_match) ast_test_validate(test, !ast_strings_match(NULL, NULL, "abc")); ast_test_validate(test, !ast_strings_match(NULL, NULL, NULL)); + /* See https://github.com/asterisk/asterisk/issues/1028 */ + ast_test_validate(test, !ast_strings_match("123456789c1", NULL, "123456789c2")); + return AST_TEST_PASS; }