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:
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/TCPConnector.h>
|
||||
#include <wpi/memory.h>
|
||||
#include <wpi/timestamp.h>
|
||||
|
||||
#include "Handle.h"
|
||||
@@ -574,7 +575,8 @@ void CS_SetHttpCameraUrls(CS_Source source, const char** urls, int count,
|
||||
|
||||
char** CS_GetHttpCameraUrls(CS_Source source, int* count, CS_Status* status) {
|
||||
auto urls = cs::GetHttpCameraUrls(source, status);
|
||||
char** out = static_cast<char**>(std::malloc(urls.size() * sizeof(char*)));
|
||||
char** out =
|
||||
static_cast<char**>(wpi::CheckedMalloc(urls.size() * sizeof(char*)));
|
||||
*count = urls.size();
|
||||
for (size_t i = 0; i < urls.size(); ++i) out[i] = cs::ConvertToC(urls[i]);
|
||||
return out;
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/memory.h>
|
||||
#include <wpi/timestamp.h>
|
||||
|
||||
#include "Handle.h"
|
||||
@@ -1315,7 +1316,7 @@ char* CS_GetUsbCameraPath(CS_Source source, CS_Status* status) {
|
||||
CS_UsbCameraInfo* CS_EnumerateUsbCameras(int* count, CS_Status* status) {
|
||||
auto cameras = cs::EnumerateUsbCameras(status);
|
||||
CS_UsbCameraInfo* out = static_cast<CS_UsbCameraInfo*>(
|
||||
std::malloc(cameras.size() * sizeof(CS_UsbCameraInfo)));
|
||||
wpi::CheckedMalloc(cameras.size() * sizeof(CS_UsbCameraInfo)));
|
||||
*count = cameras.size();
|
||||
for (size_t i = 0; i < cameras.size(); ++i) {
|
||||
out[i].dev = cameras[i].dev;
|
||||
|
||||
@@ -12,11 +12,12 @@
|
||||
#include <cstring>
|
||||
|
||||
#include <wpi/StringRef.h>
|
||||
#include <wpi/memory.h>
|
||||
|
||||
namespace cs {
|
||||
|
||||
inline char* ConvertToC(wpi::StringRef in) {
|
||||
char* out = static_cast<char*>(std::malloc(in.size() + 1));
|
||||
char* out = static_cast<char*>(wpi::CheckedMalloc(in.size() + 1));
|
||||
std::memmove(out, in.data(), in.size());
|
||||
out[in.size()] = '\0';
|
||||
return out;
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/memory.h>
|
||||
|
||||
#include "c_util.h"
|
||||
#include "cscore_cpp.h"
|
||||
@@ -68,7 +69,8 @@ void CS_SetStringProperty(CS_Property property, const char* value,
|
||||
char** CS_GetEnumPropertyChoices(CS_Property property, int* count,
|
||||
CS_Status* status) {
|
||||
auto choices = cs::GetEnumPropertyChoices(property, status);
|
||||
char** out = static_cast<char**>(std::malloc(choices.size() * sizeof(char*)));
|
||||
char** out =
|
||||
static_cast<char**>(wpi::CheckedMalloc(choices.size() * sizeof(char*)));
|
||||
*count = choices.size();
|
||||
for (size_t i = 0; i < choices.size(); ++i)
|
||||
out[i] = cs::ConvertToC(choices[i]);
|
||||
@@ -110,8 +112,8 @@ CS_Property* CS_EnumerateSourceProperties(CS_Source source, int* count,
|
||||
CS_Status* status) {
|
||||
wpi::SmallVector<CS_Property, 32> buf;
|
||||
auto vec = cs::EnumerateSourceProperties(source, buf, status);
|
||||
CS_Property* out =
|
||||
static_cast<CS_Property*>(std::malloc(vec.size() * sizeof(CS_Property)));
|
||||
CS_Property* out = static_cast<CS_Property*>(
|
||||
wpi::CheckedMalloc(vec.size() * sizeof(CS_Property)));
|
||||
*count = vec.size();
|
||||
std::copy(vec.begin(), vec.end(), out);
|
||||
return out;
|
||||
@@ -162,7 +164,7 @@ CS_VideoMode* CS_EnumerateSourceVideoModes(CS_Source source, int* count,
|
||||
CS_Status* status) {
|
||||
auto vec = cs::EnumerateSourceVideoModes(source, status);
|
||||
CS_VideoMode* out = static_cast<CS_VideoMode*>(
|
||||
std::malloc(vec.size() * sizeof(CS_VideoMode)));
|
||||
wpi::CheckedMalloc(vec.size() * sizeof(CS_VideoMode)));
|
||||
*count = vec.size();
|
||||
std::copy(vec.begin(), vec.end(), out);
|
||||
return out;
|
||||
@@ -172,8 +174,8 @@ CS_Sink* CS_EnumerateSourceSinks(CS_Source source, int* count,
|
||||
CS_Status* status) {
|
||||
wpi::SmallVector<CS_Sink, 32> buf;
|
||||
auto handles = cs::EnumerateSourceSinks(source, buf, status);
|
||||
CS_Sink* sinks =
|
||||
static_cast<CS_Sink*>(std::malloc(handles.size() * sizeof(CS_Sink)));
|
||||
CS_Sink* sinks = static_cast<CS_Sink*>(
|
||||
wpi::CheckedMalloc(handles.size() * sizeof(CS_Sink)));
|
||||
*count = handles.size();
|
||||
std::copy(handles.begin(), handles.end(), sinks);
|
||||
return sinks;
|
||||
@@ -323,8 +325,8 @@ void CS_SetDefaultLogger(unsigned int min_level) {
|
||||
CS_Source* CS_EnumerateSources(int* count, CS_Status* status) {
|
||||
wpi::SmallVector<CS_Source, 32> buf;
|
||||
auto handles = cs::EnumerateSourceHandles(buf, status);
|
||||
CS_Source* sources =
|
||||
static_cast<CS_Source*>(std::malloc(handles.size() * sizeof(CS_Source)));
|
||||
CS_Source* sources = static_cast<CS_Source*>(
|
||||
wpi::CheckedMalloc(handles.size() * sizeof(CS_Source)));
|
||||
*count = handles.size();
|
||||
std::copy(handles.begin(), handles.end(), sources);
|
||||
return sources;
|
||||
@@ -342,8 +344,8 @@ void CS_ReleaseEnumeratedSources(CS_Source* sources, int count) {
|
||||
CS_Sink* CS_EnumerateSinks(int* count, CS_Status* status) {
|
||||
wpi::SmallVector<CS_Sink, 32> buf;
|
||||
auto handles = cs::EnumerateSinkHandles(buf, status);
|
||||
CS_Sink* sinks =
|
||||
static_cast<CS_Sink*>(std::malloc(handles.size() * sizeof(CS_Sink)));
|
||||
CS_Sink* sinks = static_cast<CS_Sink*>(
|
||||
wpi::CheckedMalloc(handles.size() * sizeof(CS_Sink)));
|
||||
*count = handles.size();
|
||||
std::copy(handles.begin(), handles.end(), sinks);
|
||||
return sinks;
|
||||
@@ -378,8 +380,8 @@ char* CS_GetHostname() { return cs::ConvertToC(cs::GetHostname()); }
|
||||
|
||||
char** CS_GetNetworkInterfaces(int* count) {
|
||||
auto interfaces = cs::GetNetworkInterfaces();
|
||||
char** out =
|
||||
static_cast<char**>(std::malloc(interfaces.size() * sizeof(char*)));
|
||||
char** out = static_cast<char**>(
|
||||
wpi::CheckedMalloc(interfaces.size() * sizeof(char*)));
|
||||
*count = interfaces.size();
|
||||
for (size_t i = 0; i < interfaces.size(); ++i)
|
||||
out[i] = cs::ConvertToC(interfaces[i]);
|
||||
|
||||
Reference in New Issue
Block a user