From 05b85ebe7ffcfe41f084b6f46ab49a4f21c0eb22 Mon Sep 17 00:00:00 2001 From: Daniel Melani Date: Thu, 29 May 2014 16:05:27 +0200 Subject: [PATCH 1/2] Less surprising behaviour. Make redisFree() and freeReplyObject() less surprising by behaving just like free(). That is, don't crash when passing in NULL. --- hiredis.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hiredis.c b/hiredis.c index 2afee56..78beaae 100644 --- a/hiredis.c +++ b/hiredis.c @@ -73,6 +73,9 @@ void freeReplyObject(void *reply) { redisReply *r = reply; size_t j; + if (r == NULL) + return; + switch(r->type) { case REDIS_REPLY_INTEGER: break; /* Nothing to free */ @@ -1001,6 +1004,8 @@ static redisContext *redisContextInit(void) { } void redisFree(redisContext *c) { + if (c == NULL) + return; if (c->fd > 0) close(c->fd); if (c->obuf != NULL) From 21a1207ae13f3d422fb99669019836da3422b784 Mon Sep 17 00:00:00 2001 From: Daniel Melani Date: Thu, 29 May 2014 17:10:03 +0200 Subject: [PATCH 2/2] Add tests for redisFree() and freeReplyObject() Test that redisFree() and freeReplyObject() do not crash when NULL is passed in. --- test.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test.c b/test.c index 713cc06..93116bd 100644 --- a/test.c +++ b/test.c @@ -318,6 +318,19 @@ static void test_reply_reader(void) { redisReaderFree(reader); } +static void test_free_null(void) { + void *redisContext = NULL; + void *reply = NULL; + + test("Don't fail when redisFree is passed a NULL value: "); + redisFree(redisContext); + test_cond(redisContext == NULL); + + test("Don't fail when freeReplyObject is passed a NULL value: "); + freeReplyObject(reply); + test_cond(reply == NULL); +} + static void test_blocking_connection_errors(void) { redisContext *c; @@ -702,6 +715,7 @@ int main(int argc, char **argv) { test_format_commands(); test_reply_reader(); test_blocking_connection_errors(); + test_free_null(); printf("\nTesting against TCP connection (%s:%d):\n", cfg.tcp.host, cfg.tcp.port); cfg.type = CONN_TCP; @@ -723,6 +737,7 @@ int main(int argc, char **argv) { test_blocking_connection(cfg); } + if (fails) { printf("*** %d TESTS FAILED ***\n", fails); return 1;