summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2016-09-26 13:06:32 (GMT)
committerCMake Topic Stage <kwrobot@kitware.com>2016-09-26 13:06:32 (GMT)
commitd5fbb19b4995c9ed9dfc44b4b8e391ede21d3849 (patch)
treeebb90856ece9a7b14e11b1f21e16a719846bd179 /Source
parentc6f07d06c610a9b1e6062d70ffc78c04bdf48ee4 (diff)
parentb941f3bfe7ba3bffd535372c2a4d9d668bc83356 (diff)
downloadCMake-d5fbb19b4995c9ed9dfc44b4b8e391ede21d3849.zip
CMake-d5fbb19b4995c9ed9dfc44b4b8e391ede21d3849.tar.gz
CMake-d5fbb19b4995c9ed9dfc44b4b8e391ede21d3849.tar.bz2
Merge topic 'ctest-no-manual-delete'
b941f3bf CTest::CompressString: Avoid manual delete a5a7771a CTest::CompressString: Reorder code to avoid unnecessary allocation 6ed56457 CTest::Base64EncodeFile: Avoid manual delete
Diffstat (limited to 'Source')
-rw-r--r--Source/cmCTest.cxx47
1 files changed, 18 insertions, 29 deletions
diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx
index 6523e3e..9b5248e 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<char*>(file_buffer), len);
+ std::vector<char> 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<char> 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<unsigned char*>(&file_buffer[0]), len,
+ reinterpret_cast<unsigned char*>(&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)
@@ -2795,48 +2790,42 @@ bool cmCTest::CompressString(std::string& str)
int ret;
z_stream strm;
- unsigned char* in =
- reinterpret_cast<unsigned char*>(const_cast<char*>(str.c_str()));
- // zlib makes the guarantee that this is the maximum output size
- int outSize =
- static_cast<int>(static_cast<double>(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<unsigned char*>(const_cast<char*>(str.c_str()));
+ // zlib makes the guarantee that this is the maximum output size
+ int outSize =
+ static_cast<int>(static_cast<double>(str.size()) * 1.001 + 13.0);
+ std::vector<unsigned char> out(outSize);
+
strm.avail_in = static_cast<uInt>(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<unsigned char> base64EncodedBuffer((outSize * 3) / 2);
size_t rlen =
- cmsysBase64_Encode(out, strm.total_out, base64EncodedBuffer, 1);
-
- str = "";
- str.append(reinterpret_cast<char*>(base64EncodedBuffer), rlen);
+ cmsysBase64_Encode(&out[0], strm.total_out, &base64EncodedBuffer[0], 1);
- delete[] base64EncodedBuffer;
- delete[] out;
+ str.assign(reinterpret_cast<char*>(&base64EncodedBuffer[0]), rlen);
return true;
}