Merge "Fix the potential segmentation fault"
This commit is contained in:
commit
d51bfc9b1f
1 changed files with 7 additions and 1 deletions
|
@ -198,6 +198,7 @@ get_args(int *argc, char ***argv) {
|
|||
if (*argc <= 1) {
|
||||
FILE *fp = fopen_path(COMMAND_FILE, "r");
|
||||
if (fp != NULL) {
|
||||
char *token;
|
||||
char *argv0 = (*argv)[0];
|
||||
*argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
|
||||
(*argv)[0] = argv0; // use the same program name
|
||||
|
@ -205,7 +206,12 @@ get_args(int *argc, char ***argv) {
|
|||
char buf[MAX_ARG_LENGTH];
|
||||
for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
|
||||
if (!fgets(buf, sizeof(buf), fp)) break;
|
||||
(*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
|
||||
token = strtok(buf, "\r\n");
|
||||
if (token != NULL) {
|
||||
(*argv)[*argc] = strdup(token); // Strip newline.
|
||||
} else {
|
||||
--*argc;
|
||||
}
|
||||
}
|
||||
|
||||
check_and_fclose(fp, COMMAND_FILE);
|
||||
|
|
Loading…
Reference in a new issue