CTest::CompressString: Avoid manual delete

This commit is contained in:
Daniel Pfeifer 2016-09-23 22:06:17 +02:00
parent a5a7771a42
commit b941f3bfe7
1 changed files with 5 additions and 10 deletions

View File

@ -2803,34 +2803,29 @@ bool cmCTest::CompressString(std::string& str)
// zlib makes the guarantee that this is the maximum output size // zlib makes the guarantee that this is the maximum output size
int outSize = int outSize =
static_cast<int>(static_cast<double>(str.size()) * 1.001 + 13.0); static_cast<int>(static_cast<double>(str.size()) * 1.001 + 13.0);
unsigned char* out = new unsigned char[outSize]; std::vector<unsigned char> out(outSize);
strm.avail_in = static_cast<uInt>(str.size()); strm.avail_in = static_cast<uInt>(str.size());
strm.next_in = in; strm.next_in = in;
strm.avail_out = outSize; strm.avail_out = outSize;
strm.next_out = out; strm.next_out = &out[0];
ret = deflate(&strm, Z_FINISH); ret = deflate(&strm, Z_FINISH);
if (ret == Z_STREAM_ERROR || ret != Z_STREAM_END) { if (ret == Z_STREAM_ERROR || ret != Z_STREAM_END) {
cmCTestLog(this, ERROR_MESSAGE, "Error during gzip compression." cmCTestLog(this, ERROR_MESSAGE, "Error during gzip compression."
<< std::endl); << std::endl);
delete[] out;
return false; return false;
} }
(void)deflateEnd(&strm); (void)deflateEnd(&strm);
// Now base64 encode the resulting binary string // Now base64 encode the resulting binary string
unsigned char* base64EncodedBuffer = new unsigned char[(outSize * 3) / 2]; std::vector<unsigned char> base64EncodedBuffer((outSize * 3) / 2);
size_t rlen = size_t rlen =
cmsysBase64_Encode(out, strm.total_out, base64EncodedBuffer, 1); cmsysBase64_Encode(&out[0], strm.total_out, &base64EncodedBuffer[0], 1);
str = ""; str.assign(reinterpret_cast<char*>(&base64EncodedBuffer[0]), rlen);
str.append(reinterpret_cast<char*>(base64EncodedBuffer), rlen);
delete[] base64EncodedBuffer;
delete[] out;
return true; return true;
} }