From 547c1486ff31415b89ffc4e87e3d06e933850b96 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Thu, 16 Jan 2014 13:20:15 +0100 Subject: [PATCH] Add some missing std::nothrow ... and allocation failure checks. --- src/kits/network/libnetapi/HttpForm.cpp | 2 +- src/kits/network/libnetapi/HttpRequest.cpp | 38 ++++++++++++++------- src/kits/network/libnetapi/SecureSocket.cpp | 21 ++++++++---- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/kits/network/libnetapi/HttpForm.cpp b/src/kits/network/libnetapi/HttpForm.cpp index 4fd8690d72..ef14efab8e 100644 --- a/src/kits/network/libnetapi/HttpForm.cpp +++ b/src/kits/network/libnetapi/HttpForm.cpp @@ -203,7 +203,7 @@ BHttpFormData::CopyBuffer() if (fDataType != B_HTTPFORM_BUFFER) return B_ERROR; - char* copiedBuffer = new char[fBufferSize]; + char* copiedBuffer = new(std::nothrow) char[fBufferSize]; if (copiedBuffer == NULL) return B_NO_MEMORY; diff --git a/src/kits/network/libnetapi/HttpRequest.cpp b/src/kits/network/libnetapi/HttpRequest.cpp index 53a807e54f..dda79205a4 100644 --- a/src/kits/network/libnetapi/HttpRequest.cpp +++ b/src/kits/network/libnetapi/HttpRequest.cpp @@ -43,9 +43,9 @@ BHttpRequest::BHttpRequest(const BUrl& url, bool ssl, const char* protocolName, { _ResetOptions(); if (ssl) - fSocket = new BSecureSocket(); + fSocket = new(std::nothrow) BSecureSocket(); else - fSocket = new BSocket(); + fSocket = new(std::nothrow) BSocket(); } @@ -120,7 +120,7 @@ BHttpRequest::SetAutoReferrer(bool enable) void 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 BHttpRequest::SetPostFields(const BHttpForm& fields) { - AdoptPostFields(new BHttpForm(fields)); + AdoptPostFields(new(std::nothrow) BHttpForm(fields)); } @@ -241,8 +241,10 @@ BHttpRequest::Result() const status_t BHttpRequest::Stop() { - fSocket->Disconnect(); - // Unlock any pending connect, read or write operation. + if (fSocket) { + fSocket->Disconnect(); + // Unlock any pending connect, read or write operation. + } return BUrlRequest::Stop(); } @@ -340,10 +342,14 @@ BHttpRequest::_ProtocolLoop() if (authentication->Method() == B_HTTP_AUTHENTICATION_NONE) { // There is no authentication context for this // url yet, so let's create one. - authentication = new BHttpAuthentication(); - status = authentication->Initialize( - fHeaders["WWW-Authenticate"]); - fContext->AddAuthentication(fUrl, authentication); + authentication = new(std::nothrow) BHttpAuthentication(); + if (authentication == NULL) + status = B_NO_MEMORY; + else { + status = authentication->Initialize( + fHeaders["WWW-Authenticate"]); + fContext->AddAuthentication(fUrl, authentication); + } } newRequest = false; @@ -414,6 +420,9 @@ BHttpRequest::_ResolveHostName() status_t BHttpRequest::_MakeRequest() { + if (fSocket == NULL) + return B_NO_MEMORY; + _EmitDebug(B_URL_PROTOCOL_DEBUG_TEXT, "Connection to %s on port %d.", fUrl.Authority().String(), fRemoteAddr.Port()); status_t connectError = fSocket->Connect(fRemoteAddr); @@ -531,7 +540,7 @@ BHttpRequest::_MakeRequest() ssize_t bytesRead = 0; ssize_t bytesReceived = 0; ssize_t bytesTotal = 0; - char* inputTempBuffer = new char[kHttpBufferSize]; + char* inputTempBuffer = new(std::nothrow) char[kHttpBufferSize]; ssize_t inputTempSize = kHttpBufferSize; ssize_t chunkSize = -1; @@ -601,7 +610,7 @@ BHttpRequest::_MakeRequest() if (inputTempSize < chunkSize + 2) { delete[] inputTempBuffer; inputTempSize = chunkSize + 2; - inputTempBuffer = new char[inputTempSize]; + inputTempBuffer = new(std::nothrow) char[inputTempSize]; } fInputBuffer.RemoveData(inputTempBuffer, chunkSize + 2); @@ -647,7 +656,7 @@ BHttpRequest::_MakeRequest() if (inputTempSize < bytesRead) { inputTempSize = bytesRead; delete[] inputTempBuffer; - inputTempBuffer = new char[bytesRead]; + inputTempBuffer = new(std::nothrow) char[bytesRead]; } fInputBuffer.RemoveData(inputTempBuffer, bytesRead); } @@ -694,6 +703,9 @@ BHttpRequest::_GetLine(BString& destString) return B_ERROR; char* temporaryBuffer = new(std::nothrow) char[characterIndex + 1]; + if (temporaryBuffer == NULL) + return B_NO_MEMORY; + fInputBuffer.RemoveData(temporaryBuffer, characterIndex + 1); // Strip end-of-line character(s) diff --git a/src/kits/network/libnetapi/SecureSocket.cpp b/src/kits/network/libnetapi/SecureSocket.cpp index 08a90188d8..a121785b6a 100644 --- a/src/kits/network/libnetapi/SecureSocket.cpp +++ b/src/kits/network/libnetapi/SecureSocket.cpp @@ -73,6 +73,11 @@ BSecureSocket::Private::~Private() // SSL_free also frees the underlying BIO. if (fSSL != NULL) 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 // we got from the server, but something higher up in the certificate // 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. - return socket->CertificateVerificationFailed(BCertificate( - new BCertificate::Private(certificate))); + return socket->CertificateVerificationFailed(BCertificate(certificate)); } @@ -154,7 +163,7 @@ BSecureSocket::Private::VerifyCallback(int ok, X509_STORE_CTX* ctx) BSecureSocket::BSecureSocket() : - fPrivate(new BSecureSocket::Private()) + fPrivate(new(std::nothrow) BSecureSocket::Private()) { fInitStatus = fPrivate != NULL ? fPrivate->InitCheck() : B_NO_MEMORY; } @@ -162,7 +171,7 @@ BSecureSocket::BSecureSocket() 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; Connect(peer, timeout); @@ -173,7 +182,7 @@ BSecureSocket::BSecureSocket(const BSecureSocket& 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 // Private.