Title: [193607] trunk/Tools
Revision
193607
Author
ddkil...@apple.com
Date
2015-12-06 19:27:30 -0800 (Sun, 06 Dec 2015)

Log Message

TestNetscapePlugIn: Fix leaks found by static analyzer
<http://webkit.org/b/151881>

Reviewed by Darin Adler.

Fixes the following leaks found by the static analyzer:
    DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp:808:16: warning: Potential leak of memory pointed to by 'path'
            return false;
                   ^~~~~
    DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp:808:16: warning: Potential leak of memory pointed to by 'target'
            return false;
                   ^~~~~
    DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp:808:16: warning: Potential leak of memory pointed to by 'url'
            return false;
                   ^~~~~

* DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp:
(toCString): Switch to returning std::unique_ptr<char[]>.  Add
early return if allocation fails.
(testPostURLFile): Switch to using std::unique_ptr<char[]>.
Make sure to call fclose() on filehandle, even for an early
return.
(testSetStatus): Switch to using std::unique_ptr<char[]>.

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (193606 => 193607)


--- trunk/Tools/ChangeLog	2015-12-07 01:54:43 UTC (rev 193606)
+++ trunk/Tools/ChangeLog	2015-12-07 03:27:30 UTC (rev 193607)
@@ -1,3 +1,29 @@
+2015-12-06  David Kilzer  <ddkil...@apple.com>
+
+        TestNetscapePlugIn: Fix leaks found by static analyzer
+        <http://webkit.org/b/151881>
+
+        Reviewed by Darin Adler.
+
+        Fixes the following leaks found by the static analyzer:
+            DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp:808:16: warning: Potential leak of memory pointed to by 'path'
+                    return false;
+                           ^~~~~
+            DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp:808:16: warning: Potential leak of memory pointed to by 'target'
+                    return false;
+                           ^~~~~
+            DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp:808:16: warning: Potential leak of memory pointed to by 'url'
+                    return false;
+                           ^~~~~
+
+        * DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp:
+        (toCString): Switch to returning std::unique_ptr<char[]>.  Add
+        early return if allocation fails.
+        (testPostURLFile): Switch to using std::unique_ptr<char[]>.
+        Make sure to call fclose() on filehandle, even for an early
+        return.
+        (testSetStatus): Switch to using std::unique_ptr<char[]>.
+
 2015-12-05  David Kilzer  <ddkil...@apple.com>
 
         prepare-ChangeLog: Fix some warning messages when using svn

Modified: trunk/Tools/DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp (193606 => 193607)


--- trunk/Tools/DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp	2015-12-07 01:54:43 UTC (rev 193606)
+++ trunk/Tools/DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp	2015-12-07 03:27:30 UTC (rev 193607)
@@ -30,6 +30,7 @@
 #include "PluginTest.h"
 #include "TestObject.h"
 #include <assert.h>
+#include <memory>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -775,12 +776,16 @@
     return true;
 }
 
-static char* toCString(const NPString& string)
+static std::unique_ptr<char[]> toCString(const NPString& string)
 {
-    char* result = static_cast<char*>(malloc(string.UTF8Length + 1));
-    memcpy(result, string.UTF8Characters, string.UTF8Length);
-    result[string.UTF8Length] = '\0';
+    size_t length = string.UTF8Length;
+    std::unique_ptr<char[]> result(new char[length + 1]);
+    if (!result)
+        return result;
 
+    memcpy(result.get(), string.UTF8Characters, length);
+    result[length] = '\0';
+
     return result;
 }
 
@@ -790,30 +795,27 @@
         return false;
 
     NPString urlString = NPVARIANT_TO_STRING(args[0]);
-    char* url = ""
+    auto url = ""
 
     NPString targetString = NPVARIANT_TO_STRING(args[1]);
-    char* target = toCString(targetString);
+    auto target = toCString(targetString);
 
     NPString pathString = NPVARIANT_TO_STRING(args[2]);
-    char* path = toCString(pathString);
+    auto path = toCString(pathString);
 
     NPString contentsString = NPVARIANT_TO_STRING(args[3]);
 
-    FILE* tempFile = fopen(path, "w");
+    FILE* tempFile = fopen(path.get(), "w");
     if (!tempFile)
         return false;
 
-    if (!fwrite(contentsString.UTF8Characters, contentsString.UTF8Length, 1, tempFile))
-        return false;
-
+    size_t count = fwrite(contentsString.UTF8Characters, contentsString.UTF8Length, 1, tempFile);
     fclose(tempFile);
 
-    NPError error = browser->posturl(obj->npp, url, target, pathString.UTF8Length, path, TRUE);
+    if (!count)
+        return false;
 
-    free(path);
-    free(target);
-    free(url);
+    NPError error = browser->posturl(obj->npp, url.get(), target.get(), pathString.UTF8Length, path.get(), TRUE);
 
     BOOLEAN_TO_NPVARIANT(error == NPERR_NO_ERROR, *result);
     return true;
@@ -972,15 +974,14 @@
 
 static bool testSetStatus(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
 {
-    char* message = 0;
+    std::unique_ptr<char[]> message(nullptr);
     if (argCount && NPVARIANT_IS_STRING(args[0])) {
         NPString statusString = NPVARIANT_TO_STRING(args[0]);
         message = toCString(statusString);
     }
-    
-    browser->status(obj->npp, message);
 
-    free(message);
+    browser->status(obj->npp, message.get());
+
     return true;
 }
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to