Fix a memory leak in TBinaryProtocol.
authorDavid Reiss <dreiss@apache.org>
Tue, 1 Apr 2008 04:17:58 +0000 (04:17 +0000)
committerDavid Reiss <dreiss@apache.org>
Tue, 1 Apr 2008 04:17:58 +0000 (04:17 +0000)
Summary:
realloc doesn't free the original pointer when it returns null.

Reviewed By: cpiro

Test Plan: make check

TracCamp Project: Thrift

Revert Plan: ok

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665626 13f79535-47bb-0310-9956-ffa450edef68

lib/cpp/src/protocol/TBinaryProtocol.cpp

index b3e9fbc..2d98395 100644 (file)
@@ -406,11 +406,11 @@ uint32_t TBinaryProtocol::readStringBody(string& str, int32_t size) {
 
   // Use the heap here to prevent stack overflow for v. large strings
   if (size > string_buf_size_ || string_buf_ == NULL) {
-    string_buf_ = (uint8_t*)std::realloc(string_buf_, (uint32_t)size);
-    if (string_buf_ == NULL) {
-      string_buf_size_ = 0;
+    void* new_string_buf = std::realloc(string_buf_, (uint32_t)size);
+    if (new_string_buf == NULL) {
       throw TProtocolException(TProtocolException::UNKNOWN, "Out of memory in TBinaryProtocol::readString");
     }
+    string_buf_ = (uint8_t*)new_string_buf;
     string_buf_size_ = size;
   }
   trans_->readAll(string_buf_, size);