From 814be4f5bd62b4f66281879b3035a20ad84bb498 Mon Sep 17 00:00:00 2001 From: Henri Doreau Date: Tue, 22 Jan 2013 10:16:30 +0100 Subject: Made connect functions return NULL on alloc failures. Updated documentation and examples accordingly. --- async.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'async.c') diff --git a/async.c b/async.c index f65f869..b302d82 100644 --- a/async.c +++ b/async.c @@ -142,15 +142,27 @@ static void __redisAsyncCopyError(redisAsyncContext *ac) { } redisAsyncContext *redisAsyncConnect(const char *ip, int port) { - redisContext *c = redisConnectNonBlock(ip,port); - redisAsyncContext *ac = redisAsyncInitialize(c); + redisContext *c; + redisAsyncContext *ac; + + c = redisConnectNonBlock(ip,port); + if (c == NULL) + return NULL; + + ac = redisAsyncInitialize(c); __redisAsyncCopyError(ac); return ac; } redisAsyncContext *redisAsyncConnectUnix(const char *path) { - redisContext *c = redisConnectUnixNonBlock(path); - redisAsyncContext *ac = redisAsyncInitialize(c); + redisContext *c; + redisAsyncContext *ac; + + c = redisConnectUnixNonBlock(path); + if (c == NULL) + return NULL; + + ac = redisAsyncInitialize(c); __redisAsyncCopyError(ac); return ac; } -- cgit v1.2.3 From d7e3268f48b457cb52336d264f8860b336faea9f Mon Sep 17 00:00:00 2001 From: Henri Doreau Date: Tue, 22 Jan 2013 15:53:17 +0100 Subject: Prevent AsyncConnect from crashing on memory allocation failures. --- async.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'async.c') diff --git a/async.c b/async.c index b302d82..83d88ec 100644 --- a/async.c +++ b/async.c @@ -102,7 +102,12 @@ static dictType callbackDict = { }; static redisAsyncContext *redisAsyncInitialize(redisContext *c) { - redisAsyncContext *ac = realloc(c,sizeof(redisAsyncContext)); + redisAsyncContext *ac; + + ac = realloc(c,sizeof(redisAsyncContext)); + if (ac == NULL) + return NULL; + c = &(ac->c); /* The regular connect functions will always set the flag REDIS_CONNECTED. @@ -150,6 +155,11 @@ redisAsyncContext *redisAsyncConnect(const char *ip, int port) { return NULL; ac = redisAsyncInitialize(c); + if (ac == NULL) { + redisFree(c); + return NULL; + } + __redisAsyncCopyError(ac); return ac; } @@ -194,6 +204,9 @@ static int __redisPushCallback(redisCallbackList *list, redisCallback *source) { /* Copy callback from stack to heap */ cb = malloc(sizeof(*cb)); + if (cb == NULL) + return REDIS_ERR_OOM; + if (source != NULL) { memcpy(cb,source,sizeof(*cb)); cb->next = NULL; -- cgit v1.2.3