aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2010-02-02 18:54:33 +0000
committertilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2010-02-02 18:54:33 +0000
commit533721dbc3f6c9fd3dfbd60cc2d9db946a95e94b (patch)
tree333cc0b61225b5abf4c785c4103a6d70ec179884
parent2a5789193e0d2200c6bb8f1d9537429be5600ec4 (diff)
Correct some off-by-one errors, especially when expressions don't contain expected spaces.
Also include the tests provided by the reporter, as regression tests. (closes issue #16667) Reported by: wdoekes Patches: astsvn-func_match-off-by-one.diff uploaded by wdoekes (license 717) git-svn-id: http://svn.digium.com/svn/asterisk/trunk@244331 f38db490-d61c-443f-a65b-d21fe96a405b
-rw-r--r--funcs/func_math.c70
1 files changed, 61 insertions, 9 deletions
diff --git a/funcs/func_math.c b/funcs/func_math.c
index 761c63228..2164ff261 100644
--- a/funcs/func_math.c
+++ b/funcs/func_math.c
@@ -40,6 +40,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/config.h"
+#include "asterisk/test.h"
/*** DOCUMENTATION
<function name="MATH" language="en_US">
@@ -179,47 +180,47 @@ static int math(struct ast_channel *chan, const char *cmd, char *parse,
*op = '\0';
} else if ((op = strstr(mvalue1, "AND"))) {
iaction = BITWISEANDFUNCTION;
- op += 3;
*op = '\0';
+ op += 2;
} else if ((op = strstr(mvalue1, "XOR"))) {
iaction = BITWISEXORFUNCTION;
- op += 3;
*op = '\0';
+ op += 2;
} else if ((op = strstr(mvalue1, "OR"))) {
iaction = BITWISEORFUNCTION;
- op += 2;
*op = '\0';
+ ++op;
} else if ((op = strchr(mvalue1, '>'))) {
iaction = GTFUNCTION;
*op = '\0';
if (*(op + 1) == '=') {
- *++op = '\0';
iaction = GTEFUNCTION;
+ ++op;
} else if (*(op + 1) == '>') {
- *++op = '\0';
iaction = SHRIGHTFUNCTION;
+ ++op;
}
} else if ((op = strchr(mvalue1, '<'))) {
iaction = LTFUNCTION;
*op = '\0';
if (*(op + 1) == '=') {
- *++op = '\0';
iaction = LTEFUNCTION;
+ ++op;
} else if (*(op + 1) == '<') {
- *++op = '\0';
iaction = SHLEFTFUNCTION;
+ ++op;
}
} else if ((op = strchr(mvalue1, '='))) {
*op = '\0';
if (*(op + 1) == '=') {
- *++op = '\0';
iaction = EQFUNCTION;
+ ++op;
} else
op = NULL;
} else if ((op = strchr(mvalue1, '+'))) {
iaction = ADDFUNCTION;
*op = '\0';
- } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative first number */
+ } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative second number */
iaction = SUBTRACTFUNCTION;
*op = '\0';
}
@@ -454,6 +455,55 @@ static struct ast_custom_function decrement_function = {
.read = crement_function_read,
};
+#ifdef TEST_FRAMEWORK
+AST_TEST_DEFINE(test_MATH_function)
+{
+ enum ast_test_result_state res = AST_TEST_PASS;
+ struct ast_str *expr, *result;
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "test_MATH_function";
+ info->category = "main/pbx/";
+ info->summary = "Test MATH function substitution";
+ info->description =
+ "Executes a series of variable substitutions using the MATH function and ensures that the expected results are received.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ ast_test_status_update(&args->status_update, "Testing MATH() substitution ...\n");
+ ast_str_reset(args->ast_test_error_str);
+
+ if (!(expr = ast_str_create(16)) || !(result = ast_str_create(16))) {
+ if (expr) {
+ ast_free(expr);
+ }
+ if (result) {
+ ast_free(result);
+ }
+ return AST_TEST_FAIL;
+ }
+
+ ast_str_set(&expr, 0, "${MATH(170 AND 63,i)}");
+ ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
+ if (strcmp(ast_str_buffer(result), "42") != 0) {
+ ast_str_append(&args->ast_test_error_str, 0, "Expected result '42' not returned! ('%s')\n", ast_str_buffer(result));
+ res = AST_TEST_FAIL;
+ }
+
+ ast_str_set(&expr, 0, "${MATH(170AND63,i)}");
+ ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
+ if (strcmp(ast_str_buffer(result), "42") != 0) {
+ ast_str_append(&args->ast_test_error_str, 0, "Expected result '42' not returned! ('%s')\n", ast_str_buffer(result));
+ res = AST_TEST_FAIL;
+ }
+
+ return res;
+}
+#endif
+
static int unload_module(void)
{
int res = 0;
@@ -461,6 +511,7 @@ static int unload_module(void)
res |= ast_custom_function_unregister(&math_function);
res |= ast_custom_function_unregister(&increment_function);
res |= ast_custom_function_unregister(&decrement_function);
+ AST_TEST_UNREGISTER(test_MATH_function);
return res;
}
@@ -472,6 +523,7 @@ static int load_module(void)
res |= ast_custom_function_register(&math_function);
res |= ast_custom_function_register(&increment_function);
res |= ast_custom_function_register(&decrement_function);
+ AST_TEST_REGISTER(test_MATH_function);
return res;
}