BNetBuffer: add some error checks.

The allocation of fImpl can fail, and some methods used it without
checking. Return an error code (or NULL or 0) instead of crashing in
these cases.

Also InitCheck the fInputBuffer in BHttpRequest before trying to use it.

Fixes #11350.
This commit is contained in:
Adrien Destugues 2014-10-18 10:07:52 +02:00
parent bcd40fc754
commit 4f978fe4db
2 changed files with 22 additions and 0 deletions

View File

@ -330,6 +330,10 @@ BHttpRequest::_ProtocolLoop()
port = fContext->GetProxyPort();
}
status_t result = fInputBuffer.InitCheck();
if (result != B_OK)
return result;
if (!_ResolveHostName(host, port)) {
_EmitDebug(B_URL_PROTOCOL_DEBUG_ERROR,
"Unable to resolve hostname (%s), aborting.",

View File

@ -185,6 +185,9 @@ BNetBuffer::AppendString(const char* data)
status_t
BNetBuffer::AppendData(const void* data, size_t size)
{
if (fInit != B_OK)
return B_NO_INIT;
ssize_t bytesWritten = fImpl->Write(data, size);
if (bytesWritten < 0)
return (status_t)bytesWritten;
@ -335,6 +338,9 @@ BNetBuffer::RemoveString(char* data, size_t size)
status_t
BNetBuffer::RemoveData(void* data, size_t size)
{
if (fInit != B_OK)
return B_NO_INIT;
ssize_t bytesRead = fImpl->Read(data, size);
if (bytesRead < 0)
return (status_t)bytesRead;
@ -345,6 +351,9 @@ BNetBuffer::RemoveData(void* data, size_t size)
status_t
BNetBuffer::RemoveMessage(BMessage& data)
{
if (fInit != B_OK)
return B_NO_INIT;
unsigned char* bufferPtr = fImpl->Data();
if (*(int32*)bufferPtr != B_MESSAGE_TYPE)
@ -398,6 +407,9 @@ BNetBuffer::RemoveUint64(uint64& data)
unsigned char*
BNetBuffer::Data() const
{
if (fInit != B_OK)
return NULL;
return fImpl->Data();
}
@ -405,6 +417,9 @@ BNetBuffer::Data() const
size_t
BNetBuffer::Size() const
{
if (fInit != B_OK)
return 0;
return fImpl->Size();
}
@ -412,6 +427,9 @@ BNetBuffer::Size() const
size_t
BNetBuffer::BytesRemaining() const
{
if (fInit != B_OK)
return 0;
return fImpl->BytesRemaining();
}