diff --git a/common b/common index 7fcce77dd..0f179b266 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 7fcce77ddb6ae13a6da87e13186d3ab2a05945eb +Subproject commit 0f179b26614289150dae091366943ab34014e069 diff --git a/lib/api/server.cpp b/lib/api/server.cpp index 52ab83643..1cb8b4d46 100644 --- a/lib/api/server.cpp +++ b/lib/api/server.cpp @@ -182,6 +182,8 @@ void Server::acceptNewSession() { int fd = ::accept(sd, nullptr, nullptr); auto s = new sessions::Socket(api, fd); + if (!s) + throw MemoryAllocationError(); pollfd pfd = { .fd = fd, diff --git a/lib/api/sessions/http.cpp b/lib/api/sessions/http.cpp index d529c8ca2..f61bb9c92 100644 --- a/lib/api/sessions/http.cpp +++ b/lib/api/sessions/http.cpp @@ -49,6 +49,9 @@ Http::Http(Api *a, lws *w) : throw RuntimeError("Invalid request"); uri = new char[hdrlen + 1]; + if (!uri) + throw MemoryAllocationError(); + lws_hdr_copy(wsi, uri, hdrlen + 1, options ? WSI_TOKEN_OPTIONS_URI : WSI_TOKEN_POST_URI); /* Parse request URI */ diff --git a/lib/formats/protobuf.cpp b/lib/formats/protobuf.cpp index 6fccd0787..514e8c7ea 100644 --- a/lib/formats/protobuf.cpp +++ b/lib/formats/protobuf.cpp @@ -57,13 +57,21 @@ int protobuf_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct unsigned psz; auto *pb_msg = new Villas__Node__Message; + if (!pb_msg) + throw MemoryAllocationError(); + villas__node__message__init(pb_msg); pb_msg->n_samples = cnt; pb_msg->samples = new Villas__Node__Sample*[pb_msg->n_samples]; + if (!pb_msg->samples) + throw MemoryAllocationError(); for (unsigned i = 0; i < pb_msg->n_samples; i++) { Villas__Node__Sample *pb_smp = pb_msg->samples[i] = new Villas__Node__Sample; + if (!pb_msg->samples[i]) + throw MemoryAllocationError(); + villas__node__sample__init(pb_smp); struct sample *smp = smps[i]; @@ -77,6 +85,9 @@ int protobuf_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct if (io->flags & smp->flags & (int) SampleFlags::HAS_TS_ORIGIN) { pb_smp->timestamp = new Villas__Node__Timestamp; + if (!pb_smp->timestamp) + throw MemoryAllocationError(); + villas__node__timestamp__init(pb_smp->timestamp); pb_smp->timestamp->sec = smp->ts.origin.tv_sec; @@ -85,9 +96,14 @@ int protobuf_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct pb_smp->n_values = smp->length; pb_smp->values = new Villas__Node__Value*[pb_smp->n_values]; + if (pb_msg->values) + throw MemoryAllocationError(); for (unsigned j = 0; j < pb_smp->n_values; j++) { Villas__Node__Value *pb_val = pb_smp->values[j] = new Villas__Node__Value; + if (!pb_smp->values[i]) + throw MemoryAllocationError(); + villas__node__value__init(pb_val); enum SignalType fmt = sample_format(smp, j); @@ -110,6 +126,8 @@ int protobuf_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct case SignalType::COMPLEX: pb_val->value_case = VILLAS__NODE__VALUE__VALUE_Z; pb_val->z = new Villas__Node__Complex; + if (!pb_val->z) + throw MemoryAllocationError(); villas__node__complex__init(pb_val->z); diff --git a/lib/hooks/dp.cpp b/lib/hooks/dp.cpp index 757283019..416ccb157 100644 --- a/lib/hooks/dp.cpp +++ b/lib/hooks/dp.cpp @@ -197,7 +197,7 @@ public: fharmonics = new int[fharmonics_len]; coeffs = new std::complex[fharmonics_len]; if (!fharmonics || !coeffs) - throw RuntimeError("Failed to allocate memory"); + throw MemoryAllocationError(); json_array_foreach(json_harmonics, i, json_harmonic) { if (!json_is_integer(json_harmonic)) diff --git a/lib/hooks/jitter_calc.cpp b/lib/hooks/jitter_calc.cpp index 96ea09950..1627f4108 100644 --- a/lib/hooks/jitter_calc.cpp +++ b/lib/hooks/jitter_calc.cpp @@ -60,6 +60,9 @@ public: moving_avg = new int64_t[sz]; moving_var = new int64_t[sz]; + if (!jitter_val || !delay_series || !moving_avg || !moving_var) + throw MemoryAllocationError(); + memset(jitter_val, 0, sz); memset(delay_series, 0, sz); memset(moving_avg, 0, sz); diff --git a/lib/hooks/stats.cpp b/lib/hooks/stats.cpp index 9e44e1e62..16674f578 100644 --- a/lib/hooks/stats.cpp +++ b/lib/hooks/stats.cpp @@ -126,6 +126,9 @@ public: readHook = new StatsReadHook(this, p, n, fl, prio, en); writeHook = new StatsWriteHook(this, p, n, fl, prio, en); + if (!readHook || !writeHook) + throw MemoryAllocationError(); + if (node) { vlist_push(&node->in.hooks, (void *) readHook); vlist_push(&node->out.hooks, (void *) writeHook); diff --git a/lib/io.cpp b/lib/io.cpp index 887719f0a..77a996ee7 100644 --- a/lib/io.cpp +++ b/lib/io.cpp @@ -30,7 +30,9 @@ #include #include #include +#include +using namespace villas; using namespace villas::utils; static int io_print_lines(struct io *io, struct sample *smps[], unsigned cnt) @@ -89,6 +91,8 @@ int io_init(struct io *io, const struct format_type *fmt, struct vlist *signals, io->_vt = fmt; io->_vd = new char[fmt->size]; + if (!io->_vd) + throw MemoryAllocationError(); io->flags = flags | (io_type(io)->flags & ~(int) SampleFlags::HAS_ALL); io->delimiter = io_type(io)->delimiter ? io_type(io)->delimiter : '\n'; @@ -100,6 +104,9 @@ int io_init(struct io *io, const struct format_type *fmt, struct vlist *signals, io->in.buffer = new char[io->in.buflen]; io->out.buffer = new char[io->out.buflen]; + if (!io->in.buffer || !io->out.buffer) + throw MemoryAllocationError(); + io->signals = signals; ret = io_type(io)->init ? io_type(io)->init(io) : 0; @@ -117,6 +124,8 @@ int io_init2(struct io *io, const struct format_type *fmt, const char *dt, int f struct vlist *signals; signals = new struct vlist; + if (!signals) + throw MemoryAllocationError(); ret = vlist_init(signals); if (ret) diff --git a/lib/kernel/if.cpp b/lib/kernel/if.cpp index e2d2eb9bc..fd7b21780 100644 --- a/lib/kernel/if.cpp +++ b/lib/kernel/if.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -38,6 +39,7 @@ #include +using namespace villas; using namespace villas::utils; int if_init(struct interface *i, struct rtnl_link *link) @@ -162,7 +164,7 @@ struct interface * if_get_egress(struct sockaddr *sa, struct vlist *interfaces) /* If not found, create a new interface */ i = new struct interface; if (!i) - return nullptr; + throw MemoryAllocationError(); ret = if_init(i, link); if (ret) diff --git a/lib/kernel/tc_netem.cpp b/lib/kernel/tc_netem.cpp index 2dc18f292..6e17f830d 100644 --- a/lib/kernel/tc_netem.cpp +++ b/lib/kernel/tc_netem.cpp @@ -33,6 +33,7 @@ #include #include #include +#include using namespace villas; using namespace villas::utils; @@ -273,6 +274,8 @@ int tc_netem_set_delay_distribution(struct rtnl_qdisc *qdisc, json_t *json) size_t len = json_array_size(json); int16_t *data = new int16_t[len]; + if (!data) + throw MemoryAllocationError(); json_array_foreach(json, idx, elm) { if (!json_is_integer(elm)) diff --git a/lib/mapping.cpp b/lib/mapping.cpp index d1f90c61e..994154766 100644 --- a/lib/mapping.cpp +++ b/lib/mapping.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -217,6 +218,8 @@ int mapping_list_parse(struct vlist *ml, json_t *cfg, struct vlist *nodes) json_array_foreach(json_mapping, i, json_entry) { auto *me = new struct mapping_entry; + if (!me) + throw MemoryAllocationError(); ret = mapping_parse(me, json_entry, nodes); if (ret) diff --git a/lib/memory/heap.cpp b/lib/memory/heap.cpp index e04b87bb9..2f929cc92 100644 --- a/lib/memory/heap.cpp +++ b/lib/memory/heap.cpp @@ -24,7 +24,9 @@ #include #include +#include +using namespace villas; using namespace villas::utils; static struct memory_allocation * memory_heap_alloc(size_t len, size_t alignment, struct memory_type *m) @@ -33,7 +35,7 @@ static struct memory_allocation * memory_heap_alloc(size_t len, size_t alignment auto *ma = new struct memory_allocation; if (!ma) - return nullptr; + throw MemoryAllocationError(); ma->alignment = alignment; ma->type = m; diff --git a/lib/memory/ib.cpp b/lib/memory/ib.cpp index 14c126c6e..de391c39d 100644 --- a/lib/memory/ib.cpp +++ b/lib/memory/ib.cpp @@ -46,7 +46,7 @@ static struct memory_allocation * memory_ib_alloc(size_t len, size_t alignment, auto *ma = new struct memory_allocation; if (!ma) - return nullptr; + throw MemoryAllocationError(); ma->type = m; ma->length = len; diff --git a/lib/memory/managed.cpp b/lib/memory/managed.cpp index 1c06b1124..db08b34e2 100644 --- a/lib/memory/managed.cpp +++ b/lib/memory/managed.cpp @@ -33,7 +33,9 @@ #include #include #include +#include +using namespace villas; using namespace villas::utils; static struct memory_allocation * memory_managed_alloc(size_t len, size_t alignment, struct memory_type *m) @@ -108,7 +110,7 @@ static struct memory_allocation * memory_managed_alloc(size_t len, size_t alignm auto *ma = new struct memory_allocation; if (!ma) - return nullptr; + throw MemoryAllocationError(); ma->address = cptr; ma->type = m; diff --git a/lib/memory/mmap.cpp b/lib/memory/mmap.cpp index d6b47135c..74ff0728b 100644 --- a/lib/memory/mmap.cpp +++ b/lib/memory/mmap.cpp @@ -39,7 +39,9 @@ #include #include #include +#include +using namespace villas; using namespace villas; using namespace villas::utils; @@ -104,7 +106,7 @@ static struct memory_allocation * memory_mmap_alloc(size_t len, size_t alignment auto *ma = new struct memory_allocation; if (!ma) - return nullptr; + throw MemoryAllocationError(); if (m->flags & (int) MemoryFlags::HUGEPAGE) { #ifdef __linux__ diff --git a/lib/node.cpp b/lib/node.cpp index ca458c481..3703f835a 100644 --- a/lib/node.cpp +++ b/lib/node.cpp @@ -55,7 +55,7 @@ int node_init(struct node *n, struct node_type *vt) n->_vt = vt; n->_vd = new char[vt->size]; if (!n->_vd) - throw RuntimeError("Failed to allocate memory"); + throw MemoryAllocationError(); memset(n->_vd, 0, vt->size); diff --git a/lib/nodes/comedi.cpp b/lib/nodes/comedi.cpp index 287c0f550..22ea7452d 100644 --- a/lib/nodes/comedi.cpp +++ b/lib/nodes/comedi.cpp @@ -73,10 +73,12 @@ static int comedi_parse_direction(struct comedi *c, struct comedi_direction *d, } d->chanlist = new unsigned int[d->chanlist_len]; - assert(d->chanlist != nullptr); + if (!d->chanlist) + throw MemoryAllocationError(); d->chanspecs = new comedi_chanspec[d->chanlist_len]; - assert(d->chanspecs != nullptr); + if (!d->chanspecs) + throw MemoryAllocationError(); json_array_foreach(json_chans, i, json_chan) { int num, range, aref; @@ -238,7 +240,8 @@ static int comedi_start_in(struct node *n) /* Be prepared to consume one entire buffer */ c->buf = new char[c->in.buffer_size]; c->bufptr = c->buf; - assert(c->bufptr != nullptr); + if (!c->buf) + throw MemoryAllocationError(); info("Compiled for kernel read() interface"); #else @@ -328,7 +331,8 @@ static int comedi_start_out(struct node *n) const size_t local_buffer_size = d->sample_size * d->chanlist_len; d->buffer = new char[local_buffer_size]; d->bufptr = d->buffer; - assert(d->buffer != nullptr); + if (!d->buffer) + throw MemoryAllocationError(); /* Initialize local buffer used for write() syscalls */ for (unsigned channel = 0; channel < d->chanlist_len; channel++) { diff --git a/lib/nodes/ethercat.cpp b/lib/nodes/ethercat.cpp index 02295de7b..9ff20f3e7 100644 --- a/lib/nodes/ethercat.cpp +++ b/lib/nodes/ethercat.cpp @@ -212,15 +212,15 @@ int ethercat_prepare(struct node *n) w->in.offsets = new unsigned[w->in.num_channels]; if (!w->in.offsets) - throw RuntimeError("Failed to allocate memory"); + throw MemoryAllocationError(); w->out.offsets = new unsigned[w->out.num_channels]; if (!w->out.offsets) - throw RuntimeError("Failed to allocate memory"); + throw MemoryAllocationError(); w->domain_regs = new ec_pdo_entry_reg_t[w->in.num_channels + w->out.num_channels + 1]; if (!w->domain_regs) - throw RuntimeError("Failed to allocate memory"); + throw MemoryAllocationError(); memset(w->domain_regs, 0, (w->in.num_channels + w->out.num_channels + 1) * sizeof(ec_pdo_entry_reg_t)); diff --git a/lib/nodes/exec.cpp b/lib/nodes/exec.cpp index 3507cb01c..b11201c68 100644 --- a/lib/nodes/exec.cpp +++ b/lib/nodes/exec.cpp @@ -220,6 +220,8 @@ int exec_write(struct node *n, struct sample *smps[], unsigned cnt, unsigned *re size_t wbytes; int ret; char *line = new char[1024]; + if (!line) + throw MemoryAllocationError(); ret = io_sprint(&e->io, line, 1024, &wbytes, smps, cnt); if (ret < 0) diff --git a/lib/nodes/file.cpp b/lib/nodes/file.cpp index 1d5a2a8f8..c2a608ab3 100644 --- a/lib/nodes/file.cpp +++ b/lib/nodes/file.cpp @@ -34,12 +34,15 @@ #include #include +using namespace villas; using namespace villas::utils; static char * file_format_name(const char *format, struct timespec *ts) { struct tm tm; char *buf = new char[FILE_MAX_PATHLEN]; + if (!buf) + throw MemoryAllocationError(); /* Convert time */ gmtime_r(&ts->tv_sec, &tm); diff --git a/lib/nodes/iec61850.cpp b/lib/nodes/iec61850.cpp index d88026c55..00f9825d5 100644 --- a/lib/nodes/iec61850.cpp +++ b/lib/nodes/iec61850.cpp @@ -295,7 +295,7 @@ struct iec61850_receiver * iec61850_receiver_create(enum iec61850_receiver::Type if (!r) { r = new struct iec61850_receiver; if (!r) - return nullptr; + throw MemoryAllocationError(); r->interface = strdup(intf); r->type = t; diff --git a/lib/nodes/infiniband.cpp b/lib/nodes/infiniband.cpp index b6e00b1b3..dc17dbc5b 100644 --- a/lib/nodes/infiniband.cpp +++ b/lib/nodes/infiniband.cpp @@ -656,6 +656,9 @@ int ib_start(struct node *n) /* Allocate space for 40 Byte GHR. We don't use this. */ if (ib->conn.port_space == RDMA_PS_UDP) { ib->conn.ud.grh_ptr = new char[GRH_SIZE]; + if (!ib->conn.ud.grh_ptr) + throw MemoryAllocationError(); + ib->conn.ud.grh_mr = ibv_reg_mr(ib->ctx.pd, ib->conn.ud.grh_ptr, GRH_SIZE, IBV_ACCESS_LOCAL_WRITE); } diff --git a/lib/nodes/ngsi.cpp b/lib/nodes/ngsi.cpp index b9aa4aae0..916681059 100644 --- a/lib/nodes/ngsi.cpp +++ b/lib/nodes/ngsi.cpp @@ -30,10 +30,12 @@ #include #include +#include #include #include #include +using namespace villas; using namespace villas::utils; /* Some global settings */ @@ -218,6 +220,8 @@ static int ngsi_parse_mapping(struct vlist *mapping, json_t *cfg) return -2; auto *a = new struct ngsi_attribute; + if (!a) + throw MemoryAllocationError(); a->index = j; diff --git a/lib/nodes/opal.cpp b/lib/nodes/opal.cpp index 1125d68c3..d6d1e9cad 100644 --- a/lib/nodes/opal.cpp +++ b/lib/nodes/opal.cpp @@ -83,6 +83,9 @@ int opal_type_start(villas::node::SuperNode *sn) send_ids = new int[send_icons]; recv_ids = new int[recv_icons]; + if (!send_ids || !recv_ids) + throw MemoryAllocationError(); + err = OpalGetAsyncSendIDList(send_ids, send_icons * sizeof(int)); if (err != EOK) error("Failed to get list of send ids (%d)", err); @@ -128,6 +131,9 @@ int opal_print_global() auto *sbuf = new char[send_icons * 5]; auto *rbuf = new char[recv_icons * 5]; + if (!sbuf || !rbuf) + throw MemoryAllocationError(); + for (int i = 0; i < send_icons; i++) strcatf(&sbuf, "%u ", send_ids[i]); for (int i = 0; i < recv_icons; i++) diff --git a/lib/nodes/rtp.cpp b/lib/nodes/rtp.cpp index 49958399a..14c6cf2bc 100644 --- a/lib/nodes/rtp.cpp +++ b/lib/nodes/rtp.cpp @@ -359,6 +359,9 @@ int rtp_start(struct node *n) return -1; } + if (!r->aimd.rate_hook) + throw MemoryAllocationError(); + r->aimd.rate_hook->init(); vlist_push(&n->out.hooks, (void *) r->aimd.rate_hook); @@ -396,6 +399,8 @@ int rtp_start(struct node *n) strftime(fn, sizeof(fn), r->aimd.log_filename, &tm); r->aimd.log = new std::ofstream(fn, std::ios::out | std::ios::trunc); + if (!r->aimd.log) + throw MemoryAllocationError(); *(r->aimd.log) << "# cnt\tfrac_loss\trate" << std::endl; } diff --git a/lib/nodes/shmem.cpp b/lib/nodes/shmem.cpp index 60a98c42d..4d4fd8c60 100644 --- a/lib/nodes/shmem.cpp +++ b/lib/nodes/shmem.cpp @@ -30,12 +30,14 @@ #include #include +#include #include #include #include #include #include +using namespace villas; using namespace villas::utils; int shmem_parse(struct node *n, json_t *cfg) @@ -81,6 +83,8 @@ int shmem_parse(struct node *n, json_t *cfg) error("Setting 'exec' of node %s must be an array of strings", node_name(n)); shm->exec = new char*[json_array_size(json_exec) + 1]; + if (!shm->exec) + throw MemoryAllocationError(); size_t i; json_t *json_val; diff --git a/lib/nodes/signal_generator.cpp b/lib/nodes/signal_generator.cpp index c5c2e2032..61ec53e7d 100644 --- a/lib/nodes/signal_generator.cpp +++ b/lib/nodes/signal_generator.cpp @@ -26,8 +26,10 @@ #include #include +#include #include +using namespace villas; using namespace villas::utils; static enum signal_generator::SignalType signal_generator_lookup_type(const char *type) @@ -151,6 +153,8 @@ int signal_generator_start(struct node *n) s->counter = 0; s->started = time_now(); s->last = new double[s->values]; + if (!s->last) + throw MemoryAllocationError(); for (unsigned i = 0; i < s->values; i++) s->last[i] = s->offset; diff --git a/lib/nodes/socket.cpp b/lib/nodes/socket.cpp index 01320755e..d0e09a1b5 100644 --- a/lib/nodes/socket.cpp +++ b/lib/nodes/socket.cpp @@ -47,6 +47,7 @@ /* Forward declartions */ static struct plugin p; +using namespace villas; using namespace villas::node; using namespace villas::utils; @@ -276,13 +277,12 @@ int socket_start(struct node *n) s->out.buflen = SOCKET_INITIAL_BUFFER_LEN; s->out.buf = new char[s->out.buflen]; if (!s->out.buf) - return -1; + throw MemoryAllocationError(); s->in.buflen = SOCKET_INITIAL_BUFFER_LEN; s->in.buf = new char[s->in.buflen]; if (!s->in.buf) - return -1; - + throw MemoryAllocationError(); return 0; } @@ -408,6 +408,8 @@ retry: ret = io_sprint(&s->io, s->out.buf, s->out.buflen, &wbytes, smps, cnt); delete[] s->out.buf; s->out.buf = new char[s->out.buflen]; + if (!s->out.buf) + throw MemoryAllocationError(); goto retry; } diff --git a/lib/nodes/stats.cpp b/lib/nodes/stats.cpp index 87c5401a9..90a76895b 100644 --- a/lib/nodes/stats.cpp +++ b/lib/nodes/stats.cpp @@ -181,7 +181,7 @@ int stats_node_parse(struct node *n, json_t *cfg) stats_sig = new struct stats_node_signal; if (!stats_sig) - return -1; + throw MemoryAllocationError(); ret = stats_node_signal_parse(stats_sig, json_signal); if (ret) diff --git a/lib/nodes/test_rtt.cpp b/lib/nodes/test_rtt.cpp index 440d0cd93..b8de6188a 100644 --- a/lib/nodes/test_rtt.cpp +++ b/lib/nodes/test_rtt.cpp @@ -31,6 +31,7 @@ #include #include +using namespace villas; using namespace villas::utils; static struct plugin p; @@ -103,6 +104,8 @@ int test_rtt_prepare(struct node *n) max_values = c->values; c->filename_formatted = new char[NAME_MAX]; + if (!c->filename_formatted) + throw MemoryAllocationError(); strftime(c->filename_formatted, NAME_MAX, c->filename, &tm); } @@ -220,6 +223,8 @@ int test_rtt_parse(struct node *n, json_t *cfg) for (int i = 0; i < numrates; i++) { for (int j = 0; j < numvalues; j++) { auto *c = new struct test_rtt_case; + if (!c) + throw MemoryAllocationError(); c->rate = rates[i]; c->values = values[j]; diff --git a/lib/nodes/uldaq.cpp b/lib/nodes/uldaq.cpp index 4fc656f43..44c1b9574 100644 --- a/lib/nodes/uldaq.cpp +++ b/lib/nodes/uldaq.cpp @@ -316,6 +316,8 @@ int uldaq_parse(struct node *n, json_t *cfg) u->in.channel_count = vlist_length(&n->in.signals); u->in.queues = new struct AiQueueElement[u->in.channel_count]; + if (!u->in.queues) + throw MemoryAllocationError(); json_array_foreach(json_signals, i, json_signal) { const char *range_str = nullptr, *input_mode_str = nullptr; @@ -525,10 +527,8 @@ int uldaq_start(struct node *n) /* Allocate a buffer to receive the data */ u->in.buffer_len = u->in.channel_count * n->in.vectorize * 50; u->in.buffer = new double[u->in.buffer_len]; - if (!u->in.buffer) { - warning("Out of memory, unable to create scan buffer"); - return -1; - } + if (!u->in.buffer) + throw MemoryAllocationError(); ret = uldaq_connect(n); if (ret) diff --git a/lib/nodes/websocket.cpp b/lib/nodes/websocket.cpp index 6e56ceb2d..f2315d1a9 100644 --- a/lib/nodes/websocket.cpp +++ b/lib/nodes/websocket.cpp @@ -101,6 +101,9 @@ static int websocket_connection_init(struct websocket_connection *c) c->buffers.recv = new Buffer(DEFAULT_WEBSOCKET_BUFFER_SIZE); c->buffers.send = new Buffer(DEFAULT_WEBSOCKET_BUFFER_SIZE); + if (!c->buffers.recv || !c->buffers.send) + throw MemoryAllocationError(); + c->state = websocket_connection::State::INITIALIZED; return 0; @@ -393,6 +396,8 @@ int websocket_start(struct node *n) const char *format; auto *d = (struct websocket_destination *) vlist_at(&w->destinations, i); auto *c = new struct websocket_connection; + if (!c) + throw MemoryAllocationError(); c->state = websocket_connection::State::CONNECTING; @@ -546,6 +551,10 @@ int websocket_parse(struct node *n, json_t *cfg) error("The 'destinations' setting of node %s must be an array of URLs", node_name(n)); auto *d = new struct websocket_destination; + if (!d) + throw MemoryAllocationError(); + + memset(d, 0, sizeof(struct websocket_destination)); d->uri = strdup(uri); diff --git a/lib/path.cpp b/lib/path.cpp index ea2807f34..b09e6e5b0 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -276,6 +276,8 @@ int path_prepare(struct vpath *p) /* For other mappings we create new signal descriptors */ else { sig = new struct signal; + if (!sig) + throw MemoryAllocationError(); ret = signal_init_from_mapping(sig, me, j); if (ret) @@ -383,6 +385,8 @@ int path_parse(struct vpath *p, json_t *cfg, struct vlist *nodes) /* Create new path_source of not existing */ if (!ps) { ps = new struct vpath_source; + if (!ps) + throw MemoryAllocationError(); ps->node = me->node; ps->masked = false; @@ -407,6 +411,8 @@ int path_parse(struct vpath *p, json_t *cfg, struct vlist *nodes) throw ConfigError(cfg, "node-config-path", "Every node must only be used by a single path as destination"); auto *pd = new struct vpath_destination; + if (!pd) + throw MemoryAllocationError(); pd->node = n; pd->node->output_path = p; diff --git a/lib/signal.cpp b/lib/signal.cpp index e4d433648..1dfc17f00 100644 --- a/lib/signal.cpp +++ b/lib/signal.cpp @@ -27,6 +27,7 @@ #include #include #include +#include using namespace villas; using namespace villas::utils; @@ -108,7 +109,7 @@ struct signal * signal_create(const char *name, const char *unit, enum SignalTyp sig = new struct signal; if (!sig) - return nullptr; + throw MemoryAllocationError(); ret = signal_init(sig); if (ret) @@ -160,7 +161,7 @@ struct signal * signal_copy(struct signal *s) ns = new struct signal; if (!ns) - return nullptr; + throw MemoryAllocationError(); signal_init(ns); @@ -256,7 +257,7 @@ int signal_list_parse(struct vlist *list, json_t *cfg) json_array_foreach(cfg, i, json_signal) { s = new struct signal; if (!s) - return -1; + throw MemoryAllocationError(); ret = signal_init(s); if (ret) diff --git a/lib/socket_addr.cpp b/lib/socket_addr.cpp index a68c6e23b..56f60a5d8 100644 --- a/lib/socket_addr.cpp +++ b/lib/socket_addr.cpp @@ -27,17 +27,21 @@ #include #include +#include #ifdef WITH_SOCKET_LAYER_ETH #include #endif /* WITH_SOCKET_LAYER_ETH */ +using namespace villas; using namespace villas::utils; char * socket_print_addr(struct sockaddr *saddr) { union sockaddr_union *sa = (union sockaddr_union *) saddr; char *buf = new char[64]; + if (!buf) + throw MemoryAllocationError(); /* Address */ switch (sa->sa.sa_family) { diff --git a/lib/super_node.cpp b/lib/super_node.cpp index 326d7cba0..ef19a9f6b 100644 --- a/lib/super_node.cpp +++ b/lib/super_node.cpp @@ -153,7 +153,7 @@ void SuperNode::parse(json_t *cfg) auto *n = new struct node; if (!n) - throw RuntimeError("Failed to allocate memory"); + throw MemoryAllocationError(); ret = node_init(n, nt); if (ret) @@ -179,6 +179,8 @@ void SuperNode::parse(json_t *cfg) json_t *json_path; json_array_foreach(json_paths, i, json_path) { parse: auto *p = new vpath; + if (!p) + throw MemoryAllocationError(); ret = path_init(p); if (ret) diff --git a/src/villas-hook.cpp b/src/villas-hook.cpp index e93dde558..bf18f8276 100644 --- a/src/villas-hook.cpp +++ b/src/villas-hook.cpp @@ -197,6 +197,8 @@ check: if (optarg == endptr) throw RuntimeError("Vectorize option must be greater than 0"); smps = new struct sample*[cnt]; + if (!smps) + throw MemoryAllocationError(); ret = pool_init(&p, 10 * cnt, SAMPLE_LENGTH(DEFAULT_SAMPLE_LENGTH)); if (ret) diff --git a/src/villas-relay.cpp b/src/villas-relay.cpp index d0c6ccb16..9f168d16e 100644 --- a/src/villas-relay.cpp +++ b/src/villas-relay.cpp @@ -88,7 +88,11 @@ RelaySession * RelaySession::get(lws *wsi) auto it = sessions.find(sid); if (it == sessions.end()) { - return new RelaySession(sid); + auto *rs = new RelaySession(sid); + if (!rs) + throw MemoryAllocationError(); + + return rs; } else { logger->info("Found existing session: {}", sid); diff --git a/src/villas-test-cmp.cpp b/src/villas-test-cmp.cpp index 24f3ee046..2da183486 100644 --- a/src/villas-test-cmp.cpp +++ b/src/villas-test-cmp.cpp @@ -220,8 +220,13 @@ check: if (optarg == endptr) /* Open files */ std::vector sides; - for (auto filename : filenames) - sides.push_back(new TestCmpSide(filename, fmt, dtypes, &pool)); + for (auto filename : filenames) { + auto *s = new TestCmpSide(filename, fmt, dtypes, &pool); + if (!s) + throw MemoryAllocationError(); + + sides.push_back(s); + } line = 0; for (;;) { diff --git a/src/villas-test-rtt.cpp b/src/villas-test-rtt.cpp index 5e046297b..0ac08eb2c 100644 --- a/src/villas-test-rtt.cpp +++ b/src/villas-test-rtt.cpp @@ -174,6 +174,9 @@ check: if (optarg == endptr) struct sample *smp_send = (struct sample *) new char[SAMPLE_LENGTH(2)]; struct sample *smp_recv = (struct sample *) new char[SAMPLE_LENGTH(2)]; + if (!smp_send || !smp_recv) + throw MemoryAllocationError(); + struct node *node; if (!uri.empty()) diff --git a/tests/unit/mapping.cpp b/tests/unit/mapping.cpp index f5cd05709..c5997576e 100644 --- a/tests/unit/mapping.cpp +++ b/tests/unit/mapping.cpp @@ -47,6 +47,7 @@ Test(mapping, parse_nodes) for (unsigned i = 0; i < ARRAY_LEN(node_names); i++) { struct node *n = new struct node; + cr_assert_not_null(n); n->name = strdup(node_names[i]);