Skip to content

Commit 69ff030

Browse files
committed
Remove CLI argument VLA
Sparse and smatch reported that fapolicyd-cli used a variable length array while building its sanitized argv copy for option parsing. The command dispatch now lives in a helper, while main allocates the sanitized argv vector with calloc, checks allocation failure, and frees it after dispatch. This preserves the existing parsing behavior without using a variable length stack object.
1 parent cdfb845 commit 69ff030

1 file changed

Lines changed: 42 additions & 21 deletions

File tree

src/cli/fapolicyd-cli.c

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,29 +1337,15 @@ static int do_test_filter(const char *path)
13371337
}
13381338
#endif
13391339

1340-
int main(int argc, char * const argv[])
1340+
/*
1341+
* run_cli_command - execute the requested CLI command.
1342+
* @arg_count: number of sanitized arguments.
1343+
* @args: sanitized argument vector.
1344+
* Returns a CLI_EXIT_* status code.
1345+
*/
1346+
static int run_cli_command(int arg_count, char **args)
13411347
{
13421348
int opt, option_index, rc = CLI_EXIT_GENERIC;
1343-
int orig_argc = argc, arg_count = 0;
1344-
char *args[orig_argc+1];
1345-
1346-
for (int i = 0; i < orig_argc; i++) {
1347-
if (strcmp(argv[i], "--verbose") == 0) {
1348-
verbose = true;
1349-
continue;
1350-
}
1351-
if (strcmp(argv[i], "--lint") == 0) {
1352-
lint_rules = true;
1353-
continue;
1354-
}
1355-
if (strcmp(argv[i], "--yes") == 0 ||
1356-
strcmp(argv[i], "-y") == 0) {
1357-
assume_yes = true;
1358-
continue;
1359-
}
1360-
args[arg_count++] = argv[i];
1361-
}
1362-
args[arg_count] = NULL;
13631349

13641350
if (arg_count == 1) {
13651351
fprintf(stderr, "Too few arguments\n\n");
@@ -1574,3 +1560,38 @@ int main(int argc, char * const argv[])
15741560
fprintf(stderr, "%s", usage);
15751561
return CLI_EXIT_USAGE;
15761562
}
1563+
1564+
int main(int argc, char * const argv[])
1565+
{
1566+
int arg_count = 0;
1567+
char **args;
1568+
int rc;
1569+
1570+
args = calloc(argc + 1, sizeof(*args));
1571+
if (args == NULL) {
1572+
fprintf(stderr, "Cannot allocate argument list\n");
1573+
return CLI_EXIT_GENERIC;
1574+
}
1575+
1576+
for (int i = 0; i < argc; i++) {
1577+
if (strcmp(argv[i], "--verbose") == 0) {
1578+
verbose = true;
1579+
continue;
1580+
}
1581+
if (strcmp(argv[i], "--lint") == 0) {
1582+
lint_rules = true;
1583+
continue;
1584+
}
1585+
if (strcmp(argv[i], "--yes") == 0 ||
1586+
strcmp(argv[i], "-y") == 0) {
1587+
assume_yes = true;
1588+
continue;
1589+
}
1590+
args[arg_count++] = argv[i];
1591+
}
1592+
args[arg_count] = NULL;
1593+
1594+
rc = run_cli_command(arg_count, args);
1595+
free(args);
1596+
return rc;
1597+
}

0 commit comments

Comments
 (0)