mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user