Module: check_mk
Branch: master
Commit: 869181b9bf74b164fb5939922e50a8ac1ef7e4c0
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=869181b9bf74b1…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Mon Nov 9 09:10:03 2015 +0100
Bug fix: Handled failed realloc in a safer way.
---
livestatus/src/OutputBuffer.cc | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/livestatus/src/OutputBuffer.cc b/livestatus/src/OutputBuffer.cc
index 0aaad9e..c3b8c1e 100644
--- a/livestatus/src/OutputBuffer.cc
+++ b/livestatus/src/OutputBuffer.cc
@@ -79,6 +79,8 @@ void OutputBuffer::addBuffer(const char *buf, unsigned len)
_writepos += len;
}
+// TODO: All this code is highly error-prone due to overflow, failed allocations
+// etc. We should just use vector instead.
void OutputBuffer::needSpace(unsigned len)
{
if (_writepos + len > _end)
@@ -88,7 +90,11 @@ void OutputBuffer::needSpace(unsigned len)
while (_max_size < needed) // double, until enough space
_max_size *= 2;
- _buffer = (char *)realloc(_buffer, _max_size);
+ char* new_buffer = static_cast<char*>(realloc(_buffer, _max_size));
+ // It's better to crash voluntarily than overwriting random memory later.
+ if (!new_buffer) abort();
+
+ _buffer = new_buffer;
_writepos = _buffer + s;
_end = _buffer + _max_size;
}