ENGINE-320 COM-72: Keep ownership of blobs, freeing them after the engine call ENGINE-320 COM-72
authorMarkus Schaber <markus@pep-security.net>
Sun, 17 Dec 2017 13:19:29 +0100
branchENGINE-320 COM-72
changeset 266c68b8488ac36
parent 265 d13ae58b6862
ENGINE-320 COM-72: Keep ownership of blobs, freeing them after the engine call

Rework the Code to be a bit more C++-ish :-)
pEp_utility.h
     1.1 --- a/pEp_utility.h	Sun Dec 17 11:03:41 2017 +0100
     1.2 +++ b/pEp_utility.h	Sun Dec 17 13:19:29 2017 +0100
     1.3 @@ -79,23 +79,19 @@
     1.4  			return psaUDType;
     1.5  		}
     1.6  
     1.7 +		// Utility class which holds the blob contents and releases them at the
     1.8 +		// end of the scope via RAII.
     1.9  		class blob_buffers {
    1.10  		private:
    1.11 -			std::vector<char*> buffers;
    1.12 +			std::vector<std::unique_ptr<char[]>> buffers;
    1.13  
    1.14  		public:
    1.15  			char* allocate(size_t size) 
    1.16  			{
    1.17 -				char* buffer = (char*)malloc(size);
    1.18 -				buffers.push_back(buffer);
    1.19 -				return buffer;
    1.20 -			}
    1.21 -			
    1.22 -			~blob_buffers() {
    1.23 -				for (auto it = buffers.begin(); it != buffers.end(); it += 1) 
    1.24 -				{
    1.25 -					free(*it);
    1.26 -				}
    1.27 +				auto buffer = std::make_unique<char[]>(size);
    1.28 +				auto rawptr = buffer.get();
    1.29 +				buffers.push_back(std::move(buffer));
    1.30 +				return rawptr;
    1.31  			}
    1.32  		};
    1.33