Commit 4f20b75c authored by Rusty Russell's avatar Rusty Russell

opt: fix opt_unregister.

Instead of memmoving N structs, we were memmoving N bytes.

But why did the test pass then?  It was doing memmove(..., 1)
instead of memmove(..., sizeof(struct opt_table)!

Because the two structures were really similar; the main difference
was the first entry, which points to the name.  But they were allocated
consecutively, and Intel being little-endian, the only difference was
the first byte!  Thus memmove(1) was enough to make it "work".

Change two options in the test to be sufficiently different, and
the bug shows up.
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent e5fb923b
......@@ -189,7 +189,8 @@ bool opt_unregister(const char *names)
if (found == -1)
return false;
opt_count--;
memmove(&opt_table[found], &opt_table[found+1], opt_count - found);
memmove(&opt_table[found], &opt_table[found+1],
(opt_count - found) * sizeof(opt_table[found]));
return true;
}
......
......@@ -10,11 +10,10 @@ int main(int argc, char *argv[])
{
const char *myname = argv[0];
plan_tests(14);
plan_tests(15);
/* Simple short arg.*/
opt_register_noarg("--aaa|-a", test_noarg, NULL, "AAAAAAll");
opt_register_noarg("-b", test_noarg, NULL, "AAAAAAll");
opt_register_arg("-b", test_arg, NULL, "bbb", "b");
/* We can't unregister wrong ones, but can unregister correct one */
ok1(!opt_unregister("--aaa"));
......@@ -22,7 +21,7 @@ int main(int argc, char *argv[])
ok1(opt_unregister("--aaa|-a"));
/* Arg parsing works as if we'd never registered it */
ok1(parse_args(&argc, &argv, "-b", NULL));
ok1(parse_args(&argc, &argv, "-bbbb", NULL));
ok1(argc == 1);
ok1(argv[0] == myname);
ok1(argv[1] == NULL);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment