Bug 1372065 - fix cppcheck linter error in Necko. r?jduell draft
authorShih-Chiang Chien <schien@mozilla.com>
Mon, 12 Jun 2017 11:51:26 +0800
changeset 671589 a7a862c3319244bba74688affb34e0dba85b6a12
parent 670404 bc56729898954e32d3a3731d03d178ed78924c33
child 733560 ed5913057729672523cad8e68db013571d3b3a1b
push id81983
push userbmo:schien@mozilla.com
push dateThu, 28 Sep 2017 03:32:43 +0000
reviewersjduell
bugs1372065
milestone58.0a1
Bug 1372065 - fix cppcheck linter error in Necko. r?jduell For netwerk/cache2/CacheFileInputStream.cpp:148 and netwerk/protocol/http/nsHttpHeaderArray.cpp:358, missing "()" in the if statement. For netwerk/base/rust-url-capi/test/test.cpp:29, netwerk/streamconv/converters/nsHTTPCompressConv.cpp:297, and netwerk/streamconv/converters/nsHTTPCompressConv.cpp:300, null pointer will be returned but the original memory buffer will not be freed if |realloc| fails. We should remember the original memory buffer and free it if error is detected. MozReview-Commit-ID: 2ggXsL73jYV
netwerk/base/rust-url-capi/test/test.cpp
netwerk/cache2/CacheFileInputStream.cpp
netwerk/protocol/http/nsHttpHeaderArray.cpp
netwerk/streamconv/converters/nsHTTPCompressConv.cpp
--- a/netwerk/base/rust-url-capi/test/test.cpp
+++ b/netwerk/base/rust-url-capi/test/test.cpp
@@ -21,17 +21,20 @@ public:
     free(mBuffer);
     mBuffer = nullptr;
   }
 
   void SetSize(size_t size)
   {
     mLength = size;
     if (mBuffer) {
-      mBuffer = (char *)realloc(mBuffer, size);
+      char* originalBuffer = mBuffer;
+      if (!(mBuffer = (char *)realloc(originalBuffer, size))) {
+        free(originalBuffer);
+      }
       return;
     }
     mBuffer = (char *)malloc(size);
   }
 
   char * GetBuffer()
   {
     return mBuffer;
@@ -137,9 +140,9 @@ int main() {
   assert(rusturl_has_fragment(url) == 0);
   TEST_CALL(rusturl_get_spec(url, &container), 0);
   container.CheckEquals("http://example.com/");
 
   rusturl_free(url);
 
   printf("SUCCESS\n");
   return 0;
-}
\ No newline at end of file
+}
--- a/netwerk/cache2/CacheFileInputStream.cpp
+++ b/netwerk/cache2/CacheFileInputStream.cpp
@@ -140,18 +140,19 @@ CacheFileInputStream::ReadSegments(nsWri
          "stream is in ReadSegments!"));
     return NS_ERROR_UNEXPECTED;
   }
 
   if (mClosed) {
     LOG(("CacheFileInputStream::ReadSegments() - Stream is closed. [this=%p, "
          "status=0x%08" PRIx32 "]", this, static_cast<uint32_t>(mStatus)));
 
-    if NS_FAILED(mStatus)
+    if (NS_FAILED(mStatus)) {
       return mStatus;
+    }
 
     return NS_OK;
   }
 
   EnsureCorrectChunk(false);
 
   while (true) {
     if (NS_FAILED(mStatus))
--- a/netwerk/protocol/http/nsHttpHeaderArray.cpp
+++ b/netwerk/protocol/http/nsHttpHeaderArray.cpp
@@ -338,17 +338,17 @@ nsHttpHeaderArray::VisitHeaders(nsIHttpH
 
         nsAutoCString hdr;
         if (entry.headerNameOriginal.IsEmpty()) {
             hdr = nsDependentCString(entry.header);
         } else {
             hdr = entry.headerNameOriginal;
         }
         rv = visitor->VisitHeader(hdr, entry.value);
-        if NS_FAILED(rv) {
+        if (NS_FAILED(rv)) {
             return rv;
         }
     }
     return NS_OK;
 }
 
 /*static*/ nsresult
 nsHttpHeaderArray::ParseHeaderLine(const nsACString& line,
--- a/netwerk/streamconv/converters/nsHTTPCompressConv.cpp
+++ b/netwerk/streamconv/converters/nsHTTPCompressConv.cpp
@@ -289,20 +289,26 @@ nsHTTPCompressConv::OnDataAvailable(nsIR
       return NS_OK;
     }
 
     MOZ_FALLTHROUGH;
 
   case HTTP_COMPRESS_DEFLATE:
 
     if (mInpBuffer != nullptr && streamLen > mInpBufferLen) {
-      mInpBuffer = (unsigned char *) realloc(mInpBuffer, mInpBufferLen = streamLen);
+      unsigned char* originalInpBuffer = mInpBuffer;
+      if (!(mInpBuffer = (unsigned char *) realloc(originalInpBuffer, mInpBufferLen = streamLen))) {
+        free(originalInpBuffer);
+      }
 
       if (mOutBufferLen < streamLen * 2) {
-        mOutBuffer = (unsigned char *) realloc(mOutBuffer, mOutBufferLen = streamLen * 3);
+        unsigned char* originalOutBuffer = mOutBuffer;
+        if (!(mOutBuffer = (unsigned char *) realloc(mOutBuffer, mOutBufferLen = streamLen * 3))) {
+          free(originalOutBuffer);
+        }
       }
 
       if (mInpBuffer == nullptr || mOutBuffer == nullptr) {
         return NS_ERROR_OUT_OF_MEMORY;
       }
     }
 
     if (mInpBuffer == nullptr) {