From a9fae8ac1d952ffd3b54de6082f9729466101bb0 Mon Sep 17 00:00:00 2001 From: Rolf Eike Beer Date: Mon, 22 Dec 2014 21:56:14 +0100 Subject: [PATCH] CTest: Fix integer overflow when uploading huge files When uploading files greater 2GB a cast to 'int' overflows, leading to a bad alloc when passed to new. Also avoid floating point arithmetic when integer calculations will work as well. Reported-by: Justin Borodinsky --- Source/cmCTest.cxx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index 2bf7b77e7..814c4e02f 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -1688,7 +1688,7 @@ std::string cmCTest::Base64GzipEncodeFile(std::string file) //---------------------------------------------------------------------- std::string cmCTest::Base64EncodeFile(std::string file) { - long len = cmSystemTools::FileLength(file); + size_t const len = cmSystemTools::FileLength(file); cmsys::ifstream ifs(file.c_str(), std::ios::in #ifdef _WIN32 | std::ios::binary @@ -1699,8 +1699,7 @@ std::string cmCTest::Base64EncodeFile(std::string file) ifs.close(); unsigned char *encoded_buffer - = new unsigned char [ static_cast( - static_cast(len) * 1.5 + 5.0) ]; + = new unsigned char [ (len * 3) / 2 + 5 ]; unsigned long rlen = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1); @@ -3192,7 +3191,7 @@ bool cmCTest::CompressString(std::string& str) // Now base64 encode the resulting binary string unsigned char* base64EncodedBuffer - = new unsigned char[static_cast(outSize * 1.5)]; + = new unsigned char[(outSize * 3) / 2]; unsigned long rlen = cmsysBase64_Encode(out, strm.total_out, base64EncodedBuffer, 1);