From 6ed564577dd0fffdd7dabfaa0ba14e2518c26048 Mon Sep 17 00:00:00 2001 From: Daniel Pfeifer Date: Fri, 23 Sep 2016 22:03:49 +0200 Subject: CTest::Base64EncodeFile: Avoid manual delete --- Source/cmCTest.cxx | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index 6523e3e..d058ca7 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -1506,22 +1506,17 @@ std::string cmCTest::Base64EncodeFile(std::string const& file) | std::ios::binary #endif ); - unsigned char* file_buffer = new unsigned char[len + 1]; - ifs.read(reinterpret_cast(file_buffer), len); + std::vector file_buffer(len + 1); + ifs.read(&file_buffer[0], len); ifs.close(); - unsigned char* encoded_buffer = new unsigned char[(len * 3) / 2 + 5]; + std::vector encoded_buffer((len * 3) / 2 + 5); - size_t const rlen = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1); + size_t const rlen = cmsysBase64_Encode( + reinterpret_cast(&file_buffer[0]), len, + reinterpret_cast(&encoded_buffer[0]), 1); - std::string base64 = ""; - for (size_t i = 0; i < rlen; i++) { - base64 += encoded_buffer[i]; - } - delete[] file_buffer; - delete[] encoded_buffer; - - return base64; + return std::string(&encoded_buffer[0], rlen); } bool cmCTest::SubmitExtraFiles(const VectorOfStrings& files) -- cgit v0.12 From a5a7771a428a1d4a9bb671e56ea6d497361bc753 Mon Sep 17 00:00:00 2001 From: Daniel Pfeifer Date: Fri, 23 Sep 2016 22:04:47 +0200 Subject: CTest::CompressString: Reorder code to avoid unnecessary allocation --- Source/cmCTest.cxx | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index d058ca7..0624b52 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -2790,22 +2790,21 @@ bool cmCTest::CompressString(std::string& str) int ret; z_stream strm; - unsigned char* in = - reinterpret_cast(const_cast(str.c_str())); - // zlib makes the guarantee that this is the maximum output size - int outSize = - static_cast(static_cast(str.size()) * 1.001 + 13.0); - unsigned char* out = new unsigned char[outSize]; - strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; ret = deflateInit(&strm, -1); // default compression level if (ret != Z_OK) { - delete[] out; return false; } + unsigned char* in = + reinterpret_cast(const_cast(str.c_str())); + // zlib makes the guarantee that this is the maximum output size + int outSize = + static_cast(static_cast(str.size()) * 1.001 + 13.0); + unsigned char* out = new unsigned char[outSize]; + strm.avail_in = static_cast(str.size()); strm.next_in = in; strm.avail_out = outSize; -- cgit v0.12 From b941f3bfe7ba3bffd535372c2a4d9d668bc83356 Mon Sep 17 00:00:00 2001 From: Daniel Pfeifer Date: Fri, 23 Sep 2016 22:06:17 +0200 Subject: CTest::CompressString: Avoid manual delete --- Source/cmCTest.cxx | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index 0624b52..9b5248e 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -2803,34 +2803,29 @@ bool cmCTest::CompressString(std::string& str) // zlib makes the guarantee that this is the maximum output size int outSize = static_cast(static_cast(str.size()) * 1.001 + 13.0); - unsigned char* out = new unsigned char[outSize]; + std::vector out(outSize); strm.avail_in = static_cast(str.size()); strm.next_in = in; strm.avail_out = outSize; - strm.next_out = out; + strm.next_out = &out[0]; ret = deflate(&strm, Z_FINISH); if (ret == Z_STREAM_ERROR || ret != Z_STREAM_END) { cmCTestLog(this, ERROR_MESSAGE, "Error during gzip compression." << std::endl); - delete[] out; return false; } (void)deflateEnd(&strm); // Now base64 encode the resulting binary string - unsigned char* base64EncodedBuffer = new unsigned char[(outSize * 3) / 2]; + std::vector base64EncodedBuffer((outSize * 3) / 2); size_t rlen = - cmsysBase64_Encode(out, strm.total_out, base64EncodedBuffer, 1); + cmsysBase64_Encode(&out[0], strm.total_out, &base64EncodedBuffer[0], 1); - str = ""; - str.append(reinterpret_cast(base64EncodedBuffer), rlen); - - delete[] base64EncodedBuffer; - delete[] out; + str.assign(reinterpret_cast(&base64EncodedBuffer[0]), rlen); return true; } -- cgit v0.12