Skip to content

Commit

Permalink
introducing memory functions that abort program on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
mkind committed Nov 26, 2017
1 parent 49a0ea9 commit 2681567
Show file tree
Hide file tree
Showing 29 changed files with 147 additions and 132 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ set(SPLONEBOX-SOURCES
src/address.c
src/address.h
src/sbmemzero.c
src/mem.c
src/mem.h
src/api/sb-api.h
src/api/register.c
src/api/result.c
Expand Down Expand Up @@ -176,6 +178,8 @@ set(SPLONEBOX-SOURCES
set(SB-PLUGINKEY-SOURCES
src/sb-pluginkey.c
src/sb-common.h
src/mem.c
src/mem.h
src/rpc/sb-rpc.h
src/util.c
src/confparse.c
Expand All @@ -187,6 +191,8 @@ set(SB-PLUGINKEY-SOURCES
# sb-makekey target sources
set(SB-MAKEKEY-SOURCES
src/sb-common.h
src/mem.c
src/mem.h
src/reallocarray.c
src/tweetnacl.c
src/tweetnacl.h
Expand Down Expand Up @@ -221,6 +227,8 @@ set(TEST-SOURCES
src/parse.h
src/util.c
src/util.h
src/mem.c
src/mem.h
src/address.c
src/address.h
src/sbmemzero.c
Expand Down
10 changes: 5 additions & 5 deletions src/confparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ bitarray_init_zero(unsigned int n_bits)
{
/* round up to the next int. */
size_t sz = (n_bits+BITARRAY_MASK) >> BITARRAY_SHIFT;
return (CALLOC(sz, unsigned int));
return calloc_or_die(sz, sizeof(unsigned int));
}

/** Free the bit array <b>ba</b>. */
Expand Down Expand Up @@ -243,7 +243,7 @@ STATIC void reset(const configformat *fmt, void *options, const configvar *var,
if (!use_defaults)
return; /* all done */
if (var->initvalue) {
c = CALLOC(1, configline);
c = calloc_or_die(1, sizeof(configline));
c->key = box_strdup(var->name);
c->value = box_strdup(var->initvalue);
if (assign_value(fmt, options, c) < 0) {
Expand Down Expand Up @@ -338,7 +338,7 @@ STATIC const char * unescape_string(const char *s, char **result,
}
}
end_of_loop:
out = *result = MALLOC_ARRAY((size_t)(cp-s + 1), char);
out = *result = malloc_array_or_die((size_t)(cp-s + 1), sizeof(char));
cp = s+1;
while (1) {
switch (*cp)
Expand Down Expand Up @@ -405,7 +405,7 @@ STATIC void line_append(configline **lst, const char *key, const char *val)
{
configline *newline;

newline = CALLOC(1, configline);
newline = calloc_or_die(1, sizeof(configline));
newline->key = box_strdup(key);
newline->value = box_strdup(val);
newline->next = NULL;
Expand Down Expand Up @@ -668,7 +668,7 @@ int confparse_get_lines(const char *string, configline **result, int extended)
/* This list can get long, so we keep a pointer to the end of it
* rather than using line_append over and over and getting
* n^2 performance. */
*next = CALLOC(1, configline);
*next = calloc_or_die(1, sizeof(configline));
(*next)->key = k;
(*next)->value = v;
(*next)->next = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
\
hashmap(T, U) *hashmap_##T##_##U##_new() \
{ \
hashmap(T, U) *rv = MALLOC(hashmap(T, U)); \
hashmap(T, U) *rv = malloc_or_die(sizeof(hashmap(T, U))); \
rv->table = kh_init(T##_##U##_map); \
return rv; \
} \
Expand Down
8 changes: 4 additions & 4 deletions src/klist.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
kmptype_t **buf; \
} kmp_##name##_t; \
static inline kmp_##name##_t *kmp_init_##name(void) { \
return CALLOC(1, kmp_##name##_t); \
return calloc_or_die(1, sizeof(kmp_##name##_t)); \
} \
static inline void kmp_destroy_##name(kmp_##name##_t *mp) { \
size_t k; \
Expand All @@ -44,15 +44,15 @@
static inline kmptype_t *kmp_alloc_##name(kmp_##name##_t *mp) { \
mp->cnt++; \
if (mp->n == 0) { \
return CALLOC(1, kmptype_t); \
return calloc_or_die(1, sizeof(kmptype_t)); \
} \
return mp->buf[--mp->n]; \
} \
static inline void kmp_free_##name(kmp_##name##_t *mp, kmptype_t *p) { \
mp->cnt--; \
if (mp->n == mp->max) { \
mp->max = mp->max ? (mp->max << 1) : 16; \
mp->buf = REALLOC_ARRAY(mp->buf, mp->max, kmptype_t *); \
mp->buf = realloc_array_or_die(mp->buf, mp->max, sizeof(kmptype_t *)); \
} \
mp->buf[mp->n++] = p; \
}
Expand All @@ -76,7 +76,7 @@
size_t size; \
} kl_##name##_t; \
static inline kl_##name##_t *kl_init_##name(void) { \
kl_##name##_t *kl = CALLOC(1, kl_##name##_t); \
kl_##name##_t *kl = calloc_or_die(1, sizeof(kl_##name##_t)); \
kl->mp = kmp_init(name); \
kl->head = kl->tail = kmp_alloc(name, kl->mp); \
kl->head->next = 0; \
Expand Down
47 changes: 47 additions & 0 deletions src/mem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdlib.h>

#include "sb-common.h"

void *malloc_or_die(size_t n)
{
void *p = malloc(n);
if (!p && 0 < n)
LOG_MEMERROR();
return p;
}


void *malloc_array_or_die(size_t nmemb, size_t n)
{
void *p = reallocarray(NULL, nmemb, n);
if (!p && 0 < n)
LOG_MEMERROR();
return p;
}


void *calloc_or_die(size_t nmemb, size_t n)
{
void *p = calloc(nmemb, n);
if (!p && 0 < n)
LOG_MEMERROR();
return p;
}


void *realloc_array_or_die(void *ptr, size_t nmemb, size_t n)
{
void *p = reallocarray(ptr, nmemb, n);
if (!p && 0 < n)
LOG_MEMERROR();
return p;
}


void *realloc_or_die(void *ptr, size_t n)
{
void *p = realloc(ptr, n);
if (!p && 0 < n)
LOG_MEMERROR();
return p;
}
20 changes: 20 additions & 0 deletions src/mem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <stdlib.h>

#define FREE(p) do { \
if (PREDICT_LIKELY((p)!=NULL)) \
free(p); \
p = NULL; \
} while (0)

#define MALLOC_TYPE(t) ((t)malloc_or_die(sizeof(t)))

void *reallocarray(void *optr, size_t nmemb, size_t size);

void *realloc_array_or_die(void *ptr, size_t nmemb, size_t n);
void *malloc_array_or_die(size_t nmemb, size_t n);
void *malloc_or_die(size_t n);
void *calloc_or_die(size_t nmemb, size_t n);
void *realloc_or_die(void *ptr, size_t n);

4 changes: 2 additions & 2 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ STATIC setoptionerror options_init_from_string(const char *cf)
int retval;
setoptionerror err = SETOPT_ERR_MISC;

newoptions = CALLOC(1, options);
newoptions = calloc_or_die(1, sizeof(options));
newoptions->magic_ = OR_OPTIONS_MAGIC;
options_init(newoptions);

Expand Down Expand Up @@ -212,7 +212,7 @@ STATIC char *load_boxrc_from_disk(void)
return (NULL);
}

string = MALLOC_ARRAY((size_t)(statbuf.st_size+1), char);
string = malloc_array_or_die((size_t)(statbuf.st_size+1), sizeof(char));
ssize_t r = filesystem_read_all(fd, string, (size_t)statbuf.st_size);

if (r < 0) {
Expand Down
18 changes: 5 additions & 13 deletions src/rpc/connection/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ int connection_create(uv_stream_t *stream)

stream->data = NULL;

struct connection *con = MALLOC(struct connection);

if (con == NULL)
return (-1);
struct connection *con = malloc_or_die(sizeof(struct connection));

con->id = next_con_id++;
con->msgid = 1;
Expand Down Expand Up @@ -228,7 +225,7 @@ STATIC void broadcast_event(char *name, array args)
con = kv_A(subscribed, i);

if (con->pending_requests) {
wbuffer *rv = MALLOC(wbuffer);
wbuffer *rv = malloc_or_die(sizeof(wbuffer));
rv->size = sbuf.size;
rv->data = sb_memdup_nulterm(sbuf.data, sbuf.size);
kv_push(con->delayed_notifications, rv);
Expand Down Expand Up @@ -443,12 +440,7 @@ STATIC void parse_cb(inputstream *istream, void *data, bool eof)
}

con->packet.end = con->packet.length;
con->packet.data = MALLOC_ARRAY(MAX(con->packet.end, read), unsigned char);

if (!con->packet.data) {
LOG_ERROR("Failed to alloc mem for con packet.");
goto end;
}
con->packet.data = malloc_array_or_die(MAX(con->packet.end, read), sizeof(unsigned char));

if (msgpack_unpacker_reserve_buffer(con->mpac,
MAX(read, con->packet.end)) == false) {
Expand Down Expand Up @@ -564,7 +556,7 @@ bool connection_send_event(uint64_t id, char *name, array args)
api_free_array(args);

if (con->pending_requests) {
wbuffer *rv = MALLOC(wbuffer);
wbuffer *rv = malloc_or_die(sizeof(wbuffer));
rv->size = sbuf.size;
rv->data = sb_memdup_nulterm(sbuf.data, sbuf.size);
kv_push(con->delayed_notifications, rv);
Expand Down Expand Up @@ -738,7 +730,7 @@ STATIC void connection_handle_request(struct connection *con,
dispatcher.async = true;
}

connection_request_event_info *eventinfo = MALLOC(connection_request_event_info);
connection_request_event_info *eventinfo = malloc_or_die(sizeof(connection_request_event_info));
eventinfo->con = con;
eventinfo->dispatcher = dispatcher;
eventinfo->args = args;
Expand Down
19 changes: 5 additions & 14 deletions src/rpc/connection/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,7 @@ int crypto_write(struct crypto_context *cc, char *data,
* (crypto_box_ZEROBYTES)
*/
packetlen = length + 56;
packet = MALLOC_ARRAY(packetlen, unsigned char);

if (packet == NULL)
return -1;
packet = malloc_array_or_die(packetlen, sizeof(unsigned char));

/* update nonce */
nonce_update(cc);
Expand All @@ -527,11 +524,8 @@ int crypto_write(struct crypto_context *cc, char *data,
memcpy(packet + 16, lengthbox + 16, 24);

blocklen = length + 32;
block = CALLOC(blocklen, unsigned char);
ciphertext = MALLOC_ARRAY(blocklen, unsigned char);

if ((block == NULL) || (ciphertext == NULL))
return -1;
block = calloc_or_die(blocklen, sizeof(unsigned char));
ciphertext = malloc_array_or_die(blocklen, sizeof(unsigned char));

memcpy(block + 32, data, length);

Expand Down Expand Up @@ -593,11 +587,8 @@ int crypto_read(struct crypto_context *cc, unsigned char *in, char *out,
/* ciphertextlen = length - 8 (id) - 72 (length) - 8 (nonce) + 16 (padding) */
ciphertextlen = length - 24;

block = MALLOC_ARRAY(ciphertextlen, unsigned char);
ciphertextpadded = CALLOC(ciphertextlen, unsigned char);

if ((block == NULL) || (ciphertextpadded == NULL))
return -1;
block = malloc_array_or_die(ciphertextlen, sizeof(unsigned char));
ciphertextpadded = calloc_or_die(ciphertextlen, sizeof(unsigned char));

memcpy(ciphertextpadded + 16, in + 40, blocklen);

Expand Down
6 changes: 3 additions & 3 deletions src/rpc/connection/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ multiqueue *multiqueue_new_child(multiqueue *parent)
static multiqueue *multiqueue_new(multiqueue *parent, put_callback put_cb,
void *data)
{
multiqueue *rv = MALLOC(multiqueue);
multiqueue *rv = malloc_or_die(sizeof(multiqueue));
QUEUE_INIT(&rv->headtail);
rv->parent = parent;
rv->put_cb = put_cb;
Expand Down Expand Up @@ -172,14 +172,14 @@ static event multiqueue_remove(multiqueue *this)

static void multiqueue_push(multiqueue *this, event e)
{
multiqueueitem *item = MALLOC(multiqueueitem);
multiqueueitem *item = malloc_or_die(sizeof(multiqueueitem));
item->link = false;
item->data.item.event = e;
QUEUE_INSERT_TAIL(&this->headtail, &item->node);

if (this->parent) {
// push link node to the parent queue
item->data.item.parent = MALLOC(multiqueueitem);
item->data.item.parent = malloc_or_die(sizeof(multiqueueitem));
item->data.item.parent->link = true;
item->data.item.parent->data.queue = this;
QUEUE_INSERT_TAIL(&this->parent->headtail, &item->data.item.parent->node);
Expand Down
7 changes: 2 additions & 5 deletions src/rpc/connection/inputstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@
inputstream *inputstream_new(inputstream_cb cb, uint32_t buffer_size,
void *data)
{
inputstream *rs = MALLOC(inputstream);

if (rs == NULL)
return (NULL);
inputstream *rs = malloc_or_die(sizeof(inputstream));

rs->data = data;
rs->size = 0;
Expand All @@ -54,7 +51,7 @@ inputstream *inputstream_new(inputstream_cb cb, uint32_t buffer_size,
rs->free_handle = false;

/* initialize circular buffer */
rs->circbuf_start = MALLOC_ARRAY(buffer_size, unsigned char);
rs->circbuf_start = malloc_array_or_die(buffer_size, sizeof(unsigned char));
rs->circbuf_read_pos = rs->circbuf_start;
rs->circbuf_write_pos = rs->circbuf_start;
rs->circbuf_end = rs->circbuf_start + buffer_size;
Expand Down
15 changes: 3 additions & 12 deletions src/rpc/connection/outputstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@

outputstream *outputstream_new(uint32_t maxmem)
{
outputstream *ws = MALLOC(outputstream);

if (ws == NULL)
return (NULL);
outputstream *ws = malloc_or_die(sizeof(outputstream));

ws->maxmem = maxmem;
ws->stream = NULL;
Expand Down Expand Up @@ -78,18 +75,12 @@ int outputstream_write(outputstream *ostream, char *buffer, size_t len)
buf.base = buffer;
buf.len = len;

data = MALLOC(struct write_request_data);

if (data == NULL)
return (-1);
data = malloc_or_die(sizeof(struct write_request_data));

data->ostream = ostream;
data->buffer = buffer;
data->len = len;
req = MALLOC(uv_write_t);

if (req == NULL)
return (-1);
req = malloc_or_die(sizeof(uv_write_t));

req->data = data;
ostream->curmem += len;
Expand Down
Loading

0 comments on commit 2681567

Please sign in to comment.