am d51bfc9b: Merge "Fix the potential segmentation fault"
* commit 'd51bfc9b1fe89321af3c629e7b23a747050332e1':
Fix the potential segmentation fault
diff --git a/recovery.cpp b/recovery.cpp
index 840e63c..c82844d 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -200,6 +200,7 @@
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
@@ -207,7 +208,12 @@
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);