Skip to content

Commit 827bd47

Browse files
committed
optparse: add test for issue#5731
Problem: liboptarse unit tests don't ensure that an error during registration of subcommands doesn't result in a segfault as described in issue #5731. Add a unit test that exercises this scenario.
1 parent 94f719e commit 827bd47

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/common/liboptparse/test/optparse.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,9 +1422,48 @@ static void test_optional_args ()
14221422
optparse_destroy (p);
14231423
}
14241424

1425+
static void test_issue5732 ()
1426+
{
1427+
optparse_t *p;
1428+
struct optparse_option opts [] = {
1429+
{ .name = "dup",
1430+
.key = 'd',
1431+
.has_arg = 1,
1432+
.arginfo = "S",
1433+
.usage = "test option"
1434+
},
1435+
{ .name = "dup",
1436+
.key = 'd',
1437+
.has_arg = 1,
1438+
.arginfo = "S",
1439+
.usage = "test option"
1440+
},
1441+
OPTPARSE_TABLE_END,
1442+
};
1443+
struct optparse_subcommand subcmds[] = {
1444+
{ "sub1",
1445+
"[OPTIONS]...",
1446+
"Subcommand one",
1447+
subcmd,
1448+
0,
1449+
opts,
1450+
},
1451+
OPTPARSE_SUBCMD_END
1452+
};
1453+
1454+
p = optparse_create ("issue-5732");
1455+
if (!p)
1456+
BAIL_OUT ("optparse_create");
1457+
lives_ok ({ optparse_reg_subcommands (p, subcmds); },
1458+
"optparse_reg_subcommands lives with duplicated options");
1459+
1460+
optparse_destroy (p);
1461+
}
1462+
1463+
14251464
int main (int argc, char *argv[])
14261465
{
1427-
plan (323);
1466+
plan (324);
14281467

14291468
test_convenience_accessors (); /* 60 tests */
14301469
test_usage_output (); /* 46 tests */
@@ -1442,6 +1481,7 @@ int main (int argc, char *argv[])
14421481
test_reg_subcommands (); /* 1 test */
14431482
test_optparse_get (); /* 13 tests */
14441483
test_optional_args (); /* 9 tests */
1484+
test_issue5732 (); /* 1 test */
14451485

14461486
done_testing ();
14471487
return (0);

0 commit comments

Comments
 (0)