From: Fletcher T. Penney Date: Fri, 7 Sep 2018 00:33:06 +0000 (-0400) Subject: FIXED: Add byte for null terminator when extracting from zip archive X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=911b42c757ebc9b4f15c270fccf0dcf7ee0deddd;p=multimarkdown FIXED: Add byte for null terminator when extracting from zip archive --- diff --git a/Sources/libMultiMarkdown/zip.c b/Sources/libMultiMarkdown/zip.c index ed1948f..e8f43cb 100644 --- a/Sources/libMultiMarkdown/zip.c +++ b/Sources/libMultiMarkdown/zip.c @@ -212,20 +212,35 @@ mz_bool unzip_data_to_path(const void * data, size_t size, const char * path) { // Extract single file from archive mz_bool unzip_file_from_archive(mz_zip_archive * pZip, const char * filename, DString * destination) { - free(destination->str); - - destination->str = mz_zip_reader_extract_file_to_heap(pZip, filename, &destination->currentStringLength, 0); + mz_uint32 index; + mz_zip_archive_file_stat pStat; + mz_bool status; - if (destination->str == NULL) { - fprintf(stderr, "mz_zip_reader_extract_file_to_mem() failed.\n"); + int result = mz_zip_reader_locate_file_v2(pZip, filename, NULL, 0, &index); - // return an error + if (result == -1) { + // Failed return 0; - } else { - destination->currentStringBufferSize = destination->currentStringLength; } - return 1; + mz_zip_reader_file_stat(pZip, index, &pStat); + size_t size = pStat.m_uncomp_size + 1; // Allow for null terminator in case this is text + + if (destination->currentStringBufferSize < size) { + // Buffer to small + free(destination->str); + destination->str = malloc(size); + destination->currentStringBufferSize = size; + destination->currentStringLength = size - 1; + } + + status = mz_zip_reader_extract_to_mem(pZip, index, destination->str, destination->currentStringBufferSize, 0); + + if (!status) { + fprintf(stderr, "mz_zip_reader_extract_to_mem() failed.\n"); + } + + return status; }