Network Kit: Coverity scan review and fixes

CID 1108353, 1108335: memory leak.
CID 610473: unused variable.
CID 1108446, 1108433, 1108432, 1108419, 1108400, 991710, 991713, 991712,
	610098, 610097, 610096, 610095: uninitialized field
CID 1108421: unused field

Change the ownership of the result for Url/HttpRequests. The request now
owns its result and you either access it by reference while the request
is live, or copy it to keep it after the request destruction. To help
with that, get BUrlResult copy constructor and assignment operator to
work.

Performance issue: copying the BUrlResult also copies the underlying
BMallocIO data. This should be shared between the BUrlResult objects to
make the copy lighter. The case of BUrlSynchronousRequest is now
particularly inefficient, with at least 2 copies needed to get at the
result.
This commit is contained in:
Adrien Destugues 2013-10-19 23:31:57 +02:00
parent 72eb88f5c8
commit b3d13a000c
12 changed files with 30 additions and 25 deletions

View File

@ -23,7 +23,7 @@ class BAbstractSocket;
class BHttpRequest : public BUrlRequest {
public:
BHttpRequest(const BUrl& url,
BUrlResult& result, bool ssl = false,
bool ssl = false,
const char *protocolName = "HTTP",
BUrlProtocolListener* listener = NULL,
BUrlContext* context = NULL);

View File

@ -118,8 +118,6 @@ private:
mutable bool fAuthorityValid : 1;
mutable bool fUserInfoValid : 1;
bool fBasicUri : 1;
bool fHasProtocol : 1;
bool fHasUserName : 1;
bool fHasPassword : 1;

View File

@ -18,7 +18,6 @@ public:
BUrlRequest(const BUrl& url,
BUrlProtocolListener* listener,
BUrlContext* context,
BUrlResult& result,
const char* threadName,
const char* protocolName);
virtual ~BUrlRequest();
@ -37,7 +36,7 @@ public:
// URL protocol parameters access
const BUrl& Url() const;
BUrlResult& Result() const;
const BUrlResult& Result() const;
BUrlContext* Context() const;
BUrlProtocolListener* Listener() const;
const BString& Protocol() const;
@ -63,7 +62,7 @@ protected:
protected:
BUrl fUrl;
BUrlResult& fResult;
BUrlResult fResult;
BUrlContext* fContext;
BUrlProtocolListener* fListener;

View File

@ -704,6 +704,7 @@ BHttpForm::_GetMultipartHeader(const BHttpFormData* element) const
BHttpForm::Iterator::Iterator(BHttpForm* form)
: fElement(NULL)
{
fForm = form;
fStdIterator = form->fFields.begin();

View File

@ -28,11 +28,10 @@ static const char* kHttpProtocolThreadStrStatus[
};
BHttpRequest::BHttpRequest(const BUrl& url, BUrlResult& result, bool ssl,
const char* protocolName, BUrlProtocolListener* listener,
BUrlContext* context)
BHttpRequest::BHttpRequest(const BUrl& url, bool ssl, const char* protocolName,
BUrlProtocolListener* listener, BUrlContext* context)
:
BUrlRequest(url, listener, context, result, "BUrlProtocol.HTTP", protocolName),
BUrlRequest(url, listener, context, "BUrlProtocol.HTTP", protocolName),
fSocket(NULL),
fSSL(ssl),
fRequestMethod(B_HTTP_GET),

View File

@ -631,6 +631,8 @@ BNetworkCookie::operator!=(const BNetworkCookie& other)
void
BNetworkCookie::_Reset()
{
fInitStatus = false;
fName.Truncate(0);
fValue.Truncate(0);
fDomain.Truncate(0);

View File

@ -59,7 +59,7 @@ BNetworkCookieJar::BNetworkCookieJar(BMessage* archive)
if (heapCookie == NULL)
break;
if (!AddCookie(heapCookie)) {
if (AddCookie(heapCookie) != B_OK) {
delete heapCookie;
continue;
}
@ -69,9 +69,7 @@ BNetworkCookieJar::BNetworkCookieJar(BMessage* archive)
BNetworkCookieJar::~BNetworkCookieJar()
{
BNetworkCookie* cookiePtr;
for (Iterator it = GetIterator(); (cookiePtr = it.Next()) != NULL;)
for (Iterator it = GetIterator(); it.Next() != NULL;)
delete it.Remove();
}

View File

@ -52,6 +52,8 @@ BUrl::BUrl(BMessage* archive)
if (archive->FindString(kArchivedUrl, &url) == B_OK)
SetUrlString(url);
else
_ResetFields();
}
@ -139,6 +141,7 @@ BUrl::BUrl()
fRequest(),
fHasAuthority(false)
{
_ResetFields();
}

View File

@ -20,14 +20,12 @@
BUrlProtocolRoster::MakeRequest(const BUrl& url,
BUrlProtocolListener* listener, BUrlContext* context)
{
BUrlResult* result = new BUrlResult(url);
// FIXME this is leaked
// TODO: instanciate the correct BUrlProtocol using add-on interface
if (url.Protocol() == "http") {
return new(std::nothrow) BHttpRequest(url, *result, false, "HTTP", listener,
return new(std::nothrow) BHttpRequest(url, false, "HTTP", listener,
context);
} else if (url.Protocol() == "https") {
return new(std::nothrow) BHttpRequest(url, *result, true, "HTTPS", listener,
return new(std::nothrow) BHttpRequest(url, true, "HTTPS", listener,
context);
}

View File

@ -28,15 +28,15 @@ static const char* kProtocolThreadStrStatus[B_PROT_THREAD_STATUS__END+1]
BUrlRequest::BUrlRequest(const BUrl& url, BUrlProtocolListener* listener,
BUrlContext* context, BUrlResult& result, const char* threadName,
const char* protocolName)
BUrlContext* context, const char* threadName, const char* protocolName)
:
fUrl(url),
fResult(result),
fResult(url),
fContext(context),
fListener(listener),
fQuit(false),
fRunning(false),
fThreadStatus(B_PROT_PAUSED),
fThreadId(0),
fThreadName(threadName),
fProtocol(protocolName)
@ -46,6 +46,7 @@ BUrlRequest::BUrlRequest(const BUrl& url, BUrlProtocolListener* listener,
BUrlRequest::~BUrlRequest()
{
Stop();
}
@ -168,7 +169,7 @@ BUrlRequest::Url() const
}
BUrlResult&
const BUrlResult&
BUrlRequest::Result() const
{
return fResult;

View File

@ -18,7 +18,8 @@ BUrlResult::BUrlResult(const BUrl& url)
:
fUrl(url),
fRawData(),
fHeaders()
fHeaders(),
fStatusCode(0)
{
}
@ -101,7 +102,10 @@ BUrlResult::operator=(const BUrlResult& other)
fHeaders = other.fHeaders;
fRawData.SetSize(other.fRawData.BufferLength());
fRawData.WriteAt(0, fRawData.Buffer(), fRawData.BufferLength());
fRawData.WriteAt(0, other.fRawData.Buffer(), other.fRawData.BufferLength());
// FIXME this makes a copy of the data, it would be better to share it
fStatusCode = other.fStatusCode;
return *this;
}

View File

@ -15,7 +15,7 @@
BUrlSynchronousRequest::BUrlSynchronousRequest(BUrlRequest& request)
:
BUrlRequest(request.Url(), this, request.Context(), request.Result(),
BUrlRequest(request.Url(), NULL, request.Context(),
"BUrlSynchronousRequest", request.Protocol()),
fRequestComplete(false),
fWrappedRequest(request)
@ -45,6 +45,8 @@ BUrlSynchronousRequest::WaitUntilCompletion()
while (!fRequestComplete)
snooze(10000);
fResult = fWrappedRequest.Result();
return B_OK;
}