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

@@ -7,6 +7,7 @@
#include <stdint.h>
#include <wpi/memory.h>
#include <wpi/timestamp.h>
#include "Value_internal.h"
@@ -113,7 +114,7 @@ void nt::ConvertToC(const Value& in, NT_Value* out) {
case NT_BOOLEAN_ARRAY: {
auto v = in.GetBooleanArray();
out->data.arr_boolean.arr =
static_cast<int*>(std::malloc(v.size() * sizeof(int)));
static_cast<int*>(wpi::CheckedMalloc(v.size() * sizeof(int)));
out->data.arr_boolean.size = v.size();
std::copy(v.begin(), v.end(), out->data.arr_boolean.arr);
break;
@@ -121,15 +122,15 @@ void nt::ConvertToC(const Value& in, NT_Value* out) {
case NT_DOUBLE_ARRAY: {
auto v = in.GetDoubleArray();
out->data.arr_double.arr =
static_cast<double*>(std::malloc(v.size() * sizeof(double)));
static_cast<double*>(wpi::CheckedMalloc(v.size() * sizeof(double)));
out->data.arr_double.size = v.size();
std::copy(v.begin(), v.end(), out->data.arr_double.arr);
break;
}
case NT_STRING_ARRAY: {
auto v = in.GetStringArray();
out->data.arr_string.arr =
static_cast<NT_String*>(std::malloc(v.size() * sizeof(NT_String)));
out->data.arr_string.arr = static_cast<NT_String*>(
wpi::CheckedMalloc(v.size() * sizeof(NT_String)));
for (size_t i = 0; i < v.size(); ++i)
ConvertToC(v[i], &out->data.arr_string.arr[i]);
out->data.arr_string.size = v.size();
@@ -144,7 +145,7 @@ void nt::ConvertToC(const Value& in, NT_Value* out) {
void nt::ConvertToC(wpi::StringRef in, NT_String* out) {
out->len = in.size();
out->str = static_cast<char*>(std::malloc(in.size() + 1));
out->str = static_cast<char*>(wpi::CheckedMalloc(in.size() + 1));
std::memcpy(out->str, in.data(), in.size());
out->str[in.size()] = '\0';
}