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.
This commit is contained in:
Lioncash 2020-04-05 10:16:07 -04:00
parent f77fb2089b
commit 9e503edaf1
1 changed files with 7 additions and 7 deletions

View File

@ -97,8 +97,8 @@ std::unique_ptr<atUint8[]> PAK::Entry::getBuffer(const nod::Node& pak, atUint64&
atUint32 decompSz; atUint32 decompSz;
strm->read(&decompSz, 4); strm->read(&decompSz, 4);
decompSz = hecl::SBig(decompSz); decompSz = hecl::SBig(decompSz);
atUint8* buf = new atUint8[decompSz]; std::unique_ptr<atUint8[]> buf{new atUint8[decompSz]};
atUint8* bufCur = buf; atUint8* bufCur = buf.get();
atUint8 compBuf[0x8000]; atUint8 compBuf[0x8000];
if (compressed == 1) { if (compressed == 1) {
@ -106,7 +106,7 @@ std::unique_ptr<atUint8[]> PAK::Entry::getBuffer(const nod::Node& pak, atUint64&
z_stream zs = {}; z_stream zs = {};
inflateInit(&zs); inflateInit(&zs);
zs.avail_out = decompSz; zs.avail_out = decompSz;
zs.next_out = buf; zs.next_out = buf.get();
while (zs.avail_out) { while (zs.avail_out) {
atUint64 readSz = strm->read(compBuf, std::min(compRem, atUint32(0x8000))); atUint64 readSz = strm->read(compBuf, std::min(compRem, atUint32(0x8000)));
compRem -= readSz; compRem -= readSz;
@ -130,12 +130,12 @@ std::unique_ptr<atUint8[]> PAK::Entry::getBuffer(const nod::Node& pak, atUint64&
} }
szOut = decompSz; szOut = decompSz;
return std::unique_ptr<atUint8[]>(buf); return buf;
} else { } else {
atUint8* buf = new atUint8[size]; std::unique_ptr<atUint8[]> buf{new atUint8[size]};
pak.beginReadStream(offset)->read(buf, size); pak.beginReadStream(offset)->read(buf.get(), size);
szOut = size; szOut = size;
return std::unique_ptr<atUint8[]>(buf); return buf;
} }
} }