diff --git a/modules/cachedb_redis/cachedb_redis_dbase.c b/modules/cachedb_redis/cachedb_redis_dbase.c index 5ef92c6a5a4..e53c24c226c 100644 --- a/modules/cachedb_redis/cachedb_redis_dbase.c +++ b/modules/cachedb_redis/cachedb_redis_dbase.c @@ -565,29 +565,24 @@ static int _redis_run_command(cachedb_con *connection, redisReply **rpl, str *ke node->context->errstr); if (match_prefix(reply->str, reply->len, MOVED_PREFIX, MOVED_PREFIX_LEN)) { - // It's a MOVED response - redis_moved *moved_info = pkg_malloc(sizeof(redis_moved)); - if (!moved_info) { - LM_ERR("cachedb_redis: Unable to allocate redis_moved struct, no more pkg memory\n"); - freeReplyObject(reply); - reply = NULL; - goto try_next_con; - } else { - if (parse_moved_reply(reply, moved_info) < 0) { - LM_ERR("cachedb_redis: Unable to parse MOVED reply\n"); - pkg_free(moved_info); - moved_info = NULL; - freeReplyObject(reply); - goto try_next_con; - } - - LM_DBG("cachedb_redis: MOVED slot: [%d] endpoint: [%.*s] port: [%d]\n", moved_info->slot, moved_info->endpoint.len, moved_info->endpoint.s, moved_info->port); - node = get_redis_connection_by_endpoint(con, moved_info); + /* MOVED response */ + redis_moved moved_info_s; + redis_moved *moved_info = &moved_info_s; - pkg_free(moved_info); - moved_info = NULL; + if (parse_moved_reply(reply, moved_info) < 0) { + LM_ERR("failed to parse MOVED reply\n"); freeReplyObject(reply); reply = NULL; + goto try_next_con; + } + + LM_DBG("MOVED slot=%d endpoint=%.*s:%d\n", + moved_info->slot, moved_info->endpoint.len, + moved_info->endpoint.s, moved_info->port); + node = get_redis_connection_by_endpoint(con, moved_info); + + freeReplyObject(reply); + reply = NULL; if (node == NULL) { LM_ERR("Unable to locate connection by endpoint\n"); @@ -603,9 +598,8 @@ static int _redis_run_command(cachedb_con *connection, redisReply **rpl, str *ke } } - i = QUERY_ATTEMPTS; // New node that is the target being MOVED to, should have the attempts reset + i = QUERY_ATTEMPTS; continue; - } } freeReplyObject(reply); diff --git a/modules/cachedb_redis/cachedb_redis_utils.c b/modules/cachedb_redis/cachedb_redis_utils.c index 6abac0882b5..413842455f6 100644 --- a/modules/cachedb_redis/cachedb_redis_utils.c +++ b/modules/cachedb_redis/cachedb_redis_utils.c @@ -394,6 +394,7 @@ int parse_moved_reply(redisReply *reply, redis_moved *out) { while (p < end && *p >= '0' && *p <= '9') { slot = slot * 10 + (*p - '0'); p++; + if (slot > 16383) return ERR_INVALID_SLOT; } if (slot == 0 && (p == reply->str + MOVED_PREFIX_LEN || *(p - 1) < '0' || *(p - 1) > '9')) return ERR_INVALID_SLOT; @@ -426,11 +427,12 @@ int parse_moved_reply(redisReply *reply, redis_moved *out) { while (p < end && *p >= '0' && *p <= '9') { port = port * 10 + (*p - '0'); p++; + if (port > 65535) return ERR_INVALID_PORT; } if (port < 0 || port > 65535 || port_start == p) return ERR_INVALID_PORT; } - } else if (out->endpoint.s < end) { + } else if (p < end) { out->endpoint.s = host_start; out->endpoint.len = end - host_start; }