From a4783a755e553af62d7abe82b30a5ce5cc92f166 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 5 Jun 2022 03:01:10 +0200 Subject: [PATCH] Fix memory leak Access checksum objects directly vs pointer reference. Structure size equals a 64 bit pointer (fits into a single register). Allocated memory was never deallocated (my mistake). --- r5dev/thirdparty/lzham/include/lzham.h | 8 +++---- r5dev/thirdparty/lzham/lzham_api.cpp | 4 ++-- r5dev/thirdparty/lzham/lzhamcomp/lzham_comp.h | 2 +- .../lzham/lzhamcomp/lzham_lzcomp.cpp | 19 +++++++-------- .../lzham/lzhamdecomp/lzham_decomp.h | 2 +- .../lzham/lzhamdecomp/lzham_lzdecomp.cpp | 23 +++++++++---------- 6 files changed, 27 insertions(+), 31 deletions(-) diff --git a/r5dev/thirdparty/lzham/include/lzham.h b/r5dev/thirdparty/lzham/include/lzham.h index 526c0ab7..0e04dfc7 100644 --- a/r5dev/thirdparty/lzham/include/lzham.h +++ b/r5dev/thirdparty/lzham/include/lzham.h @@ -161,7 +161,7 @@ extern "C" { // Deinitializes a compressor, releasing all allocated memory. // returns adler32 and crc32 of source data (valid only on success). - LZHAM_DLL_EXPORT lzham_compress_checksums* LZHAM_CDECL lzham_compress_deinit(lzham_compress_state_ptr pState); + LZHAM_DLL_EXPORT lzham_compress_checksums LZHAM_CDECL lzham_compress_deinit(lzham_compress_state_ptr pState); // Compresses an arbitrarily sized block of data, writing as much available compressed data as possible to the output buffer. // This method may be called as many times as needed, but for best perf. try not to call it with tiny buffers. @@ -282,7 +282,7 @@ extern "C" { // Deinitializes a decompressor. // returns adler32 of decompressed data if compute_adler32 was true, otherwise it returns the adler32 from the compressed stream. - LZHAM_DLL_EXPORT lzham_decompress_checksums* LZHAM_CDECL lzham_decompress_deinit(lzham_decompress_state_ptr pState); + LZHAM_DLL_EXPORT lzham_decompress_checksums LZHAM_CDECL lzham_decompress_deinit(lzham_decompress_state_ptr pState); // Decompresses an arbitrarily sized block of compressed data, writing as much available decompressed data as possible to the output buffer. // This method is implemented as a coroutine so it may be called as many times as needed. However, for best perf. try not to call it with tiny buffers. @@ -618,14 +618,14 @@ extern "C" { typedef lzham_compress_state_ptr (LZHAM_CDECL *lzham_compress_init_func)(const lzham_compress_params *pParams); typedef lzham_compress_state_ptr (LZHAM_CDECL *lzham_compress_reinit_func)(lzham_compress_state_ptr pState); - typedef lzham_compress_checksums* (LZHAM_CDECL *lzham_compress_deinit_func)(lzham_compress_state_ptr pState); + typedef lzham_compress_checksums (LZHAM_CDECL *lzham_compress_deinit_func)(lzham_compress_state_ptr pState); typedef lzham_compress_status_t (LZHAM_CDECL *lzham_compress_func)(lzham_compress_state_ptr pState, const lzham_uint8 *pIn_buf, size_t *pIn_buf_size, lzham_uint8 *pOut_buf, size_t *pOut_buf_size, lzham_bool no_more_input_bytes_flag); typedef lzham_compress_status_t (LZHAM_CDECL *lzham_compress2_func)(lzham_compress_state_ptr pState, const lzham_uint8 *pIn_buf, size_t *pIn_buf_size, lzham_uint8 *pOut_buf, size_t *pOut_buf_size, lzham_flush_t flush_type); typedef lzham_compress_status_t (LZHAM_CDECL *lzham_compress_memory_func)(const lzham_compress_params *pParams, lzham_uint8* pDst_buf, size_t *pDst_len, const lzham_uint8* pSrc_buf, size_t src_len, lzham_uint32 *pAdler32, lzham_uint32* pCrc32); typedef lzham_decompress_state_ptr (LZHAM_CDECL *lzham_decompress_init_func)(const lzham_decompress_params *pParams); typedef lzham_decompress_state_ptr (LZHAM_CDECL *lzham_decompress_reinit_func)(lzham_compress_state_ptr pState, const lzham_decompress_params *pParams); - typedef lzham_decompress_checksums* (LZHAM_CDECL *lzham_decompress_deinit_func)(lzham_decompress_state_ptr pState); + typedef lzham_decompress_checksums (LZHAM_CDECL *lzham_decompress_deinit_func)(lzham_decompress_state_ptr pState); typedef lzham_decompress_status_t (LZHAM_CDECL *lzham_decompress_func)(lzham_decompress_state_ptr pState, const lzham_uint8 *pIn_buf, size_t *pIn_buf_size, lzham_uint8 *pOut_buf, size_t *pOut_buf_size, lzham_bool no_more_input_bytes_flag); typedef lzham_decompress_status_t (LZHAM_CDECL *lzham_decompress_memory_func)(const lzham_decompress_params *pParams, lzham_uint8* pDst_buf, size_t *pDst_len, const lzham_uint8* pSrc_buf, size_t src_len, lzham_uint32 *pAdler32, lzham_uint32 *pCrc32); diff --git a/r5dev/thirdparty/lzham/lzham_api.cpp b/r5dev/thirdparty/lzham/lzham_api.cpp index c622b6c5..9672d735 100644 --- a/r5dev/thirdparty/lzham/lzham_api.cpp +++ b/r5dev/thirdparty/lzham/lzham_api.cpp @@ -24,7 +24,7 @@ extern "C" LZHAM_DLL_EXPORT lzham_decompress_state_ptr lzham_decompress_reinit(l return lzham::lzham_lib_decompress_reinit(p, pParams); } -extern "C" LZHAM_DLL_EXPORT lzham_decompress_checksums* lzham_decompress_deinit(lzham_decompress_state_ptr p) +extern "C" LZHAM_DLL_EXPORT lzham_decompress_checksums lzham_decompress_deinit(lzham_decompress_state_ptr p) { return lzham::lzham_lib_decompress_deinit(p); } @@ -53,7 +53,7 @@ extern "C" LZHAM_DLL_EXPORT lzham_compress_state_ptr lzham_compress_reinit(lzham return lzham::lzham_lib_compress_reinit(p); } -extern "C" LZHAM_DLL_EXPORT lzham_compress_checksums* lzham_compress_deinit(lzham_compress_state_ptr p) +extern "C" LZHAM_DLL_EXPORT lzham_compress_checksums lzham_compress_deinit(lzham_compress_state_ptr p) { return lzham::lzham_lib_compress_deinit(p); } diff --git a/r5dev/thirdparty/lzham/lzhamcomp/lzham_comp.h b/r5dev/thirdparty/lzham/lzhamcomp/lzham_comp.h index 05827470..68dd1e72 100644 --- a/r5dev/thirdparty/lzham/lzhamcomp/lzham_comp.h +++ b/r5dev/thirdparty/lzham/lzhamcomp/lzham_comp.h @@ -9,7 +9,7 @@ namespace lzham lzham_compress_state_ptr LZHAM_CDECL lzham_lib_compress_reinit(lzham_compress_state_ptr p); - lzham_compress_checksums* LZHAM_CDECL lzham_lib_compress_deinit(lzham_compress_state_ptr p); + lzham_compress_checksums LZHAM_CDECL lzham_lib_compress_deinit(lzham_compress_state_ptr p); lzham_compress_status_t LZHAM_CDECL lzham_lib_compress( lzham_compress_state_ptr p, diff --git a/r5dev/thirdparty/lzham/lzhamcomp/lzham_lzcomp.cpp b/r5dev/thirdparty/lzham/lzhamcomp/lzham_lzcomp.cpp index 55a86de7..f8eab13b 100644 --- a/r5dev/thirdparty/lzham/lzhamcomp/lzham_lzcomp.cpp +++ b/r5dev/thirdparty/lzham/lzhamcomp/lzham_lzcomp.cpp @@ -150,18 +150,15 @@ namespace lzham return pState; } - lzham_compress_checksums* LZHAM_CDECL lzham_lib_compress_deinit(lzham_compress_state_ptr p) + lzham_compress_checksums LZHAM_CDECL lzham_lib_compress_deinit(lzham_compress_state_ptr p) { + lzham_compress_checksums checksums{}; lzham_compress_state *pState = static_cast(p); if (!pState) - return nullptr; + return checksums; - lzham_compress_checksums* checksums = new lzham_compress_checksums(); - checksums->adler32 = pState->m_compressor.get_src_adler32(); - checksums->crc32 = pState->m_compressor.get_src_crc32(); - - printf("checksums->adler32 %zX\n", checksums->adler32); - printf("checksums->crc32 %zX\n", checksums->crc32); + checksums.adler32 = pState->m_compressor.get_src_adler32(); + checksums.crc32 = pState->m_compressor.get_src_crc32(); lzham_delete(pState); return checksums; @@ -558,10 +555,10 @@ namespace lzham lzham_compress_state_ptr pComp = (lzham_compress_state_ptr)pStream->state; if (pComp) { - lzham_compress_checksums* checksums = lzham_lib_compress_deinit(pComp); + lzham_compress_checksums checksums = lzham_lib_compress_deinit(pComp); - pStream->adler32 = checksums->adler32; - pStream->crc32 = checksums->crc32; + pStream->adler32 = checksums.adler32; + pStream->crc32 = checksums.crc32; pStream->state = NULL; } diff --git a/r5dev/thirdparty/lzham/lzhamdecomp/lzham_decomp.h b/r5dev/thirdparty/lzham/lzhamdecomp/lzham_decomp.h index eb9a74d7..fded67ce 100644 --- a/r5dev/thirdparty/lzham/lzhamdecomp/lzham_decomp.h +++ b/r5dev/thirdparty/lzham/lzhamdecomp/lzham_decomp.h @@ -11,7 +11,7 @@ namespace lzham lzham_decompress_state_ptr LZHAM_CDECL lzham_lib_decompress_reinit(lzham_decompress_state_ptr pState, const lzham_decompress_params *pParams); - lzham_decompress_checksums* LZHAM_CDECL lzham_lib_decompress_deinit(lzham_decompress_state_ptr pState); + lzham_decompress_checksums LZHAM_CDECL lzham_lib_decompress_deinit(lzham_decompress_state_ptr pState); lzham_decompress_status_t LZHAM_CDECL lzham_lib_decompress( lzham_decompress_state_ptr pState, diff --git a/r5dev/thirdparty/lzham/lzhamdecomp/lzham_lzdecomp.cpp b/r5dev/thirdparty/lzham/lzhamdecomp/lzham_lzdecomp.cpp index 53deceb1..f5fa9093 100644 --- a/r5dev/thirdparty/lzham/lzhamdecomp/lzham_lzdecomp.cpp +++ b/r5dev/thirdparty/lzham/lzhamdecomp/lzham_lzdecomp.cpp @@ -1273,16 +1273,15 @@ namespace lzham return pState; } - lzham_decompress_checksums* LZHAM_CDECL lzham_lib_decompress_deinit(lzham_decompress_state_ptr p) + lzham_decompress_checksums LZHAM_CDECL lzham_lib_decompress_deinit(lzham_decompress_state_ptr p) { + lzham_decompress_checksums checksums{}; lzham_decompressor *pState = static_cast(p); if (!pState) - return NULL; + return checksums; - lzham_decompress_checksums* checksums = new lzham_decompress_checksums(); - - checksums->adler32 = pState->m_decomp_adler32; - checksums->crc32 = pState->m_decomp_crc32; + checksums.adler32 = pState->m_decomp_adler32; + checksums.crc32 = pState->m_decomp_crc32; lzham_free(pState->m_pRaw_decomp_buf); lzham_delete(pState); @@ -1357,12 +1356,12 @@ namespace lzham return LZHAM_DECOMP_STATUS_FAILED_INITIALIZING; lzham_decompress_status_t status = lzham_lib_decompress(pState, pSrc_buf, &src_len, pDst_buf, pDst_len, true); - lzham_decompress_checksums* checksums = lzham_lib_decompress_deinit(pState); + lzham_decompress_checksums checksums = lzham_lib_decompress_deinit(pState); if (pAdler32) - *pAdler32 = checksums->adler32; + *pAdler32 = checksums.adler32; if (pCrc32) - *pCrc32 = checksums->crc32; + *pCrc32 = checksums.crc32; return status; @@ -1511,10 +1510,10 @@ namespace lzham lzham_decompress_state_ptr pState = static_cast(pStream->state); if (pState) { - lzham_decompress_checksums* checksums = lzham_lib_decompress_deinit(pState); + lzham_decompress_checksums checksums = lzham_lib_decompress_deinit(pState); - pStream->adler32 = checksums->adler32; - pStream->crc32 = checksums->crc32; + pStream->adler32 = checksums.adler32; + pStream->crc32 = checksums.crc32; pStream->state = NULL; }