diff --git a/src/cpulimit.c b/src/cpulimit.c index d034120..e66a2ba 100644 --- a/src/cpulimit.c +++ b/src/cpulimit.c @@ -506,19 +506,15 @@ int main(int argc, char *argv[]) /* Executable file */ const char *cmd = argv[optind]; /* Command line arguments */ - char **cmd_args = (char **)malloc((size_t)(argc - optind + 1) * sizeof(char *)); + char **cmd_args = (char **)calloc((size_t)(argc - optind + 1), sizeof(char *)); if (cmd_args == NULL) { fprintf(stderr, "Memory allocation failed for cmd_args\n"); exit(EXIT_FAILURE); } - /* Prepare command arguments */ - for (i = 0; i < argc - optind; i++) - { - cmd_args[i] = argv[i + optind]; - } - cmd_args[i] = NULL; + /* Prepare command arguments for execvp */ + memcpy(cmd_args, argv + optind, (size_t)(argc - optind) * sizeof(char *)); /* If verbose, print the command being executed */ if (verbose)