diff --git a/app/src/main/cpp/archive.c b/app/src/main/cpp/archive.c index 23fcdbacd..4af150e11 100644 --- a/app/src/main/cpp/archive.c +++ b/app/src/main/cpp/archive.c @@ -52,7 +52,7 @@ pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static archive_ctx **ctx_pool = NULL; #define CTX_POOL_SIZE 20 -static void *mempool = NULL; +static void *mempool = MAP_FAILED; static size_t *mempoolofs = NULL; static int page_size = 0; @@ -66,7 +66,7 @@ static int page_size = 0; static bool need_encrypt = false; static char *passwd = NULL; -static void *archiveAddr = NULL; +static void *archiveAddr = MAP_FAILED; static size_t archiveSize = 0; static entry *entries = NULL; static size_t entryCount = 0; @@ -117,8 +117,8 @@ static inline int compare_entries(const void *a, const void *b) { return strnatcmp(fa, fb); } -static long archive_map_entries_index(archive_ctx *ctx, bool sort) { - long count = 0; +static int archive_map_entries_index(archive_ctx *ctx, bool sort) { + int count = 0; while (archive_read_next_header(ctx->arc, &ctx->entry) == ARCHIVE_OK) { const char *name = archive_entry_pathname2(ctx->entry); if (archive_entry_is_file(ctx->entry) && filename_is_playable_file(name)) { @@ -165,8 +165,8 @@ static void mempool_prefault_pages(void *addr, size_t size) { } } -static long archive_list_all_entries(archive_ctx *ctx) { - long count = 0; +static int archive_list_all_entries(archive_ctx *ctx) { + int count = 0; while (archive_read_next_header(ctx->arc, &ctx->entry) == ARCHIVE_OK) if (archive_entry_is_playable(ctx->entry)) count++; @@ -296,11 +296,12 @@ Java_com_hippo_ehviewer_jni_ArchiveKt_openArchive(JNIEnv *env, jclass thiz, jint LOGE("Allocate archive ctx pool failed:ENOMEM"); return 0; } - long r = archive_alloc_ctx(&ctx); + int r = archive_alloc_ctx(&ctx); if (r) { if (r == -ENOMEM) LOGE("%s", "Mem alloc failed"); LOGE("%s%s", "Archive open failed:", archive_error_string(ctx->arc)); + return 0; } else { entryCount = archive_list_all_entries(ctx); LOGI("%s%zu%s", "Found ", entryCount, " image entries in archive"); @@ -347,10 +348,6 @@ Java_com_hippo_ehviewer_jni_ArchiveKt_openArchive(JNIEnv *env, jclass thiz, jint r = archive_map_entries_index(ctx, sort_entries); if (!archive_prealloc_mempool()) { r = 0; - for (int i = 0; i < entryCount; ++i) { - free((void *) entries[i].filename); - } - free(entries); } } @@ -403,20 +400,32 @@ JNIEXPORT void JNICALL Java_com_hippo_ehviewer_jni_ArchiveKt_closeArchive(JNIEnv *env, jclass thiz) { EH_UNUSED(env); EH_UNUSED(thiz); - if (!ctx_pool) return; - for (int i = 0; i < CTX_POOL_SIZE; i++) - archive_release_ctx(ctx_pool[i]); + if (ctx_pool) { + for (int i = 0; i < CTX_POOL_SIZE; i++) + archive_release_ctx(ctx_pool[i]); + free(ctx_pool); + ctx_pool = NULL; + } free(passwd); - free(ctx_pool); passwd = NULL; need_encrypt = false; - munmap(archiveAddr, archiveSize); - munmap(mempool, MEMPOOL_SIZE); + if (archiveAddr != MAP_FAILED) { + munmap(archiveAddr, archiveSize); + archiveAddr = MAP_FAILED; + } + if (mempool != MAP_FAILED) { + munmap(mempool, MEMPOOL_SIZE); + mempool = MAP_FAILED; + } free(mempoolofs); - for (int i = 0; i < entryCount; ++i) { - free((void *) entries[i].filename); + mempoolofs = NULL; + if (entries) { + for (int i = 0; i < entryCount; ++i) { + free((void *) entries[i].filename); + } + free(entries); + entries = NULL; } - free(entries); } JNIEXPORT jboolean JNICALL