From e1f2fe26552625c935aad319f7ee43c9d05f01c1 Mon Sep 17 00:00:00 2001 From: remittor Date: Fri, 29 Nov 2019 11:29:26 +0300 Subject: [PATCH] Fix critical bug on reserve memory --- src/lz4pack.cpp | 3 +-- src/utils.cpp | 3 +-- src/wcx_archive.cpp | 6 ++---- src/wcx_cache.cpp | 9 +++------ src/wcx_packer.cpp | 12 +++++------- 5 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/lz4pack.cpp b/src/lz4pack.cpp index 8115cfd..3a04855 100644 --- a/src/lz4pack.cpp +++ b/src/lz4pack.cpp @@ -51,8 +51,7 @@ int packer::frame_create(UINT64 data_size) size_t bsz = LZ4F_compressFrameBound(m_block_size, &m_prefs); if (bsz > m_buf.size()) { size_t msz = bsz + 2048; - size_t sz = m_buf.reserve(msz + 2048); - FIN_IF(sz == bst::npos, -11); + FIN_IF(!m_buf.reserve(msz + 2048), -11); m_buf.resize(msz); } diff --git a/src/utils.cpp b/src/utils.cpp index 9eca062..b22109c 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -82,8 +82,7 @@ int get_full_filename(LPCWSTR path, LPCWSTR name, bst::wstr & fullname) { size_t fnlen = path ? wcslen(path) : 0; fnlen += wcslen(name); - size_t cap = fullname.reserve(fnlen + 32); - if (cap == bst::npos) + if (!fullname.reserve(fnlen + 32)) return -10; int hr = get_full_filename(path, name, fullname.data(), fnlen + 15); if (hr) { diff --git a/src/wcx_archive.cpp b/src/wcx_archive.cpp index 2e742e3..ce2e78d 100644 --- a/src/wcx_archive.cpp +++ b/src/wcx_archive.cpp @@ -192,13 +192,11 @@ int archive::extract(int Operation, LPCWSTR DestPath, LPCWSTR DestName) const size_t buf_size = 4*1024*1024; if (m_buf.capacity() < buf_size) { - size_t sz = m_buf.reserve(buf_size + 256); - FIN_IF(sz == bst::npos, 0x117000 | E_NO_MEMORY); + FIN_IF(!m_buf.reserve(buf_size + 256), 0x117000 | E_NO_MEMORY); m_buf.resize(buf_size); } if (m_dst.capacity() < buf_size) { - size_t sz = m_dst.reserve(buf_size + 256); - FIN_IF(sz == bst::npos, 0x118000 | E_NO_MEMORY); + FIN_IF(!m_dst.reserve(buf_size + 256), 0x118000 | E_NO_MEMORY); m_dst.resize(buf_size); } diff --git a/src/wcx_cache.cpp b/src/wcx_cache.cpp index 6c51bb8..5bc2a39 100644 --- a/src/wcx_cache.cpp +++ b/src/wcx_cache.cpp @@ -350,8 +350,7 @@ int cache::scan_pax_file(HANDLE hFile) // TODO: support GNU tar!!! FIN_IF(m_arcfile.get_tar_format() != tar::POSIX_FORMAT, 0x45010100 | E_EOPEN); - size_t sz = buf.reserve(buf_size + tar::BLOCKSIZE * 2); - FIN_IF(sz == bst::npos, 0x45010100 | E_EOPEN); + FIN_IF(!buf.reserve(buf_size + tar::BLOCKSIZE * 2), 0x45010100 | E_EOPEN); buf.resize(buf_size); pos.QuadPart = 0; @@ -426,12 +425,10 @@ int cache::scan_paxlz4_file(HANDLE hFile) bst::buf dst; tar::pax_decode pax; - size_t sz = buf.reserve(buf_size + tar::BLOCKSIZE); - FIN_IF(sz == bst::npos, 0x44010100 | E_EOPEN); + FIN_IF(!buf.reserve(buf_size + tar::BLOCKSIZE), 0x44010100 | E_EOPEN); buf.resize(buf_size); - sz = dst.reserve(buf_size + tar::BLOCKSIZE); - FIN_IF(sz == bst::npos, 0x44010200 | E_EOPEN); + FIN_IF(!dst.reserve(buf_size + tar::BLOCKSIZE), 0x44010200 | E_EOPEN); dst.resize(buf_size); pos.QuadPart = m_arcfile.get_data_begin(); diff --git a/src/wcx_packer.cpp b/src/wcx_packer.cpp index 6a974bd..7a49990 100644 --- a/src/wcx_packer.cpp +++ b/src/wcx_packer.cpp @@ -53,10 +53,9 @@ int packer::pack_files(LPCWSTR SubPath, LPCWSTR SrcPath, LPCWSTR AddList) m_delete_out_file = true; if (m_buf.empty()) { - size_t bufsize = 4*1024*1024; - sz = m_buf.reserve(bufsize + tar::BLOCKSIZE * 8); - FIN_IF(sz == bst::npos, 0x207000 | E_NO_MEMORY); - sz = m_buf.resize(bufsize); + const size_t bufsize = 4*1024*1024; + FIN_IF(!m_buf.reserve(bufsize + tar::BLOCKSIZE * 8), 0x207000 | E_NO_MEMORY); + m_buf.resize(bufsize); FIN_IF(m_buf.size() & tar::BLOCKSIZE_MASK, 0x208000 | E_NO_MEMORY); } m_lz4.reset(); @@ -75,9 +74,8 @@ int packer::pack_files(LPCWSTR SubPath, LPCWSTR SrcPath, LPCWSTR AddList) FIN_IF(!x || dw != frlen, 0x209300 | E_ECREATE); if (m_ext_buf.empty()) { - size_t bufsize = 2*1024*1024; - sz = m_ext_buf.reserve(bufsize + 64); - FIN_IF(sz == bst::npos, 0x209900 | E_NO_MEMORY); + const size_t bufsize = 2*1024*1024; + FIN_IF(!m_ext_buf.reserve(bufsize + 64), 0x209900 | E_NO_MEMORY); m_ext_buf.resize(bufsize); }