From 54acc8f08759713338b6cb6c94916438bbd91b80 Mon Sep 17 00:00:00 2001 From: Justin Brewer Date: Tue, 27 Mar 2018 13:44:01 -0500 Subject: Remove redundant zero stores calloc is guaranteed to provide a zero-initialized buffer. There is no need to set fields to zero explicitly. Signed-off-by: Justin Brewer --- read.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'read.c') diff --git a/read.c b/read.c index 061bbda..4baa74e 100644 --- a/read.c +++ b/read.c @@ -420,8 +420,6 @@ redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) { if (r == NULL) return NULL; - r->err = 0; - r->errstr[0] = '\0'; r->fn = fn; r->buf = sdsempty(); r->maxbuf = REDIS_READER_MAX_BUF; -- cgit v1.2.3 From 58e6b87f51ba31b4a2954d0babe6b3cf7019c985 Mon Sep 17 00:00:00 2001 From: Justin Brewer Date: Tue, 27 Mar 2018 13:47:29 -0500 Subject: Remove redundant NULL checks free(NULL) is a valid NOP. Most of the hiredis free functions behave the same way. redisReaderFree is updated to also be NULL-safe. There is one redundant NULL check at sds.c:1036, but it's left as is since sds is imported from upstream. Signed-off-by: Justin Brewer --- hiredis.c | 30 +++++++++--------------------- net.c | 9 +++------ read.c | 13 ++++++------- 3 files changed, 18 insertions(+), 34 deletions(-) (limited to 'read.c') diff --git a/hiredis.c b/hiredis.c index 8eabac0..98f43c9 100644 --- a/hiredis.c +++ b/hiredis.c @@ -84,16 +84,14 @@ void freeReplyObject(void *reply) { case REDIS_REPLY_ARRAY: if (r->element != NULL) { for (j = 0; j < r->elements; j++) - if (r->element[j] != NULL) - freeReplyObject(r->element[j]); + freeReplyObject(r->element[j]); free(r->element); } break; case REDIS_REPLY_ERROR: case REDIS_REPLY_STATUS: case REDIS_REPLY_STRING: - if (r->str != NULL) - free(r->str); + free(r->str); break; } free(r); @@ -432,11 +430,7 @@ cleanup: } sdsfree(curarg); - - /* No need to check cmd since it is the last statement that can fail, - * but do it anyway to be as defensive as possible. */ - if (cmd != NULL) - free(cmd); + free(cmd); return error_type; } @@ -612,18 +606,12 @@ void redisFree(redisContext *c) { return; if (c->fd > 0) close(c->fd); - if (c->obuf != NULL) - sdsfree(c->obuf); - if (c->reader != NULL) - redisReaderFree(c->reader); - if (c->tcp.host) - free(c->tcp.host); - if (c->tcp.source_addr) - free(c->tcp.source_addr); - if (c->unix_sock.path) - free(c->unix_sock.path); - if (c->timeout) - free(c->timeout); + sdsfree(c->obuf); + redisReaderFree(c->reader); + free(c->tcp.host); + free(c->tcp.source_addr); + free(c->unix_sock.path); + free(c->timeout); free(c); } diff --git a/net.c b/net.c index 7ce9ef4..5d50f7c 100644 --- a/net.c +++ b/net.c @@ -285,8 +285,7 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port, * This is a bit ugly, but atleast it works and doesn't leak memory. **/ if (c->tcp.host != addr) { - if (c->tcp.host) - free(c->tcp.host); + free(c->tcp.host); c->tcp.host = strdup(addr); } @@ -299,8 +298,7 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port, memcpy(c->timeout, timeout, sizeof(struct timeval)); } } else { - if (c->timeout) - free(c->timeout); + free(c->timeout); c->timeout = NULL; } @@ -452,8 +450,7 @@ int redisContextConnectUnix(redisContext *c, const char *path, const struct time memcpy(c->timeout, timeout, sizeof(struct timeval)); } } else { - if (c->timeout) - free(c->timeout); + free(c->timeout); c->timeout = NULL; } diff --git a/read.c b/read.c index 4baa74e..2bad85e 100644 --- a/read.c +++ b/read.c @@ -52,11 +52,9 @@ static void __redisReaderSetError(redisReader *r, int type, const char *str) { } /* Clear input buffer on errors. */ - if (r->buf != NULL) { - sdsfree(r->buf); - r->buf = NULL; - r->pos = r->len = 0; - } + sdsfree(r->buf); + r->buf = NULL; + r->pos = r->len = 0; /* Reset task stack. */ r->ridx = -1; @@ -433,10 +431,11 @@ redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) { } void redisReaderFree(redisReader *r) { + if (r == NULL) + return; if (r->reply != NULL && r->fn && r->fn->freeObject) r->fn->freeObject(r->reply); - if (r->buf != NULL) - sdsfree(r->buf); + sdsfree(r->buf); free(r); } -- cgit v1.2.3