Bug 1372065 - fix cppcheck linter error in Necko. r=jduell
authorShih-Chiang Chien <schien@mozilla.com>
Mon, 12 Jun 2017 11:51:26 +0800
changeset 383444 e0adce5b62a4f10f346b95f51f2553b7ce320109
parent 383443 e2174dbb6efc708cbb67b1d4b54938c12e3f2fa1
child 383445 d2b369de68ee0aa889f112469b27ab19224633c2
push id32594
push userkwierso@gmail.com
push dateThu, 28 Sep 2017 22:49:33 +0000
treeherdermozilla-central@6dea0ee45b66 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjduell
bugs1372065
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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) {