Commit 0f179b26 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

add more nullptr checks after memory allocations

parent 7fcce77d
......@@ -50,7 +50,7 @@ public:
setp = CPU_ALLOC(num_cpus);
if (!setp)
throw villas::RuntimeError("Failed to allocated memory");
throw MemoryAllocationError();
zero();
}
......
......@@ -62,6 +62,14 @@ public:
{ }
};
class MemoryAllocationError : public RuntimeError {
public:
MemoryAllocationError() :
RuntimeError("Failed to allocate memory")
{ }
};
class JsonError : public std::runtime_error {
protected:
......
......@@ -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));
......
......@@ -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);
}
......
......@@ -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);
......
......@@ -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;
......
......@@ -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;
......
......@@ -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);
......
......@@ -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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment