Set error when invalid timeout value is given to redisConnectWithTimeout

Closes #154

This commit properly sets the error value inside of
redisContextWaitReady when an invalid sec or usec value is provided.
Tests for each case are provided to demonstrate that the issue is
properly fixed and to avoid regression.

Signed-off-by: Aaron Bedra <aaron@aaronbedra.com>
This commit is contained in:
Aaron Bedra 2013-03-14 21:34:21 -05:00
parent fd38e55d71
commit fca66b9e8b
2 changed files with 28 additions and 0 deletions

1
net.c
View File

@ -136,6 +136,7 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *
/* Only use timeout when not NULL. */ /* Only use timeout when not NULL. */
if (timeout != NULL) { if (timeout != NULL) {
if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) { if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) {
__redisSetErrorFromErrno(c, REDIS_ERR_IO, NULL);
close(fd); close(fd);
return REDIS_ERR; return REDIS_ERR;
} }

27
test.c
View File

@ -8,6 +8,7 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <errno.h> #include <errno.h>
#include <limits.h>
#include "hiredis.h" #include "hiredis.h"
@ -22,6 +23,7 @@ struct config {
struct { struct {
const char *host; const char *host;
int port; int port;
struct timeval timeout;
} tcp; } tcp;
struct { struct {
@ -433,6 +435,30 @@ static void test_blocking_io_errors(struct config config) {
redisFree(c); redisFree(c);
} }
static void test_invalid_timeout_errors(struct config config) {
redisContext *c;
test("Set error when an invalid timeout usec value is given to redisConnectWithTimeout: ");
config.tcp.timeout.tv_sec = 0;
config.tcp.timeout.tv_usec = 10000001;
c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout);
test_cond(c->err == REDIS_ERR_IO);
test("Set error when an invalid timeout sec value is given to redisConnectWithTimeout: ");
config.tcp.timeout.tv_sec = (((LONG_MAX) - 999) / 1000) + 1;
config.tcp.timeout.tv_usec = 0;
c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout);
test_cond(c->err == REDIS_ERR_IO);
redisFree(c);
}
static void test_throughput(struct config config) { static void test_throughput(struct config config) {
redisContext *c = connect(config); redisContext *c = connect(config);
redisReply **replies; redisReply **replies;
@ -641,6 +667,7 @@ int main(int argc, char **argv) {
cfg.type = CONN_TCP; cfg.type = CONN_TCP;
test_blocking_connection(cfg); test_blocking_connection(cfg);
test_blocking_io_errors(cfg); test_blocking_io_errors(cfg);
test_invalid_timeout_errors(cfg);
if (throughput) test_throughput(cfg); if (throughput) test_throughput(cfg);
printf("\nTesting against Unix socket connection (%s):\n", cfg.unix.path); printf("\nTesting against Unix socket connection (%s):\n", cfg.unix.path);