Skip to content
Permalink
Browse files
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[]>.


Canonical link: https://commits.webkit.org/170057@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@193607 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
ddkilzer committed Dec 7, 2015
1 parent 34850c1 commit 33f03010356cda2fbb21c7b54495cbbb24d5b1f2
Showing with 46 additions and 19 deletions.
  1. +26 −0 Tools/ChangeLog
  2. +20 −19 Tools/DumpRenderTree/TestNetscapePlugIn/PluginObject.cpp
@@ -1,3 +1,29 @@
2015-12-06 David Kilzer <ddkilzer@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 <ddkilzer@apple.com>

prepare-ChangeLog: Fix some warning messages when using svn
@@ -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,11 +776,15 @@ static bool testGetPropertyReturnValue(PluginObject* obj, const NPVariant* args,
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 @@ static bool testPostURLFile(PluginObject* obj, const NPVariant* args, uint32_t a
return false;

NPString urlString = NPVARIANT_TO_STRING(args[0]);
char* url = toCString(urlString);
auto url = toCString(urlString);

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 @@ bool testWindowOpen(NPP npp)

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;
}

0 comments on commit 33f0301

Please sign in to comment.