1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

add more nullptr checks after memory allocations

This commit is contained in:
Steffen Vogel 2020-07-04 16:20:21 +02:00 committed by Steffen Vogel
parent 34b7ae66db
commit 06c0bd8870
9 changed files with 31 additions and 11 deletions

View file

@ -50,7 +50,7 @@ public:
setp = CPU_ALLOC(num_cpus);
if (!setp)
throw villas::RuntimeError("Failed to allocated memory");
throw MemoryAllocationError();
zero();
}

View file

@ -62,6 +62,14 @@ public:
{ }
};
class MemoryAllocationError : public RuntimeError {
public:
MemoryAllocationError() :
RuntimeError("Failed to allocate memory")
{ }
};
class JsonError : public std::runtime_error {
protected:

View file

@ -211,7 +211,7 @@ AFILE * afopen(const char *uri, const char *mode)
AFILE *af = new AFILE;
if (!af)
throw RuntimeError("Failed to allocate memory!");
throw MemoryAllocationError();
memset(af, 0, sizeof(AFILE));

View file

@ -35,7 +35,7 @@ Buffer::Buffer(size_t sz) :
{
buf = new char[size];
if (!buf)
throw RuntimeError("Failed to allocate memory");
throw MemoryAllocationError();
memset(buf, 0, size);
}

View file

@ -190,7 +190,7 @@ char * Hist::dump() const
{
char *buf = new char[128];
if (!buf)
throw RuntimeError("Failed to allocate memory!");
throw MemoryAllocationError();
memset(buf, 0, 128);

View file

@ -144,7 +144,7 @@ Container::~Container()
std::shared_ptr<Container>
Container::create()
{
std::shared_ptr<Container> container { new Container };
std::shared_ptr<Container> container(new Container);
return container;
}
@ -549,6 +549,9 @@ Device::pciHotReset()
sizeof(struct vfio_pci_dependent_device) * 64;
auto *reset_info_buf = new char[reset_info_len];
if (!reset_info_buf)
throw MemoryAllocationError();
auto *reset_info = reinterpret_cast<struct vfio_pci_hot_reset_info *>(reset_info_buf);
reset_info->argsz = reset_info_len;
@ -577,6 +580,9 @@ Device::pciHotReset()
const size_t reset_len = sizeof(struct vfio_pci_hot_reset) +
sizeof(int32_t) * 1;
auto *reset_buf = new char[reset_len];
if (!reset_buf)
throw MemoryAllocationError();
auto *reset = reinterpret_cast<struct vfio_pci_hot_reset*>(reset_buf);
reset->argsz = reset_len;
@ -609,9 +615,10 @@ Device::pciMsiInit(int efds[])
sizeof(int) * irqCount;
auto *irqSetBuf = new char[irqSetSize];
if (!irqSetBuf)
throw MemoryAllocationError();
auto *irqSet = reinterpret_cast<struct vfio_irq_set*>(irqSetBuf);
if (irqSet == nullptr)
return -1;
irqSet->argsz = irqSetSize;
irqSet->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
@ -653,9 +660,10 @@ Device::pciMsiDeinit(int efds[])
sizeof(int) * irqCount;
auto *irqSetBuf = new char[irqSetSize];
if (!irqSetBuf)
throw MemoryAllocationError();
auto *irqSet = reinterpret_cast<struct vfio_irq_set*>(irqSetBuf);
if (irqSet == nullptr)
return -1;
irqSet->argsz = irqSetSize;
irqSet->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;

View file

@ -24,9 +24,11 @@
#include <limits>
#include <cstdint>
#include <villas/exceptions.hpp>
#include <villas/utils.hpp>
#include <villas/memory_manager.hpp>
using namespace villas;
using namespace villas::utils;
namespace villas {
@ -39,6 +41,8 @@ MemoryManager::get()
{
if (instance == nullptr) {
instance = new MemoryManager;
if (!instance)
throw MemoryAllocationError();
}
return *instance;

View file

@ -262,7 +262,7 @@ void * memdup(const void *src, size_t bytes)
{
void *dst = new char[bytes];
if (!dst)
throw RuntimeError("Failed to allocate memory!");
throw MemoryAllocationError();
memcpy(dst, src, bytes);

View file

@ -48,7 +48,7 @@ Test(list, vlist_lookup)
for (unsigned i = 0; i < ARRAY_LEN(nouns); i++) {
struct data *d = new struct data;
if (!d)
throw RuntimeError("Failed to allocate memory!");
throw MemoryAllocationError();
d->tag = nouns[i];
d->data = i;