From 1b33e2d10461b6577718f7eb566674ad756d1caa Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 27 Jan 2020 12:17:57 +0100 Subject: [PATCH] handle failed memory allocations --- common/lib/kernel/vfio_legacy.cpp | 10 ++++++++++ common/lib/utils.cpp | 3 +++ common/tests/unit/list.cpp | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/common/lib/kernel/vfio_legacy.cpp b/common/lib/kernel/vfio_legacy.cpp index f2f9a769a..3bc58ddb6 100644 --- a/common/lib/kernel/vfio_legacy.cpp +++ b/common/lib/kernel/vfio_legacy.cpp @@ -285,6 +285,9 @@ int vfio_device_attach(struct vfio_device *d, struct vfio_container *c, const ch d->regions = new struct vfio_region_info[d->info.num_regions]; d->mappings = new void *[d->info.num_regions]; + if (!d->irqs || !d->regions || !d->mappsings) + throw RuntimeError("Failed to allocate memory!"); + /* Get device regions */ for (int i = 0; i < d->info.num_regions && i < 8; i++) { struct vfio_region_info *region = &d->regions[i]; @@ -328,6 +331,9 @@ int vfio_pci_reset(struct vfio_device *d) struct vfio_pci_hot_reset_info *reset_info = new char[reset_info_len]; struct vfio_pci_hot_reset *reset = new char[reset_len]; + if (!reset_info || !reset) + throw RuntimeError("Failed to allocate memory!"); + reset_info->argsz = reset_info_len; reset->argsz = reset_len; @@ -405,6 +411,8 @@ int vfio_pci_msi_deinit(struct vfio_device *d, int efds[32]) irq_setlen = sizeof(struct vfio_irq_set) + sizeof(int) * irq_count; irq_set = new char[irq_setlen]; + if (!irq_set) + throw RuntimeError("Failed to allocate memory!"); irq_set->argsz = irq_setlen; irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER; @@ -439,6 +447,8 @@ int vfio_pci_msi_init(struct vfio_device *d, int efds[32]) irq_setlen = sizeof(struct vfio_irq_set) + sizeof(int) * irq_count; irq_set = new char[irq_setlen]; + if (!irq_set) + throw RuntimeError("Failed to allocate memory!"); irq_set->argsz = irq_setlen; irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER; diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index d5379ad72..e8b8878e3 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include static pthread_t main_thread; @@ -260,6 +261,8 @@ char * vstrf(const char *fmt, va_list va) void * memdup(const void *src, size_t bytes) { void *dst = new char[bytes]; + if (!dst) + throw RuntimeError("Failed to allocate memory!"); memcpy(dst, src, bytes); diff --git a/common/tests/unit/list.cpp b/common/tests/unit/list.cpp index 5aca1b828..65b08ad5a 100644 --- a/common/tests/unit/list.cpp +++ b/common/tests/unit/list.cpp @@ -24,9 +24,12 @@ #include #include +#include #include #include +using namespace villas; + static const char *nouns[] = { "time", "person", "year", "way", "day", "thing", "man", "world", "life", "hand", "part", "child", "eye", "woman", "place", "work", "week", "case", "point", "government", "company", "number", "group", "problem", "fact" }; struct data { @@ -45,6 +48,8 @@ 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!"); d->tag = nouns[i]; d->data = i;