Skip to content

Commit

Permalink
[parser] throw bad_alloc if malloc/realloc is failed
Browse files Browse the repository at this point in the history
Normally, we don't expect to see malloc/realloc failure,
but it is possible when running in harsh condition.
  • Loading branch information
justinjoy authored and loganek committed Jan 8, 2019
1 parent e5b3052 commit b6b966b
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions parser/protocol_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <cassert>
#include <cstring>
#include <cstdlib>

namespace HawkTracer
{
Expand Down Expand Up @@ -141,20 +142,38 @@ bool ProtocolReader::_read_string(FieldType& value)
{
size_t length = 32;
size_t pos = 0;
char* data = (char*)malloc(length);
char* data;

if (void *tmp = std::malloc(length))
{
data = static_cast<char*>(tmp);
}
else
{
throw std::bad_alloc();
}

int c;
while ((c = _stream->read_byte()) > 0)
{
data[pos++] = (char)c;
if (pos == length)
{
length *= 2;
data = (char*)realloc(data, length);
if (void *tmp = std::realloc(data, length))
{
data = static_cast<char*>(tmp);
}
else
{
std::free(data);
throw std::bad_alloc();
}
}
}
if (c < 0)
{
free(data);
std::free(data);
return false;
}

Expand Down

0 comments on commit b6b966b

Please sign in to comment.