From ee9c98b488ab21ecb1a6e9e10dbbd16054865b58 Mon Sep 17 00:00:00 2001 From: simon987 Date: Mon, 7 Jun 2021 09:01:06 -0400 Subject: [PATCH] Fix for segfault in some comic files --- libscan/comic/comic.c | 8 ++++++-- test/main.cpp | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/libscan/comic/comic.c b/libscan/comic/comic.c index 45bbec6..6ae1714 100644 --- a/libscan/comic/comic.c +++ b/libscan/comic/comic.c @@ -12,6 +12,10 @@ void parse_comic(scan_comic_ctx_t *ctx, vfile_t *f, document_t *doc) { struct archive_entry *entry = NULL; arc_data_t arc_data; + if (ctx->tn_size <= 0) { + return; + } + int ret = arc_open(&arc_ctx, f, &a, &arc_data, TRUE); if (ret != ARCHIVE_OK) { CTX_LOG_ERRORF(f->filepath, "(cbr.c) [%d] %s", ret, archive_error_string(a)) @@ -26,10 +30,10 @@ void parse_comic(scan_comic_ctx_t *ctx, vfile_t *f, document_t *doc) { const char *file_path = utf8_name == NULL ? archive_entry_pathname(entry) : utf8_name; char *p = strrchr(file_path, '.'); - if (p != NULL && strcmp(p, ".png") == 0 || strcmp(p, ".jpg") == 0 || strcmp(p, ".jpeg") == 0) { + if (p != NULL && (strcmp(p, ".png") == 0 || strcmp(p, ".jpg") == 0 || strcmp(p, ".jpeg") == 0)) { size_t entry_size = archive_entry_size(entry); void *buf = malloc(entry_size); - int read = archive_read_data(a, buf, entry_size); + size_t read = archive_read_data(a, buf, entry_size); if (read != entry_size) { const char *err_str = archive_error_string(a); diff --git a/test/main.cpp b/test/main.cpp index f6dae1e..4edf776 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -291,6 +291,19 @@ TEST(Comic, ComicCbr) { cleanup(&doc, &f); } +TEST(Comic, ComicIssue160) { + vfile_t f; + document_t doc; + load_doc_file("libscan-test-files/test_files/ebook/comic-segfault-issue-160.cbr", &f, &doc); + + int tn_size_saved = comic_ctx.tn_size; + comic_ctx.tn_size = 0; + parse_comic(&comic_ctx, &f, &doc); + comic_ctx.tn_size = tn_size_saved; + + cleanup(&doc, &f); +} + TEST(Comic, ComicCbrAsIs) { vfile_t f; document_t doc;