]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
strings.c: Improve numeric detection in `ast_strings_match()`.
authorSean Bright <sean@seanbright.com>
Wed, 15 Jan 2025 16:42:29 +0000 (11:42 -0500)
committerSean Bright <sean@seanbright.com>
Thu, 16 Jan 2025 14:20:49 +0000 (14:20 +0000)
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

main/strings.c
tests/test_strings.c

index d40eed6a6c9892449ba1ae72696ecb7968c0a6a1..f370466776b67bedf5a555e226ce83bc3b53ece2 100644 (file)
@@ -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)) {
index a12105640505d907632515cdfec1b13e5c7c4dfa..3dfe8ebf74e7d0a74a8285e9cc76b6f5811ace49 100644 (file)
@@ -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;
 }