From a9fae8ac1d952ffd3b54de6082f9729466101bb0 Mon Sep 17 00:00:00 2001 From: Rolf Eike Beer Date: Mon, 22 Dec 2014 21:56:14 +0100 Subject: [PATCH 1/2] 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); From 0bdd4ebfb8303cc46e4a8bbe16f6c7bcc7108e2a Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 23 Dec 2014 11:26:55 -0500 Subject: [PATCH 2/2] cmCTest: Use size_t for cmsysBase64_Encode return value --- Source/CTest/cmCTestRunTest.cxx | 4 ++-- Source/CTest/cmCTestTestHandler.cxx | 5 ++--- Source/cmCTest.cxx | 6 +++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Source/CTest/cmCTestRunTest.cxx b/Source/CTest/cmCTestRunTest.cxx index 9e3c9fcf0..ff55528b4 100644 --- a/Source/CTest/cmCTestRunTest.cxx +++ b/Source/CTest/cmCTestRunTest.cxx @@ -116,10 +116,10 @@ void cmCTestRunTest::CompressOutput() unsigned char *encoded_buffer = new unsigned char[static_cast(outSize * 1.5)]; - unsigned long rlen + size_t rlen = cmsysBase64_Encode(out, strm.total_out, encoded_buffer, 1); - for(unsigned long i = 0; i < rlen; i++) + for(size_t i = 0; i < rlen; i++) { this->CompressedOutput += encoded_buffer[i]; } diff --git a/Source/CTest/cmCTestTestHandler.cxx b/Source/CTest/cmCTestTestHandler.cxx index f330e584b..8518a90d4 100644 --- a/Source/CTest/cmCTestTestHandler.cxx +++ b/Source/CTest/cmCTestTestHandler.cxx @@ -1976,9 +1976,8 @@ std::string cmCTestTestHandler::GenerateRegressionImages( = new unsigned char [ static_cast( static_cast(len) * 1.5 + 5.0) ]; - unsigned long rlen + size_t rlen = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1); - unsigned long cc; ostr << "\t\t\t" << std::endl << "\t\t\t\t"; - for ( cc = 0; cc < rlen; cc ++ ) + for (size_t cc = 0; cc < rlen; cc ++ ) { ostr << encoded_buffer[cc]; if ( cc % 60 == 0 && cc ) diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index 814c4e02f..1362a503c 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -1701,11 +1701,11 @@ std::string cmCTest::Base64EncodeFile(std::string file) unsigned char *encoded_buffer = new unsigned char [ (len * 3) / 2 + 5 ]; - unsigned long rlen + size_t const rlen = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1); std::string base64 = ""; - for(unsigned long i = 0; i < rlen; i++) + for(size_t i = 0; i < rlen; i++) { base64 += encoded_buffer[i]; } @@ -3193,7 +3193,7 @@ bool cmCTest::CompressString(std::string& str) unsigned char* base64EncodedBuffer = new unsigned char[(outSize * 3) / 2]; - unsigned long rlen + size_t rlen = cmsysBase64_Encode(out, strm.total_out, base64EncodedBuffer, 1); str = "";