From 84ffdf0ed52f936b0976d8dad09bc2c309917532 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 25 Dec 2017 03:01:33 +0800 Subject: [PATCH] Eliminate a possbility to cause segfault --- core/jni/include/vector.h | 2 +- core/jni/magiskboot/dtb.c | 5 ++--- core/jni/utils/cpio.c | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/core/jni/include/vector.h b/core/jni/include/vector.h index 9156b0c6d..9984734af 100644 --- a/core/jni/include/vector.h +++ b/core/jni/include/vector.h @@ -29,7 +29,7 @@ struct vector *vec_dup(struct vector *v); for (int _ = 0; v && _ < (v)->size; ++_, e = (v)->data[_]) #define vec_for_each_r(v, e) \ - e = v ? (v)->data[(v)->size - 1] : NULL; \ + e = (v && (v)->size > 0) ? (v)->data[(v)->size - 1] : NULL; \ for (int _ = ((int) (v)->size) - 1; v && _ >= 0; --_, e = (v)->data[_]) #define vec_cur(v) vec_entry(v)[_] diff --git a/core/jni/magiskboot/dtb.c b/core/jni/magiskboot/dtb.c index 0bd57cac8..c1106d826 100644 --- a/core/jni/magiskboot/dtb.c +++ b/core/jni/magiskboot/dtb.c @@ -49,7 +49,7 @@ static void dtb_dump(const char *file) { for (int i = 0; i < size; ++i) { if (memcmp(dtb + i, DTB_MAGIC, 4) == 0) { fdt = dtb + i; - fprintf(stderr, "Dumping dtb.%04d\n\n", dtb_num++); + fprintf(stderr, "Dumping dtb.%04d\n", dtb_num++); print_subnode(fdt, 0, 0); } } @@ -61,7 +61,7 @@ static void dtb_dump(const char *file) { static void dtb_patch(const char *file, int patch) { size_t size ; void *dtb, *fdt; - fprintf(stderr, "Loading dtbs from [%s]\n\n", file); + fprintf(stderr, "Loading dtbs from [%s]\n", file); if (patch) mmap_rw(file, &dtb, &size); else @@ -84,7 +84,6 @@ static void dtb_patch(const char *file, int patch) { } } } - fprintf(stderr, "\n"); munmap(dtb, size); exit(!found); } diff --git a/core/jni/utils/cpio.c b/core/jni/utils/cpio.c index 0307d29e6..0ec3c088f 100644 --- a/core/jni/utils/cpio.c +++ b/core/jni/utils/cpio.c @@ -57,9 +57,9 @@ void cpio_vec_insert(struct vector *v, cpio_entry *n) { // Parse cpio file to a vector of cpio_entry void parse_cpio(struct vector *v, const char *filename) { - fprintf(stderr, "Loading cpio: [%s]\n", filename); int fd = open(filename, O_RDONLY); if (fd < 0) return; + fprintf(stderr, "Loading cpio: [%s]\n", filename); cpio_newc_header header; cpio_entry *f; while(xxread(fd, &header, 110) != -1) {