Add some missing std::nothrow

... and allocation failure checks.
This commit is contained in:
Adrien Destugues
2014-01-16 13:29:15 +01:00
parent b70c72a692
commit 547c1486ff
3 changed files with 41 additions and 20 deletions
+1 -1
View File
@@ -203,7 +203,7 @@ BHttpFormData::CopyBuffer()
if (fDataType != B_HTTPFORM_BUFFER) if (fDataType != B_HTTPFORM_BUFFER)
return B_ERROR; return B_ERROR;
char* copiedBuffer = new char[fBufferSize]; char* copiedBuffer = new(std::nothrow) char[fBufferSize];
if (copiedBuffer == NULL) if (copiedBuffer == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
+25 -13
View File
@@ -43,9 +43,9 @@ BHttpRequest::BHttpRequest(const BUrl& url, bool ssl, const char* protocolName,
{ {
_ResetOptions(); _ResetOptions();
if (ssl) if (ssl)
fSocket = new BSecureSocket(); fSocket = new(std::nothrow) BSecureSocket();
else else
fSocket = new BSocket(); fSocket = new(std::nothrow) BSocket();
} }
@@ -120,7 +120,7 @@ BHttpRequest::SetAutoReferrer(bool enable)
void void
BHttpRequest::SetHeaders(const BHttpHeaders& headers) BHttpRequest::SetHeaders(const BHttpHeaders& headers)
{ {
AdoptHeaders(new BHttpHeaders(headers)); AdoptHeaders(new(std::nothrow) BHttpHeaders(headers));
} }
@@ -135,7 +135,7 @@ BHttpRequest::AdoptHeaders(BHttpHeaders* const headers)
void void
BHttpRequest::SetPostFields(const BHttpForm& fields) BHttpRequest::SetPostFields(const BHttpForm& fields)
{ {
AdoptPostFields(new BHttpForm(fields)); AdoptPostFields(new(std::nothrow) BHttpForm(fields));
} }
@@ -241,8 +241,10 @@ BHttpRequest::Result() const
status_t status_t
BHttpRequest::Stop() BHttpRequest::Stop()
{ {
fSocket->Disconnect(); if (fSocket) {
// Unlock any pending connect, read or write operation. fSocket->Disconnect();
// Unlock any pending connect, read or write operation.
}
return BUrlRequest::Stop(); return BUrlRequest::Stop();
} }
@@ -340,10 +342,14 @@ BHttpRequest::_ProtocolLoop()
if (authentication->Method() == B_HTTP_AUTHENTICATION_NONE) { if (authentication->Method() == B_HTTP_AUTHENTICATION_NONE) {
// There is no authentication context for this // There is no authentication context for this
// url yet, so let's create one. // url yet, so let's create one.
authentication = new BHttpAuthentication(); authentication = new(std::nothrow) BHttpAuthentication();
status = authentication->Initialize( if (authentication == NULL)
fHeaders["WWW-Authenticate"]); status = B_NO_MEMORY;
fContext->AddAuthentication(fUrl, authentication); else {
status = authentication->Initialize(
fHeaders["WWW-Authenticate"]);
fContext->AddAuthentication(fUrl, authentication);
}
} }
newRequest = false; newRequest = false;
@@ -414,6 +420,9 @@ BHttpRequest::_ResolveHostName()
status_t status_t
BHttpRequest::_MakeRequest() BHttpRequest::_MakeRequest()
{ {
if (fSocket == NULL)
return B_NO_MEMORY;
_EmitDebug(B_URL_PROTOCOL_DEBUG_TEXT, "Connection to %s on port %d.", _EmitDebug(B_URL_PROTOCOL_DEBUG_TEXT, "Connection to %s on port %d.",
fUrl.Authority().String(), fRemoteAddr.Port()); fUrl.Authority().String(), fRemoteAddr.Port());
status_t connectError = fSocket->Connect(fRemoteAddr); status_t connectError = fSocket->Connect(fRemoteAddr);
@@ -531,7 +540,7 @@ BHttpRequest::_MakeRequest()
ssize_t bytesRead = 0; ssize_t bytesRead = 0;
ssize_t bytesReceived = 0; ssize_t bytesReceived = 0;
ssize_t bytesTotal = 0; ssize_t bytesTotal = 0;
char* inputTempBuffer = new char[kHttpBufferSize]; char* inputTempBuffer = new(std::nothrow) char[kHttpBufferSize];
ssize_t inputTempSize = kHttpBufferSize; ssize_t inputTempSize = kHttpBufferSize;
ssize_t chunkSize = -1; ssize_t chunkSize = -1;
@@ -601,7 +610,7 @@ BHttpRequest::_MakeRequest()
if (inputTempSize < chunkSize + 2) { if (inputTempSize < chunkSize + 2) {
delete[] inputTempBuffer; delete[] inputTempBuffer;
inputTempSize = chunkSize + 2; inputTempSize = chunkSize + 2;
inputTempBuffer = new char[inputTempSize]; inputTempBuffer = new(std::nothrow) char[inputTempSize];
} }
fInputBuffer.RemoveData(inputTempBuffer, fInputBuffer.RemoveData(inputTempBuffer,
chunkSize + 2); chunkSize + 2);
@@ -647,7 +656,7 @@ BHttpRequest::_MakeRequest()
if (inputTempSize < bytesRead) { if (inputTempSize < bytesRead) {
inputTempSize = bytesRead; inputTempSize = bytesRead;
delete[] inputTempBuffer; delete[] inputTempBuffer;
inputTempBuffer = new char[bytesRead]; inputTempBuffer = new(std::nothrow) char[bytesRead];
} }
fInputBuffer.RemoveData(inputTempBuffer, bytesRead); fInputBuffer.RemoveData(inputTempBuffer, bytesRead);
} }
@@ -694,6 +703,9 @@ BHttpRequest::_GetLine(BString& destString)
return B_ERROR; return B_ERROR;
char* temporaryBuffer = new(std::nothrow) char[characterIndex + 1]; char* temporaryBuffer = new(std::nothrow) char[characterIndex + 1];
if (temporaryBuffer == NULL)
return B_NO_MEMORY;
fInputBuffer.RemoveData(temporaryBuffer, characterIndex + 1); fInputBuffer.RemoveData(temporaryBuffer, characterIndex + 1);
// Strip end-of-line character(s) // Strip end-of-line character(s)
+15 -6
View File
@@ -73,6 +73,11 @@ BSecureSocket::Private::~Private()
// SSL_free also frees the underlying BIO. // SSL_free also frees the underlying BIO.
if (fSSL != NULL) if (fSSL != NULL)
SSL_free(fSSL); SSL_free(fSSL);
else {
// The SSL session was never created (Connect() was not called or
// failed). We must free the BIO we created in the constructor.
BIO_free(fBIO);
}
} }
@@ -141,11 +146,15 @@ BSecureSocket::Private::VerifyCallback(int ok, X509_STORE_CTX* ctx)
// Get the certificate that we could not validate (this may not be the one // Get the certificate that we could not validate (this may not be the one
// we got from the server, but something higher up in the certificate // we got from the server, but something higher up in the certificate
// chain) // chain)
X509* certificate = X509_STORE_CTX_get_current_cert(ctx); X509* x509 = X509_STORE_CTX_get_current_cert(ctx);
BCertificate::Private* certificate =
new(std::nothrow) BCertificate::Private(x509);
if (certificate == NULL)
return 0;
// Let the BSecureSocket (or subclass) decide if we should continue anyway. // Let the BSecureSocket (or subclass) decide if we should continue anyway.
return socket->CertificateVerificationFailed(BCertificate( return socket->CertificateVerificationFailed(BCertificate(certificate));
new BCertificate::Private(certificate)));
} }
@@ -154,7 +163,7 @@ BSecureSocket::Private::VerifyCallback(int ok, X509_STORE_CTX* ctx)
BSecureSocket::BSecureSocket() BSecureSocket::BSecureSocket()
: :
fPrivate(new BSecureSocket::Private()) fPrivate(new(std::nothrow) BSecureSocket::Private())
{ {
fInitStatus = fPrivate != NULL ? fPrivate->InitCheck() : B_NO_MEMORY; fInitStatus = fPrivate != NULL ? fPrivate->InitCheck() : B_NO_MEMORY;
} }
@@ -162,7 +171,7 @@ BSecureSocket::BSecureSocket()
BSecureSocket::BSecureSocket(const BNetworkAddress& peer, bigtime_t timeout) BSecureSocket::BSecureSocket(const BNetworkAddress& peer, bigtime_t timeout)
: :
fPrivate(new BSecureSocket::Private()) fPrivate(new(std::nothrow) BSecureSocket::Private())
{ {
fInitStatus = fPrivate != NULL ? fPrivate->InitCheck() : B_NO_MEMORY; fInitStatus = fPrivate != NULL ? fPrivate->InitCheck() : B_NO_MEMORY;
Connect(peer, timeout); Connect(peer, timeout);
@@ -173,7 +182,7 @@ BSecureSocket::BSecureSocket(const BSecureSocket& other)
: :
BSocket(other) BSocket(other)
{ {
fPrivate = new BSecureSocket::Private(*other.fPrivate); fPrivate = new(std::nothrow) BSecureSocket::Private(*other.fPrivate);
// TODO: this won't work this way! - write working copy constructor for // TODO: this won't work this way! - write working copy constructor for
// Private. // Private.