From 76495c07fcaa93f672076060ac1474e3f6991dc2 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 13 Apr 2014 20:43:00 +1000 Subject: [PATCH] Create helper functions for aligning all memory allocations and checking their return values and make dealloc take a pointer as a macro --- src/libckpool.c | 51 +++++++++++++++++++++++++++++-------------------- src/libckpool.h | 7 ++++++- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/libckpool.c b/src/libckpool.c index 92519408..20a36376 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -259,15 +259,10 @@ bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port) } hlen = url_len + 1; - align_len(&hlen); - url_address = malloc(hlen); - if (unlikely(!url_address)) - quit(1, "Failed to malloc url_address of length %d in extract_sockaddr", (int)hlen); + url_address = ckalloc(hlen); sprintf(url_address, "%.*s", url_len, url_begin); - port = malloc(8); - if (unlikely(!port)) - quit(1, "Failed to malloc port in extract_sockaddr"); + port = ckalloc(8); if (port_len) { char *slash; @@ -424,7 +419,7 @@ int read_socket_line(connsock_t *cs) int ret, bufsiz; fd_set rd; - dealloc(&cs->buf); + dealloc(cs->buf); retry: FD_ZERO(&rd); FD_SET(cs->fd, &rd); @@ -471,7 +466,7 @@ retry: ret = bufofs + 1; out: if (ret < 1) - dealloc(&cs->buf); + dealloc(cs->buf); return ret; } @@ -564,7 +559,7 @@ json_t *json_rpc_call(connsock_t *cs, const char *rpc_req) if (!val) LOGWARNING("JSON decode failed(%d): %s", err_val.line, err_val.text); out: - dealloc(&cs->buf); + dealloc(cs->buf); return val; } @@ -602,6 +597,28 @@ void realloc_strcat(char **ptr, const char *s) sprintf(ofs, "%s", s); } +void *_ckalloc(size_t len, const char *file, const char *func, const int line) +{ + void *ptr; + + align_len(&len); + ptr = malloc(len); + if (unlikely(!ptr)) + quitfrom(1, file, func, line, "Failed to ckalloc! errno=%d", errno); + return ptr; +} + +void *_ckzalloc(size_t len, const char *file, const char *func, const int line) +{ + void *ptr; + + align_len(&len); + ptr = calloc(len, 1); + if (unlikely(!ptr)) + quitfrom(1, file, func, line, "Failed to ckalloc! errno=%d", errno); + return ptr; +} + void _dealloc(void **ptr) { free(*ptr); @@ -630,14 +647,9 @@ void *bin2hex(const uchar *p, size_t len) uchar *s; slen = len * 2 + 1; - align_len(&slen); - s = calloc(slen, 1); - if (unlikely(!s)) { - LOGERR("Failed to calloc s in bin2hex"); - } else - __bin2hex(s, p, len); + s = ckzalloc(slen); + __bin2hex(s, p, len); - /* Returns NULL if calloc failed. */ return s; } @@ -742,10 +754,7 @@ char *http_base64(const char *src) l = strlen((const char *)src); hlen = ((l + 2) / 3) * 4 + 1; - align_len(&hlen); - str = malloc(hlen); - if (unlikely(!str)) - quit(1, "Failed to malloc string of length %d in http_base64", (int)hlen); + str = ckalloc(hlen); dst = str; r = 0; diff --git a/src/libckpool.h b/src/libckpool.h index 7989fa5d..147651d8 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -45,7 +45,10 @@ #define ck_runlock(_lock) _ck_runlock(_lock, __FILE__, __func__, __LINE__) #define ck_wunlock(_lock) _ck_wunlock(_lock, __FILE__, __func__, __LINE__) -#define dealloc(ptr) _dealloc((void *)(ptr)) +#define ckalloc(len) _ckalloc(len, __FILE__, __func__, __LINE__) +#define ckzalloc(len) _ckzalloc(len, __FILE__, __func__, __LINE__) + +#define dealloc(ptr) _dealloc((void *)&(ptr)) /* Placeholders for when we have more comprehensive logging facilities */ #define LOGERR(fmt, ...) do { \ @@ -189,6 +192,8 @@ json_t *json_rpc_call(connsock_t *cs, const char *rpc_req); void align_len(size_t *len); void realloc_strcat(char **ptr, const char *s); +void *_ckalloc(size_t len, const char *file, const char *func, const int line); +void *_ckzalloc(size_t len, const char *file, const char *func, const int line); void _dealloc(void **ptr); void __bin2hex(uchar *s, const uchar *p, size_t len); void *bin2hex(const uchar *p, size_t len);