summaryrefslogtreecommitdiffstats
path: root/src/tests/util-tests.c
diff options
context:
space:
mode:
authorLukas Slebodnik <lslebodn@redhat.com>2014-06-20 17:04:59 +0200
committerJakub Hrozek <jhrozek@redhat.com>2014-07-08 09:53:02 +0200
commit852722ecb5dc09fc80cd3c837edb1cf6db529210 (patch)
tree2501d5f8442f9db69e6f572225b27e8133750547 /src/tests/util-tests.c
parentbe8e3c2eb7619ae1b94fd8941e0f18c8969b2804 (diff)
downloadsssd-852722ecb5dc09fc80cd3c837edb1cf6db529210.tar.gz
sssd-852722ecb5dc09fc80cd3c837edb1cf6db529210.tar.xz
sssd-852722ecb5dc09fc80cd3c837edb1cf6db529210.zip
UTIL: Fix access out of bound in parse_args
While parsing string with multiple whitespaces, it may happen variable i is zero and we want to test end of argument "tmp[i-1] != '\0'". Side effect of this bug is duplicite string output array. Input string: "foo b" Expected output: { "foo", "a", NULL } Output: { "foo", "foo", "a", NULL } This patch uses inverted logic. Instead of testing whether to read next char or skip multiple whitespaces, we will test whether we have new argument which should be stored in output array. Reviewed-by: Pavel Reichl <preichl@redhat.com> Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
Diffstat (limited to 'src/tests/util-tests.c')
-rw-r--r--src/tests/util-tests.c30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/tests/util-tests.c b/src/tests/util-tests.c
index 198edf597..b79153e3e 100644
--- a/src/tests/util-tests.c
+++ b/src/tests/util-tests.c
@@ -141,6 +141,9 @@ START_TEST(test_parse_args)
const char *parsed6[] = { "foo", "a", "e", NULL };
const char *parsed7[] = { "foo", "a", "f", NULL };
const char *parsed8[] = { "foo", "a\tg", NULL };
+ const char *parsed9[] = { "foo", NULL };
+ const char *parsed10[] = { " ", "foo", "\t", "\\'", NULL };
+ const char *parsed11[] = { "a", NULL };
struct pa_testcase tc[] = {
{ "foo", parsed1 },
{ "foo a", parsed2 },
@@ -148,15 +151,22 @@ START_TEST(test_parse_args)
{ "foo a\\ c", parsed4 },
{ "foo a d ", parsed5 },
{ "foo a e ", parsed6 },
- { "foo a f ", parsed7 },
+ { "foo\ta\t \tf \t", parsed7 },
{ "foo a\\\tg", parsed8 },
+ { " foo ", parsed9 },
+ { "\\ foo \\\t \\' ", parsed10 },
+ { "a", parsed11 },
+ { " ", NULL },
+ { "", NULL },
+ { " \t ", NULL },
{ NULL, NULL }
};
for (i=0; tc[i].argstr != NULL; i++) {
parsed = parse_args(tc[i].argstr);
fail_if(parsed == NULL && tc[i].parsed != NULL,
- "Could not parse correct argument string '%s'\n");
+ "Could not parse correct %d argument string '%s'\n",
+ i, tc[i].argstr);
ret = diff_string_lists(test_ctx, parsed, discard_const(tc[i].parsed),
&only_ret, &only_exp, &both);
@@ -164,8 +174,20 @@ START_TEST(test_parse_args)
fail_unless(only_ret[0] == NULL, "The parser returned more data than expected\n");
fail_unless(only_exp[0] == NULL, "The parser returned less data than expected\n");
- for (ii = 0; parsed[ii]; ii++) free(parsed[ii]);
- free(parsed);
+ if (parsed) {
+ int parsed_len;
+ int expected_len;
+
+ for (parsed_len=0; parsed[parsed_len]; ++parsed_len);
+ for (expected_len=0; tc[i].parsed[expected_len]; ++expected_len);
+
+ fail_unless(parsed_len == expected_len,
+ "Test %d: length of 1st array [%d] != length of 2nd "
+ "array[%d]\n", i, parsed_len, expected_len);
+
+ for (ii = 0; parsed[ii]; ii++) free(parsed[ii]);
+ free(parsed);
+ }
}
talloc_free(test_ctx);