Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe allocation wrappers #754

Merged
merged 4 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ ADD_LIBRARY(hiredis SHARED
net.c
read.c
sds.c
sockcompat.c)
sockcompat.c
alloc.c)

SET_TARGET_PROPERTIES(hiredis
PROPERTIES
Expand Down
19 changes: 10 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (C) 2010-2011 Pieter Noordhuis <pcnoordhuis at gmail dot com>
# This file is released under the BSD license, see the COPYING file

OBJ=net.o hiredis.o sds.o async.o read.o sockcompat.o
OBJ=net.o hiredis.o sds.o async.o read.o sockcompat.o alloc.o
SSL_OBJ=ssl.o
EXAMPLES=hiredis-example hiredis-example-libevent hiredis-example-libev hiredis-example-glib
ifeq ($(USE_SSL),1)
Expand Down Expand Up @@ -95,15 +95,16 @@ all: $(SSL_DYLIBNAME) $(SSL_STLIBNAME) $(SSL_PKGCONFNAME)
endif

# Deps (use make dep to generate this)
async.o: async.c fmacros.h async.h hiredis.h read.h sds.h net.h dict.c dict.h
dict.o: dict.c fmacros.h dict.h
hiredis.o: hiredis.c fmacros.h hiredis.h read.h sds.h net.h win32.h
net.o: net.c fmacros.h net.h hiredis.h read.h sds.h sockcompat.h win32.h
read.o: read.c fmacros.h read.h sds.h
sds.o: sds.c sds.h
async.o: async.c fmacros.h alloc.h async.h hiredis.h read.h sds.h net.h dict.c dict.h win32.h async_private.h
dict.o: dict.c fmacros.h alloc.h dict.h
hiredis.o: hiredis.c fmacros.h hiredis.h read.h sds.h alloc.h net.h async.h win32.h
alloc.o: alloc.c alloc.h
net.o: net.c fmacros.h net.h hiredis.h read.h sds.h alloc.h sockcompat.h win32.h
read.o: read.c fmacros.h read.h sds.h win32.h
sds.o: sds.c sds.h sdsalloc.h
sockcompat.o: sockcompat.c sockcompat.h
ssl.o: ssl.c hiredis.h
test.o: test.c fmacros.h hiredis.h read.h sds.h
ssl.o: ssl.c hiredis.h read.h sds.h alloc.h async.h async_private.h
test.o: test.c fmacros.h hiredis.h read.h sds.h alloc.h net.h

$(DYLIBNAME): $(OBJ)
$(DYLIB_MAKE_CMD) -o $(DYLIBNAME) $(OBJ) $(REAL_LDFLAGS)
Expand Down
2 changes: 1 addition & 1 deletion adapters/ae.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {
return REDIS_ERR;

/* Create container for context and r/w events */
e = (redisAeEvents*)malloc(sizeof(*e));
e = (redisAeEvents*)hi_malloc(sizeof(*e));
e->context = ac;
e->loop = loop;
e->fd = c->fd;
Expand Down
2 changes: 1 addition & 1 deletion adapters/ivykis.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static int redisIvykisAttach(redisAsyncContext *ac) {
return REDIS_ERR;

/* Create container for context and r/w events */
e = (redisIvykisEvents*)malloc(sizeof(*e));
e = (redisIvykisEvents*)hi_malloc(sizeof(*e));
e->context = ac;

/* Register functions to start/stop listening for events */
Expand Down
2 changes: 1 addition & 1 deletion adapters/libev.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
return REDIS_ERR;

/* Create container for context and r/w events */
e = (redisLibevEvents*)malloc(sizeof(*e));
e = (redisLibevEvents*)hi_malloc(sizeof(*e));
e->context = ac;
#if EV_MULTIPLICITY
e->loop = loop;
Expand Down
2 changes: 1 addition & 1 deletion adapters/libevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
return REDIS_ERR;

/* Create container for context and r/w events */
e = (redisLibeventEvents*)calloc(1, sizeof(*e));
e = (redisLibeventEvents*)hi_calloc(1, sizeof(*e));
e->context = ac;

/* Register functions to start/stop listening for events */
Expand Down
65 changes: 65 additions & 0 deletions alloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2020, Michael Grunder <michael dot grunder at gmail dot com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "fmacros.h"
#include "alloc.h"
michael-grunder marked this conversation as resolved.
Show resolved Hide resolved
#include <string.h>

void *hi_malloc(size_t size) {
void *ptr = malloc(size);
if (ptr == NULL)
HIREDIS_OOM_HANDLER;

return ptr;
}

void *hi_calloc(size_t nmemb, size_t size) {
void *ptr = calloc(nmemb, size);
if (ptr == NULL)
HIREDIS_OOM_HANDLER;

return ptr;
}

void *hi_realloc(void *ptr, size_t size) {
void *newptr = realloc(ptr, size);
if (newptr == NULL)
HIREDIS_OOM_HANDLER;

return newptr;
}

char *hi_strdup(const char *str) {
char *newstr = strdup(str);
if (newstr == NULL)
HIREDIS_OOM_HANDLER;

return newstr;
}
44 changes: 44 additions & 0 deletions alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2020, Michael Grunder <michael dot grunder at gmail dot com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef HIREDIS_ALLOC_H

#include <stdlib.h> /* for size_t */

#ifndef HIREDIS_OOM_HANDLER
#define HIREDIS_OOM_HANDLER abort()
#endif

void *hi_malloc(size_t size);
void *hi_calloc(size_t nmemb, size_t size);
void *hi_realloc(void *ptr, size_t size);
char *hi_strdup(const char *str);

#endif /* HIREDIS_ALLOC_H */
5 changes: 3 additions & 2 deletions async.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/

#include "fmacros.h"
#include "alloc.h"
#include <stdlib.h>
#include <string.h>
#ifndef _MSC_VER
Expand Down Expand Up @@ -57,7 +58,7 @@ static unsigned int callbackHash(const void *key) {

static void *callbackValDup(void *privdata, const void *src) {
((void) privdata);
redisCallback *dup = malloc(sizeof(*dup));
redisCallback *dup = hi_malloc(sizeof(*dup));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can still result in a SEGFAULT,
If hi_malloc returns NULL -> next call to memcpy will SEGFAULT

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just waking up here but how can hi_malloc return NULL if HIREDIS_OOM_HANDLER is called? (eg. _exit?)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lamby yeah, I forgot to redo the comment, talked about it already with @michael-grunder
either way personally I'm against doing abort() if we can handle it, for example when doing callback-allocation, we can return error instead of abort, remember that this is a 3rd-party library for most users, and crashing someone's application due to internal errors is not the way to do things IMHO

memcpy(dup,src,sizeof(*dup));
return dup;
}
Expand Down Expand Up @@ -754,7 +755,7 @@ int redisAsyncFormattedCommand(redisAsyncContext *ac, redisCallbackFn *fn, void

void redisAsyncSetTimeout(redisAsyncContext *ac, struct timeval tv) {
if (!ac->c.timeout) {
ac->c.timeout = calloc(1, sizeof(tv));
ac->c.timeout = hi_calloc(1, sizeof(tv));
}

if (tv.tv_sec == ac->c.timeout->tv_sec &&
Expand Down
7 changes: 4 additions & 3 deletions dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/

#include "fmacros.h"
#include "alloc.h"
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
Expand Down Expand Up @@ -71,7 +72,7 @@ static void _dictReset(dict *ht) {

/* Create a new hash table */
static dict *dictCreate(dictType *type, void *privDataPtr) {
dict *ht = malloc(sizeof(*ht));
dict *ht = hi_malloc(sizeof(*ht));
_dictInit(ht,type,privDataPtr);
return ht;
}
Expand Down Expand Up @@ -142,7 +143,7 @@ static int dictAdd(dict *ht, void *key, void *val) {
return DICT_ERR;

/* Allocates the memory and stores key */
entry = malloc(sizeof(*entry));
entry = hi_malloc(sizeof(*entry));
entry->next = ht->table[index];
ht->table[index] = entry;

Expand Down Expand Up @@ -256,7 +257,7 @@ static dictEntry *dictFind(dict *ht, const void *key) {
}

static dictIterator *dictGetIterator(dict *ht) {
dictIterator *iter = malloc(sizeof(*iter));
dictIterator *iter = hi_malloc(sizeof(*iter));

iter->ht = ht;
iter->index = -1;
Expand Down
1 change: 1 addition & 0 deletions hiredis.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct timeval; /* forward declaration */
#endif
#include <stdint.h> /* uintXX_t, etc */
#include "sds.h" /* for sds */
#include "alloc.h" /* for allocation wrappers */

#define HIREDIS_MAJOR 0
#define HIREDIS_MINOR 14
Expand Down
14 changes: 7 additions & 7 deletions net.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,13 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
if (c->tcp.host != addr) {
free(c->tcp.host);

c->tcp.host = strdup(addr);
c->tcp.host = hi_strdup(addr);
}

if (timeout) {
if (c->timeout != timeout) {
if (c->timeout == NULL)
c->timeout = malloc(sizeof(struct timeval));
c->timeout = hi_malloc(sizeof(struct timeval));

memcpy(c->timeout, timeout, sizeof(struct timeval));
}
Expand All @@ -383,7 +383,7 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
c->tcp.source_addr = NULL;
} else if (c->tcp.source_addr != source_addr) {
free(c->tcp.source_addr);
c->tcp.source_addr = strdup(source_addr);
c->tcp.source_addr = hi_strdup(source_addr);
}

snprintf(_port, 6, "%d", port);
Expand Down Expand Up @@ -447,7 +447,7 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,

/* For repeat connection */
free(c->saddr);
c->saddr = malloc(p->ai_addrlen);
c->saddr = hi_malloc(p->ai_addrlen);
memcpy(c->saddr, p->ai_addr, p->ai_addrlen);
c->addrlen = p->ai_addrlen;

Expand Down Expand Up @@ -526,12 +526,12 @@ int redisContextConnectUnix(redisContext *c, const char *path, const struct time

c->connection_type = REDIS_CONN_UNIX;
if (c->unix_sock.path != path)
c->unix_sock.path = strdup(path);
c->unix_sock.path = hi_strdup(path);

if (timeout) {
if (c->timeout != timeout) {
if (c->timeout == NULL)
c->timeout = malloc(sizeof(struct timeval));
c->timeout = hi_malloc(sizeof(struct timeval));

memcpy(c->timeout, timeout, sizeof(struct timeval));
}
Expand All @@ -543,7 +543,7 @@ int redisContextConnectUnix(redisContext *c, const char *path, const struct time
if (redisContextTimeoutMsec(c,&timeout_msec) != REDIS_OK)
return REDIS_ERR;

sa = (struct sockaddr_un*)(c->saddr = malloc(sizeof(struct sockaddr_un)));
sa = (struct sockaddr_un*)(c->saddr = hi_malloc(sizeof(struct sockaddr_un)));
c->addrlen = sizeof(struct sockaddr_un);
sa->sun_family = AF_UNIX;
strncpy(sa->sun_path, path, sizeof(sa->sun_path) - 1);
Expand Down
2 changes: 1 addition & 1 deletion ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static void initOpensslLocks(void) {
return;
}
nlocks = CRYPTO_num_locks();
ossl_locks = malloc(sizeof(*ossl_locks) * nlocks);
ossl_locks = hi_malloc(sizeof(*ossl_locks) * nlocks);
for (ii = 0; ii < nlocks; ii++) {
sslLockInit(ossl_locks + ii);
}
Expand Down