Commit 58e6b87f authored by Justin Brewer's avatar Justin Brewer
Browse files

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: default avatarJustin Brewer <jzb0012@auburn.edu>
parent 54acc8f0
...@@ -84,7 +84,6 @@ void freeReplyObject(void *reply) { ...@@ -84,7 +84,6 @@ void freeReplyObject(void *reply) {
case REDIS_REPLY_ARRAY: case REDIS_REPLY_ARRAY:
if (r->element != NULL) { if (r->element != NULL) {
for (j = 0; j < r->elements; j++) for (j = 0; j < r->elements; j++)
if (r->element[j] != NULL)
freeReplyObject(r->element[j]); freeReplyObject(r->element[j]);
free(r->element); free(r->element);
} }
...@@ -92,7 +91,6 @@ void freeReplyObject(void *reply) { ...@@ -92,7 +91,6 @@ void freeReplyObject(void *reply) {
case REDIS_REPLY_ERROR: case REDIS_REPLY_ERROR:
case REDIS_REPLY_STATUS: case REDIS_REPLY_STATUS:
case REDIS_REPLY_STRING: case REDIS_REPLY_STRING:
if (r->str != NULL)
free(r->str); free(r->str);
break; break;
} }
...@@ -432,10 +430,6 @@ cleanup: ...@@ -432,10 +430,6 @@ cleanup:
} }
sdsfree(curarg); 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; return error_type;
...@@ -612,17 +606,11 @@ void redisFree(redisContext *c) { ...@@ -612,17 +606,11 @@ void redisFree(redisContext *c) {
return; return;
if (c->fd > 0) if (c->fd > 0)
close(c->fd); close(c->fd);
if (c->obuf != NULL)
sdsfree(c->obuf); sdsfree(c->obuf);
if (c->reader != NULL)
redisReaderFree(c->reader); redisReaderFree(c->reader);
if (c->tcp.host)
free(c->tcp.host); free(c->tcp.host);
if (c->tcp.source_addr)
free(c->tcp.source_addr); free(c->tcp.source_addr);
if (c->unix_sock.path)
free(c->unix_sock.path); free(c->unix_sock.path);
if (c->timeout)
free(c->timeout); free(c->timeout);
free(c); free(c);
} }
......
...@@ -285,7 +285,6 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port, ...@@ -285,7 +285,6 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
* This is a bit ugly, but atleast it works and doesn't leak memory. * This is a bit ugly, but atleast it works and doesn't leak memory.
**/ **/
if (c->tcp.host != addr) { if (c->tcp.host != addr) {
if (c->tcp.host)
free(c->tcp.host); free(c->tcp.host);
c->tcp.host = strdup(addr); c->tcp.host = strdup(addr);
...@@ -299,7 +298,6 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port, ...@@ -299,7 +298,6 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
memcpy(c->timeout, timeout, sizeof(struct timeval)); memcpy(c->timeout, timeout, sizeof(struct timeval));
} }
} else { } else {
if (c->timeout)
free(c->timeout); free(c->timeout);
c->timeout = NULL; c->timeout = NULL;
} }
...@@ -452,7 +450,6 @@ int redisContextConnectUnix(redisContext *c, const char *path, const struct time ...@@ -452,7 +450,6 @@ int redisContextConnectUnix(redisContext *c, const char *path, const struct time
memcpy(c->timeout, timeout, sizeof(struct timeval)); memcpy(c->timeout, timeout, sizeof(struct timeval));
} }
} else { } else {
if (c->timeout)
free(c->timeout); free(c->timeout);
c->timeout = NULL; c->timeout = NULL;
} }
......
...@@ -52,11 +52,9 @@ static void __redisReaderSetError(redisReader *r, int type, const char *str) { ...@@ -52,11 +52,9 @@ static void __redisReaderSetError(redisReader *r, int type, const char *str) {
} }
/* Clear input buffer on errors. */ /* Clear input buffer on errors. */
if (r->buf != NULL) {
sdsfree(r->buf); sdsfree(r->buf);
r->buf = NULL; r->buf = NULL;
r->pos = r->len = 0; r->pos = r->len = 0;
}
/* Reset task stack. */ /* Reset task stack. */
r->ridx = -1; r->ridx = -1;
...@@ -433,9 +431,10 @@ redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) { ...@@ -433,9 +431,10 @@ redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) {
} }
void redisReaderFree(redisReader *r) { void redisReaderFree(redisReader *r) {
if (r == NULL)
return;
if (r->reply != NULL && r->fn && r->fn->freeObject) if (r->reply != NULL && r->fn && r->fn->freeObject)
r->fn->freeObject(r->reply); r->fn->freeObject(r->reply);
if (r->buf != NULL)
sdsfree(r->buf); sdsfree(r->buf);
free(r); free(r);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment