From 9e503edaf16f65d064b8440597b3f554e0a7e785 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 5 Apr 2020 10:16:07 -0400 Subject: [PATCH] PAK: Make use of unique_ptr in getBuffer() Same behavior, however we seat the allocated memory into a unique_ptr from the get-go to prevent any possible memory being leaked. --- DataSpec/DNAMP1/PAK.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/DataSpec/DNAMP1/PAK.cpp b/DataSpec/DNAMP1/PAK.cpp index f24d65721..4b7835662 100644 --- a/DataSpec/DNAMP1/PAK.cpp +++ b/DataSpec/DNAMP1/PAK.cpp @@ -97,8 +97,8 @@ std::unique_ptr PAK::Entry::getBuffer(const nod::Node& pak, atUint64& atUint32 decompSz; strm->read(&decompSz, 4); decompSz = hecl::SBig(decompSz); - atUint8* buf = new atUint8[decompSz]; - atUint8* bufCur = buf; + std::unique_ptr buf{new atUint8[decompSz]}; + atUint8* bufCur = buf.get(); atUint8 compBuf[0x8000]; if (compressed == 1) { @@ -106,7 +106,7 @@ std::unique_ptr PAK::Entry::getBuffer(const nod::Node& pak, atUint64& z_stream zs = {}; inflateInit(&zs); zs.avail_out = decompSz; - zs.next_out = buf; + zs.next_out = buf.get(); while (zs.avail_out) { atUint64 readSz = strm->read(compBuf, std::min(compRem, atUint32(0x8000))); compRem -= readSz; @@ -130,12 +130,12 @@ std::unique_ptr PAK::Entry::getBuffer(const nod::Node& pak, atUint64& } szOut = decompSz; - return std::unique_ptr(buf); + return buf; } else { - atUint8* buf = new atUint8[size]; - pak.beginReadStream(offset)->read(buf, size); + std::unique_ptr buf{new atUint8[size]}; + pak.beginReadStream(offset)->read(buf.get(), size); szOut = size; - return std::unique_ptr(buf); + return buf; } }