From 4b5f67c0baeea1fbac94b8ce200307b35015be33 Mon Sep 17 00:00:00 2001 From: anth64 Date: Tue, 15 Sep 2026 10:52:03 +0200 Subject: [PATCH] fix: avoid failing open on empty archives (malloc(0) returns NULL) --- src/fpk.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/fpk.c b/src/fpk.c index 683e74d..8ee9ef1 100644 --- a/src/fpk.c +++ b/src/fpk.c @@ -113,18 +113,23 @@ struct fpk *fpk_open(const char *path) goto fail; strtab_size = dir_offset - strtab_offset; - fpk->strtab = malloc((size_t)strtab_size); - if (fpk->strtab == NULL) - goto fail; - if (fpk_fseek64(fpk->file, strtab_offset) != 0) - goto fail; - if (fread(fpk->strtab, 1, (size_t)strtab_size, fpk->file) != - strtab_size) - goto fail; + if (strtab_size > 0) { + fpk->strtab = malloc((size_t)strtab_size); + if (fpk->strtab == NULL) + goto fail; + if (fpk_fseek64(fpk->file, strtab_offset) != 0) + goto fail; + if (fread(fpk->strtab, 1, (size_t)strtab_size, fpk->file) != + strtab_size) + goto fail; + } - fpk->entries = malloc((size_t)fpk->entry_count * sizeof(fpk_entry_t)); - if (fpk->entries == NULL) - goto fail; + if (fpk->entry_count > 0) { + fpk->entries = malloc( + (size_t)fpk->entry_count * sizeof(fpk_entry_t)); + if (fpk->entries == NULL) + goto fail; + } if (fpk_fseek64(fpk->file, dir_offset) != 0) goto fail; for (i = 0; i < fpk->entry_count; i++) {