diff options
author | Richard Mudgett <rmudgett@digium.com> | 2015-07-15 15:40:32 -0500 |
---|---|---|
committer | Richard Mudgett <rmudgett@digium.com> | 2015-07-15 19:56:32 -0500 |
commit | e31cb6b2484bbf5726c59b263f13b995e60d537d (patch) | |
tree | d5c264e0342e37d3633147785f476b8a8fcaa779 /tests | |
parent | 243c0d1609d193130f24d4586070036fdb900f0f (diff) |
strings.h: Fix issues with escape string functions.
Fixes for issues with the ASTERISK-24934 patch.
* Fixed ast_escape_alloc() and ast_escape_c_alloc() if the s parameter is
an empty string. If it were an empty string the functions returned NULL
as if there were a memory allocation failure. This failure caused the AMI
VarSet event to not get posted if the new value was an empty string.
* Fixed dest buffer overwrite potential in ast_escape() and
ast_escape_c(). If the dest buffer size is smaller than the space needed
by the escaped s parameter string then the dest buffer would be written
beyond the end by the nul string terminator. The num parameter was really
the dest buffer size parameter so I renamed it to size.
* Made nul terminate the dest buffer if the source string parameter s was
an empty string in ast_escape() and ast_escape_c().
* Updated ast_escape() and ast_escape_c() doxygen function description
comments to reflect reality.
* Added some more unit test cases to /main/strings/escape to cover the
empty source string issues.
ASTERISK-25255 #close
Reported by: Richard Mudgett
Change-Id: Id77fc704600ebcce81615c1200296f74de254104
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_strings.c | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/tests/test_strings.c b/tests/test_strings.c index ab65037ee..5e3446d99 100644 --- a/tests/test_strings.c +++ b/tests/test_strings.c @@ -460,7 +460,32 @@ AST_TEST_DEFINE(escape_test) char buf[128]; #define TEST_ESCAPE(s, to_escape, expected) \ - !strcmp(ast_escape(buf, s, sizeof(buf) / sizeof(char), to_escape), expected) + !strcmp(ast_escape(buf, s, ARRAY_LEN(buf), to_escape), expected) + +#define TEST_ESCAPE_C(s, expected) \ + !strcmp(ast_escape_c(buf, s, ARRAY_LEN(buf)), expected) + +#define TEST_ESCAPE_ALLOC(s, to_escape, expected) \ + ({ \ + int res = 0; \ + char *a_buf = ast_escape_alloc(s, to_escape); \ + if (a_buf) { \ + res = !strcmp(a_buf, expected); \ + ast_free(a_buf); \ + } \ + res; \ + }) + +#define TEST_ESCAPE_C_ALLOC(s, expected) \ + ({ \ + int res = 0; \ + char *a_buf = ast_escape_c_alloc(s); \ + if (a_buf) { \ + res = !strcmp(a_buf, expected); \ + ast_free(a_buf); \ + } \ + res; \ + }) switch (cmd) { case TEST_INIT: @@ -474,16 +499,27 @@ AST_TEST_DEFINE(escape_test) } ast_test_validate(test, TEST_ESCAPE("null escape", NULL, "null escape")); + ast_test_validate(test, TEST_ESCAPE("empty escape", "", "empty escape")); + ast_test_validate(test, TEST_ESCAPE("", "Z", "")); ast_test_validate(test, TEST_ESCAPE("no matching escape", "Z", "no matching escape")); ast_test_validate(test, TEST_ESCAPE("escape Z", "Z", "escape \\Z")); ast_test_validate(test, TEST_ESCAPE("Z", "Z", "\\Z")); - ast_test_validate(test, TEST_ESCAPE(";;", ";;", "\\;\\;")); + ast_test_validate(test, TEST_ESCAPE(";;", ";", "\\;\\;")); ast_test_validate(test, TEST_ESCAPE("escape \n", "\n", "escape \\n")); ast_test_validate(test, TEST_ESCAPE("escape \n again \n", "\n", "escape \\n again \\n")); - ast_test_validate(test, !strcmp(ast_escape_c(buf, "escape \a\b\f\n\r\t\v\\\'\"\?", - sizeof(buf) / sizeof(char)), - "escape \\a\\b\\f\\n\\r\\t\\v\\\\\\\'\\\"\\?")); + ast_test_validate(test, TEST_ESCAPE_C("", "")); + ast_test_validate(test, TEST_ESCAPE_C("escape \a\b\f\n\r\t\v\\\'\"\?", + "escape \\a\\b\\f\\n\\r\\t\\v\\\\\\\'\\\"\\?")); + + ast_test_validate(test, TEST_ESCAPE_ALLOC("", "Z", "")); + ast_test_validate(test, TEST_ESCAPE_ALLOC("Z", "Z", "\\Z")); + ast_test_validate(test, TEST_ESCAPE_ALLOC("a", "Z", "a")); + + ast_test_validate(test, TEST_ESCAPE_C_ALLOC("", "")); + ast_test_validate(test, TEST_ESCAPE_C_ALLOC("\n", "\\n")); + ast_test_validate(test, TEST_ESCAPE_C_ALLOC("a", "a")); + return AST_TEST_PASS; } |