Skip to content

Commit

Permalink
Merge pull request redis#246 from dmelani/master
Browse files Browse the repository at this point in the history
Less surprising behaviour in redisFree() and freeReplyObject()
  • Loading branch information
pietern committed May 29, 2014
2 parents f225c27 + 21a1207 commit 2602e1b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions hiredis.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -723,6 +737,7 @@ int main(int argc, char **argv) {
test_blocking_connection(cfg);
}


if (fails) {
printf("*** %d TESTS FAILED ***\n", fails);
return 1;
Expand Down

0 comments on commit 2602e1b

Please sign in to comment.