Check for nullptr return from malloc, calloc, and realloc. (#1023)

These are used in ntcore and cscore.  Add inline null-checking versions
to wpi/memory.h and use them throughout.
This commit is contained in:
Peter Johnson
2018-05-04 17:55:46 -07:00
committed by GitHub
parent e8d5759d95
commit 7a34f5d17d
9 changed files with 148 additions and 68 deletions

View File

@@ -15,6 +15,7 @@
#include <wpi/MathExtras.h>
#include <wpi/leb128.h>
#include <wpi/memory.h>
using namespace nt;
@@ -52,7 +53,7 @@ WireDecoder::WireDecoder(wpi::raw_istream& is, unsigned int proto_rev,
// Start with a 1K temporary buffer. Use malloc instead of new so we can
// realloc.
m_allocated = 1024;
m_buf = static_cast<char*>(std::malloc(m_allocated));
m_buf = static_cast<char*>(wpi::CheckedMalloc(m_allocated));
m_proto_rev = proto_rev;
m_error = nullptr;
}
@@ -71,7 +72,7 @@ void WireDecoder::Realloc(size_t len) {
if (m_allocated >= len) return;
size_t newlen = m_allocated * 2;
while (newlen < len) newlen *= 2;
m_buf = static_cast<char*>(std::realloc(m_buf, newlen));
m_buf = static_cast<char*>(wpi::CheckedRealloc(m_buf, newlen));
m_allocated = newlen;
}