From d11adf90e7f622547b96cd4eee6941b1ff5af9eb Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sat, 26 Nov 2016 20:38:53 +1000 Subject: [PATCH] * better error handling in source/redis & backend/redis --- src/backends/redis.c | 16 ++++++++++------ src/sources/redis.c | 9 ++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/backends/redis.c b/src/backends/redis.c index 6a0d2a9..1b17c83 100644 --- a/src/backends/redis.c +++ b/src/backends/redis.c @@ -56,7 +56,8 @@ redis_connect(cfg_t *cfg) { break; } if (cfg->password[0]) { - reply = redisCommand(conn, "AUTH %s", cfg->password); + if ((reply = redisCommand(conn, "AUTH %s", cfg->password)) == NULL) + break; if (reply->type == REDIS_REPLY_ERROR) { snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str); break; @@ -64,7 +65,8 @@ redis_connect(cfg_t *cfg) { freeReplyObject(reply); } if (cfg->database) { - reply = redisCommand(conn, "SELECT %d", cfg->database); + if ((reply = redisCommand(conn, "SELECT %d", cfg->database)) == NULL) + break; if (reply->type == REDIS_REPLY_ERROR) { snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str); break; @@ -193,14 +195,16 @@ ban(cfg_t *cfg, const char *ip) { redisReply *reply; do { - reply = redisCommand(cfg->conn, "HINCRBY %s %s %d", cfg->hash, ip, 1); - if (reply && reply->type == REDIS_REPLY_ERROR) { + if ((reply = redisCommand(cfg->conn, "HINCRBY %s %s %d", cfg->hash, ip, 1)) == NULL) + break; + if (reply->type == REDIS_REPLY_ERROR) { snprintf(cfg->error, sizeof(cfg->error), "HINCRBY: %s", reply->str); break; } freeReplyObject(reply); - reply = redisCommand(cfg->conn, "PUBLISH %s %s", cfg->hash, ip); - if (reply && reply->type == REDIS_REPLY_ERROR) { + if ((reply = redisCommand(cfg->conn, "PUBLISH %s %s", cfg->hash, ip)) == NULL) + break; + if (reply->type == REDIS_REPLY_ERROR) { snprintf(cfg->error, sizeof(cfg->error), "PUBLISH: %s", reply->str); break; } diff --git a/src/sources/redis.c b/src/sources/redis.c index b2f4a8b..3b14c8e 100644 --- a/src/sources/redis.c +++ b/src/sources/redis.c @@ -51,7 +51,8 @@ redis_connect(cfg_t *cfg) { break; } if (cfg->password[0]) { - reply = redisCommand(conn, "AUTH %s", cfg->password); + if ((reply = redisCommand(conn, "AUTH %s", cfg->password)) == NULL) + break; if (reply->type == REDIS_REPLY_ERROR) { snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str); break; @@ -59,7 +60,8 @@ redis_connect(cfg_t *cfg) { freeReplyObject(reply); } if (cfg->database) { - reply = redisCommand(conn, "SELECT %d", cfg->database); + if ((reply = redisCommand(conn, "SELECT %d", cfg->database)) == NULL) + break; if (reply->type == REDIS_REPLY_ERROR) { snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str); break; @@ -72,7 +74,8 @@ redis_connect(cfg_t *cfg) { strlcpy(cfg->error, "can't enable nonblocking mode", sizeof(cfg->error)); break; } - reply = redisCommand(conn, "SUBSCRIBE %s", cfg->hash); + if ((reply = redisCommand(conn, "SUBSCRIBE %s", cfg->hash)) == NULL) + break; if (reply->type == REDIS_REPLY_ERROR) { snprintf(cfg->error, sizeof(cfg->error), "can't subscribe: %s", reply->str); break;