From 6add02702b71cceff322da0875d0f07f24924e9d Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 26 Oct 2018 17:02:07 -0400 Subject: [PATCH] Fix bug in MagiskBoot --- native/jni/magiskboot/cpio.cpp | 5 +++-- native/jni/magiskboot/cpio.h | 12 ++++++------ native/jni/magiskboot/ramdisk.cpp | 13 ++++++++++--- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/native/jni/magiskboot/cpio.cpp b/native/jni/magiskboot/cpio.cpp index 264e364f0..0b153ef6d 100644 --- a/native/jni/magiskboot/cpio.cpp +++ b/native/jni/magiskboot/cpio.cpp @@ -211,9 +211,10 @@ bool cpio::mv(const char *from, const char *to) { delete arr[t]; arr[t] = nullptr; } - free(arr[f]->filename); - arr[f]->filename = strdup(to); fprintf(stderr, "Move [%s] -> [%s]\n", from, to); + char * tmp = strdup(to); + free(arr[f]->filename); + arr[f]->filename = tmp; return true; } fprintf(stderr, "Cannot find entry %s\n", from); diff --git a/native/jni/magiskboot/cpio.h b/native/jni/magiskboot/cpio.h index 96c662b03..9a2e6472e 100644 --- a/native/jni/magiskboot/cpio.h +++ b/native/jni/magiskboot/cpio.h @@ -24,20 +24,20 @@ struct cpio_newc_header { struct cpio_entry { // uint32_t ino; - uint32_t mode; - uint32_t uid; - uint32_t gid; + uint32_t mode = 0; + uint32_t uid = 0; + uint32_t gid = 0; // uint32_t nlink; // uint32_t mtime; - uint32_t filesize; + uint32_t filesize = 0; // uint32_t devmajor; // uint32_t devminor; // uint32_t rdevmajor; // uint32_t rdevminor; // uint32_t namesize; // uint32_t check; - char *filename; - void *data; + char *filename = nullptr; + void *data = nullptr; cpio_entry() {} cpio_entry(int fd, cpio_newc_header &header); diff --git a/native/jni/magiskboot/ramdisk.cpp b/native/jni/magiskboot/ramdisk.cpp index 7c08a47ff..40218d30c 100644 --- a/native/jni/magiskboot/ramdisk.cpp +++ b/native/jni/magiskboot/ramdisk.cpp @@ -208,10 +208,17 @@ int cpio_commands(int argc, char *argv[]) { char *cmdv[6]; while (argc) { + // Clean up cmdc = 0; - for (char *tok = strtok(argv[0], " "); tok; tok = strtok(NULL, " ")) + memset(cmdv, NULL, sizeof(cmdv)); + + // Split the commands + for (char *tok = strtok(argv[0], " "); tok; tok = strtok(nullptr, " ")) cmdv[cmdc++] = tok; + if (cmdc == 0) + continue; + if (strcmp(cmdv[0], "test") == 0) { exit(cpio.test()); } else if (strcmp(cmdv[0], "restore") == 0) { @@ -224,13 +231,13 @@ int cpio_commands(int argc, char *argv[]) { return 0; } else if (cmdc >= 2 && strcmp(cmdv[0], "backup") == 0) { auto bak = Array(); - cpio.backup(bak, cmdv[1], cmdc > 2 ? cmdv[2] : nullptr); + cpio.backup(bak, cmdv[1], cmdv[2]); cpio.insert(bak); } else if (cmdc >= 4 && strcmp(cmdv[0], "magisk") == 0) { cpio.patch(strcmp(cmdv[2], "true") == 0, strcmp(cmdv[3], "true") == 0); auto bak = Array(); - cpio.backup(bak, cmdv[1], cmdc > 4 ? cmdv[4] : nullptr); + cpio.backup(bak, cmdv[1], cmdv[4]); auto e = new cpio_entry(); e->filename = strdup(".backup/.magisk");