Use static buffer for error string on context

This commit is contained in:
Pieter Noordhuis 2011-04-21 15:56:22 +02:00
parent 7f11360604
commit 0ee7c66818
3 changed files with 40 additions and 35 deletions

View File

@ -828,14 +828,19 @@ int redisFormatCommandArgv(char **target, int argc, const char **argv, const siz
return totlen;
}
void __redisSetError(redisContext *c, int type, const sds errstr) {
void __redisSetError(redisContext *c, int type, const char *str) {
size_t len;
c->err = type;
if (errstr != NULL) {
c->errstr = errstr;
if (str != NULL) {
len = strlen(str);
len = len < (sizeof(c->errstr)-1) ? len : (sizeof(c->errstr)-1);
memcpy(c->errstr,str,len);
c->errstr[len] = '\0';
} else {
/* Only REDIS_ERR_IO may lack a description! */
assert(type == REDIS_ERR_IO);
c->errstr = sdsnew(strerror(errno));
strerror_r(errno,c->errstr,sizeof(c->errstr));
}
}
@ -847,7 +852,7 @@ static redisContext *redisContextInit(void) {
return NULL;
c->err = 0;
c->errstr = NULL;
c->errstr[0] = '\0';
c->obuf = sdsempty();
c->reader = redisReplyReaderCreate();
return c;
@ -856,8 +861,6 @@ static redisContext *redisContextInit(void) {
void redisFree(redisContext *c) {
if (c->fd > 0)
close(c->fd);
if (c->errstr != NULL)
sdsfree(c->errstr);
if (c->obuf != NULL)
sdsfree(c->obuf);
if (c->reader != NULL)
@ -933,8 +936,7 @@ int redisBufferRead(redisContext *c) {
return REDIS_ERR;
}
} else if (nread == 0) {
__redisSetError(c,REDIS_ERR_EOF,
sdsnew("Server closed the connection"));
__redisSetError(c,REDIS_ERR_EOF,"Server closed the connection");
return REDIS_ERR;
} else {
redisReplyReaderFeed(c->reader,buf,nread);
@ -979,8 +981,7 @@ int redisBufferWrite(redisContext *c, int *done) {
* or set an error in the context otherwise. */
int redisGetReplyFromReader(redisContext *c, void **reply) {
if (redisReplyReaderGetReply(c->reader,reply) == REDIS_ERR) {
__redisSetError(c,REDIS_ERR_PROTOCOL,
sdsnew(((redisReader*)c->reader)->errstr));
__redisSetError(c,REDIS_ERR_PROTOCOL,c->reader->errstr);
return REDIS_ERR;
}
return REDIS_OK;

View File

@ -153,7 +153,7 @@ int redisFormatCommandArgv(char **target, int argc, const char **argv, const siz
/* Context for a connection to Redis */
typedef struct redisContext {
int err; /* Error flags, 0 when there is no error */
char *errstr; /* String representation of error when applicable */
char errstr[128]; /* String representation of error when applicable */
int fd;
int flags;
char *obuf; /* Write buffer */

50
net.c
View File

@ -49,18 +49,28 @@
#include "net.h"
#include "sds.h"
/* Forward declaration */
void __redisSetError(redisContext *c, int type, sds err);
/* Defined in hiredis.c */
void __redisSetError(redisContext *c, int type, const char *str);
static void __redisSetErrorFromErrno(redisContext *c, int type, const char *prefix) {
char buf[128];
size_t len = 0;
if (prefix != NULL)
len = snprintf(buf,sizeof(buf),"%s: ",prefix);
strerror_r(errno,buf+len,sizeof(buf)-len);
__redisSetError(c,type,buf);
}
static int redisCreateSocket(redisContext *c, int type) {
int s, on = 1;
if ((s = socket(type, SOCK_STREAM, 0)) == -1) {
__redisSetError(c,REDIS_ERR_IO,NULL);
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
return REDIS_ERR;
}
if (type == AF_INET) {
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
__redisSetError(c,REDIS_ERR_IO,NULL);
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
close(s);
return REDIS_ERR;
}
@ -75,8 +85,7 @@ static int redisSetBlocking(redisContext *c, int fd, int blocking) {
* Note that fcntl(2) for F_GETFL and F_SETFL can't be
* interrupted by a signal. */
if ((flags = fcntl(fd, F_GETFL)) == -1) {
__redisSetError(c,REDIS_ERR_IO,
sdscatprintf(sdsempty(), "fcntl(F_GETFL): %s", strerror(errno)));
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_GETFL)");
close(fd);
return REDIS_ERR;
}
@ -87,8 +96,7 @@ static int redisSetBlocking(redisContext *c, int fd, int blocking) {
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) == -1) {
__redisSetError(c,REDIS_ERR_IO,
sdscatprintf(sdsempty(), "fcntl(F_SETFL): %s", strerror(errno)));
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_SETFL)");
close(fd);
return REDIS_ERR;
}
@ -98,8 +106,7 @@ static int redisSetBlocking(redisContext *c, int fd, int blocking) {
static int redisSetTcpNoDelay(redisContext *c, int fd) {
int yes = 1;
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) {
__redisSetError(c,REDIS_ERR_IO,
sdscatprintf(sdsempty(), "setsockopt(TCP_NODELAY): %s", strerror(errno)));
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(TCP_NODELAY)");
close(fd);
return REDIS_ERR;
}
@ -124,15 +131,14 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *
FD_SET(fd, &wfd);
if (select(FD_SETSIZE, NULL, &wfd, NULL, toptr) == -1) {
__redisSetError(c,REDIS_ERR_IO,
sdscatprintf(sdsempty(), "select(2): %s", strerror(errno)));
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"select(2)");
close(fd);
return REDIS_ERR;
}
if (!FD_ISSET(fd, &wfd)) {
errno = ETIMEDOUT;
__redisSetError(c,REDIS_ERR_IO,NULL);
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
close(fd);
return REDIS_ERR;
}
@ -140,15 +146,14 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *
err = 0;
errlen = sizeof(err);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
__redisSetError(c,REDIS_ERR_IO,
sdscatprintf(sdsempty(), "getsockopt(SO_ERROR): %s", strerror(errno)));
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"getsockopt(SO_ERROR)");
close(fd);
return REDIS_ERR;
}
if (err) {
errno = err;
__redisSetError(c,REDIS_ERR_IO,NULL);
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
close(fd);
return REDIS_ERR;
}
@ -156,20 +161,18 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *
return REDIS_OK;
}
__redisSetError(c,REDIS_ERR_IO,NULL);
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
close(fd);
return REDIS_ERR;
}
int redisContextSetTimeout(redisContext *c, struct timeval tv) {
if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) {
__redisSetError(c,REDIS_ERR_IO,
sdscatprintf(sdsempty(), "setsockopt(SO_RCVTIMEO): %s", strerror(errno)));
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)");
return REDIS_ERR;
}
if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)) == -1) {
__redisSetError(c,REDIS_ERR_IO,
sdscatprintf(sdsempty(), "setsockopt(SO_SNDTIMEO): %s", strerror(errno)));
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_SNDTIMEO)");
return REDIS_ERR;
}
return REDIS_OK;
@ -192,8 +195,9 @@ int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct t
he = gethostbyname(addr);
if (he == NULL) {
__redisSetError(c,REDIS_ERR_OTHER,
sdscatprintf(sdsempty(),"Can't resolve: %s",addr));
char buf[128];
snprintf(buf,sizeof(buf),"Can't resolve: %s", addr);
__redisSetError(c,REDIS_ERR_OTHER,buf);
close(s);
return REDIS_ERR;
}