Browse Source

check that redisContext is not NULL before using it

pull/388/head
Claudiu Boriga 8 years ago
parent
commit
67de59ada4
1 changed files with 27 additions and 4 deletions
  1. +27
    -4
      daemon/redis.c

+ 27
- 4
daemon/redis.c View File

@ -74,6 +74,10 @@ static void json_restore_call(struct redis *r, struct callmaster *m, const str *
static void redis_pipe(struct redis *r, const char *fmt, ...) { static void redis_pipe(struct redis *r, const char *fmt, ...) {
va_list ap; va_list ap;
if (!r->ctx) {
ilog(LOG_ERROR, "Unable to pipe redis command. No redis context");
return;
}
va_start(ap, fmt); va_start(ap, fmt);
redisvAppendCommand(r->ctx, fmt, ap); redisvAppendCommand(r->ctx, fmt, ap);
va_end(ap); va_end(ap);
@ -83,6 +87,10 @@ static redisReply *redis_get(struct redis *r, int type, const char *fmt, ...) {
va_list ap; va_list ap;
redisReply *ret; redisReply *ret;
if (!r->ctx) {
ilog(LOG_ERROR, "Unable to get redis reply. No redis context");
return NULL;
}
va_start(ap, fmt); va_start(ap, fmt);
ret = redis_expect(type, redisvCommand(r->ctx, fmt, ap)); ret = redis_expect(type, redisvCommand(r->ctx, fmt, ap));
va_end(ap); va_end(ap);
@ -94,6 +102,10 @@ static int redisCommandNR(redisContext *r, const char *fmt, ...) {
redisReply *ret; redisReply *ret;
int i = 0; int i = 0;
if (!r) {
ilog(LOG_ERROR, "Unable to send redis command. No redis context");
return -1;
}
va_start(ap, fmt); va_start(ap, fmt);
ret = redisvCommand(r, fmt, ap); ret = redisvCommand(r, fmt, ap);
va_end(ap); va_end(ap);
@ -115,6 +127,11 @@ static int redisCommandNR(redisContext *r, const char *fmt, ...) {
static int redis_check_type(struct redis *r, char *key, char *suffix, char *type) { static int redis_check_type(struct redis *r, char *key, char *suffix, char *type) {
redisReply *rp; redisReply *rp;
if (!r->ctx) {
ilog(LOG_ERROR, "Unable to check redis reply type. No redis context");
return -1;
}
rp = redisCommand(r->ctx, "TYPE %s%s", key, suffix ? : ""); rp = redisCommand(r->ctx, "TYPE %s%s", key, suffix ? : "");
if (!rp) if (!rp)
return -1; return -1;
@ -133,6 +150,11 @@ static int redis_check_type(struct redis *r, char *key, char *suffix, char *type
static void redis_consume(struct redis *r) { static void redis_consume(struct redis *r) {
redisReply *rp; redisReply *rp;
if (!r->ctx) {
ilog(LOG_ERROR, "Unable to consume pipelined replies. No redis context");
r->pipeline = 0;
return;
}
while (r->pipeline) { while (r->pipeline) {
if (redisGetReply(r->ctx, (void **) &rp) == REDIS_OK) if (redisGetReply(r->ctx, (void **) &rp) == REDIS_OK)
freeReplyObject(rp); freeReplyObject(rp);
@ -291,7 +313,7 @@ void on_redis_notification(redisAsyncContext *actx, void *reply, void *privdata)
// select the right db for restoring the call // select the right db for restoring the call
if (redisCommandNR(r->ctx, "SELECT %i", r->db)) { if (redisCommandNR(r->ctx, "SELECT %i", r->db)) {
if (r->ctx->err)
if (r->ctx && r->ctx->err)
rlog(LOG_ERROR, "Redis error: %s", r->ctx->errstr); rlog(LOG_ERROR, "Redis error: %s", r->ctx->errstr);
redisFree(r->ctx); redisFree(r->ctx);
r->ctx = NULL; r->ctx = NULL;
@ -1597,7 +1619,8 @@ int redis_restore(struct callmaster *m, struct redis *r) {
calls = redis_get(r, REDIS_REPLY_ARRAY, "KEYS *"); calls = redis_get(r, REDIS_REPLY_ARRAY, "KEYS *");
if (!calls) { if (!calls) {
rlog(LOG_ERR, "Could not retrieve call list from Redis: %s", r->ctx->errstr);
rlog(LOG_ERR, "Could not retrieve call list from Redis: %s",
r->ctx ? r->ctx->errstr : "No redis context");
goto err; goto err;
} }
@ -2049,7 +2072,7 @@ err:
mutex_unlock(&r->lock); mutex_unlock(&r->lock);
rwlock_unlock_r(&c->master_lock); rwlock_unlock_r(&c->master_lock);
if (r->ctx->err)
if (r->ctx && r->ctx->err)
rlog(LOG_ERR, "Redis error: %s", r->ctx->errstr); rlog(LOG_ERR, "Redis error: %s", r->ctx->errstr);
redisFree(r->ctx); redisFree(r->ctx);
r->ctx = NULL; r->ctx = NULL;
@ -2082,7 +2105,7 @@ err:
rwlock_unlock_r(&c->master_lock); rwlock_unlock_r(&c->master_lock);
mutex_unlock(&r->lock); mutex_unlock(&r->lock);
if (r->ctx->err)
if (r->ctx && r->ctx->err)
rlog(LOG_ERR, "Redis error: %s", r->ctx->errstr); rlog(LOG_ERR, "Redis error: %s", r->ctx->errstr);
redisFree(r->ctx); redisFree(r->ctx);
r->ctx = NULL; r->ctx = NULL;


Loading…
Cancel
Save