Merge remote-tracking branch 'justinbrewer/hiredis-pr1' into posix-build-fixes

This commit is contained in:
michael-grunder 2018-05-13 18:08:57 -07:00
commit 7a1acdfeb5
6 changed files with 29 additions and 105 deletions

View File

@ -1,39 +1,12 @@
#ifndef __HIREDIS_FMACRO_H #ifndef __HIREDIS_FMACRO_H
#define __HIREDIS_FMACRO_H #define __HIREDIS_FMACRO_H
#if defined(__linux__)
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#endif
#if defined(__CYGWIN__)
#define _POSIX_C_SOURCE 200112L
#include <sys/cdefs.h>
#endif
#if defined(__linux__) || defined(__OpenBSD__) || defined(__NetBSD__)
#define _XOPEN_SOURCE 600 #define _XOPEN_SOURCE 600
#elif defined(__APPLE__) && defined(__MACH__)
#define _XOPEN_SOURCE
#elif defined(__FreeBSD__)
// intentionally left blank, don't define _XOPEN_SOURCE
#elif defined(AIX)
// intentionally left blank, don't define _XOPEN_SOURCE
#else
#define _XOPEN_SOURCE
#endif
#if defined(__sun__)
#define _POSIX_C_SOURCE 200112L #define _POSIX_C_SOURCE 200112L
#endif
#if defined(__APPLE__) && defined(__MACH__) #if defined(__APPLE__) && defined(__MACH__)
#define _OSX /* Enable TCP_KEEPALIVE */
#endif #define _DARWIN_C_SOURCE
#ifndef AIX
# define _XOPEN_SOURCE_EXTENDED 1
# define _ALL_SOURCE
#endif #endif
#endif #endif

View File

@ -84,7 +84,6 @@ void freeReplyObject(void *reply) {
case REDIS_REPLY_ARRAY: case REDIS_REPLY_ARRAY:
if (r->element != NULL) { if (r->element != NULL) {
for (j = 0; j < r->elements; j++) for (j = 0; j < r->elements; j++)
if (r->element[j] != NULL)
freeReplyObject(r->element[j]); freeReplyObject(r->element[j]);
free(r->element); free(r->element);
} }
@ -92,7 +91,6 @@ void freeReplyObject(void *reply) {
case REDIS_REPLY_ERROR: case REDIS_REPLY_ERROR:
case REDIS_REPLY_STATUS: case REDIS_REPLY_STATUS:
case REDIS_REPLY_STRING: case REDIS_REPLY_STRING:
if (r->str != NULL)
free(r->str); free(r->str);
break; break;
} }
@ -432,10 +430,6 @@ cleanup:
} }
sdsfree(curarg); sdsfree(curarg);
/* No need to check cmd since it is the last statement that can fail,
* but do it anyway to be as defensive as possible. */
if (cmd != NULL)
free(cmd); free(cmd);
return error_type; return error_type;
@ -581,7 +575,7 @@ void __redisSetError(redisContext *c, int type, const char *str) {
} else { } else {
/* Only REDIS_ERR_IO may lack a description! */ /* Only REDIS_ERR_IO may lack a description! */
assert(type == REDIS_ERR_IO); assert(type == REDIS_ERR_IO);
__redis_strerror_r(errno, c->errstr, sizeof(c->errstr)); strerror_r(errno, c->errstr, sizeof(c->errstr));
} }
} }
@ -596,14 +590,8 @@ static redisContext *redisContextInit(void) {
if (c == NULL) if (c == NULL)
return NULL; return NULL;
c->err = 0;
c->errstr[0] = '\0';
c->obuf = sdsempty(); c->obuf = sdsempty();
c->reader = redisReaderCreate(); c->reader = redisReaderCreate();
c->tcp.host = NULL;
c->tcp.source_addr = NULL;
c->unix_sock.path = NULL;
c->timeout = NULL;
if (c->obuf == NULL || c->reader == NULL) { if (c->obuf == NULL || c->reader == NULL) {
redisFree(c); redisFree(c);
@ -618,17 +606,11 @@ void redisFree(redisContext *c) {
return; return;
if (c->fd > 0) if (c->fd > 0)
close(c->fd); close(c->fd);
if (c->obuf != NULL)
sdsfree(c->obuf); sdsfree(c->obuf);
if (c->reader != NULL)
redisReaderFree(c->reader); redisReaderFree(c->reader);
if (c->tcp.host)
free(c->tcp.host); free(c->tcp.host);
if (c->tcp.source_addr)
free(c->tcp.source_addr); free(c->tcp.source_addr);
if (c->unix_sock.path)
free(c->unix_sock.path); free(c->unix_sock.path);
if (c->timeout)
free(c->timeout); free(c->timeout);
free(c); free(c);
} }

View File

@ -80,30 +80,6 @@
* SO_REUSEADDR is being used. */ * SO_REUSEADDR is being used. */
#define REDIS_CONNECT_RETRIES 10 #define REDIS_CONNECT_RETRIES 10
/* strerror_r has two completely different prototypes and behaviors
* depending on system issues, so we need to operate on the error buffer
* differently depending on which strerror_r we're using. */
#ifndef _GNU_SOURCE
/* "regular" POSIX strerror_r that does the right thing. */
#define __redis_strerror_r(errno, buf, len) \
do { \
strerror_r((errno), (buf), (len)); \
} while (0)
#else
/* "bad" GNU strerror_r we need to clean up after. */
#define __redis_strerror_r(errno, buf, len) \
do { \
char *err_str = strerror_r((errno), (buf), (len)); \
/* If return value _isn't_ the start of the buffer we passed in, \
* then GNU strerror_r returned an internal static buffer and we \
* need to copy the result into our private buffer. */ \
if (err_str != (buf)) { \
strncpy((buf), err_str, ((len) - 1)); \
(buf)[(len)-1] = '\0'; \
} \
} while (0)
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

14
net.c
View File

@ -71,7 +71,7 @@ static void __redisSetErrorFromErrno(redisContext *c, int type, const char *pref
if (prefix != NULL) if (prefix != NULL)
len = snprintf(buf,sizeof(buf),"%s: ",prefix); len = snprintf(buf,sizeof(buf),"%s: ",prefix);
__redis_strerror_r(errorno, (char *)(buf + len), sizeof(buf) - len); strerror_r(errorno, (char *)(buf + len), sizeof(buf) - len);
__redisSetError(c,type,buf); __redisSetError(c,type,buf);
} }
@ -136,7 +136,7 @@ int redisKeepAlive(redisContext *c, int interval) {
val = interval; val = interval;
#ifdef _OSX #if defined(__APPLE__) && defined(__MACH__)
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &val, sizeof(val)) < 0) { if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &val, sizeof(val)) < 0) {
__redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
return REDIS_ERR; return REDIS_ERR;
@ -285,7 +285,6 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
* This is a bit ugly, but atleast it works and doesn't leak memory. * This is a bit ugly, but atleast it works and doesn't leak memory.
**/ **/
if (c->tcp.host != addr) { if (c->tcp.host != addr) {
if (c->tcp.host)
free(c->tcp.host); free(c->tcp.host);
c->tcp.host = strdup(addr); c->tcp.host = strdup(addr);
@ -299,7 +298,6 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
memcpy(c->timeout, timeout, sizeof(struct timeval)); memcpy(c->timeout, timeout, sizeof(struct timeval));
} }
} else { } else {
if (c->timeout)
free(c->timeout); free(c->timeout);
c->timeout = NULL; c->timeout = NULL;
} }
@ -412,7 +410,10 @@ addrretry:
error: error:
rv = REDIS_ERR; rv = REDIS_ERR;
end: end:
if(servinfo) {
freeaddrinfo(servinfo); freeaddrinfo(servinfo);
}
return rv; // Need to return REDIS_OK if alright return rv; // Need to return REDIS_OK if alright
} }
@ -432,7 +433,7 @@ int redisContextConnectUnix(redisContext *c, const char *path, const struct time
struct sockaddr_un sa; struct sockaddr_un sa;
long timeout_msec = -1; long timeout_msec = -1;
if (redisCreateSocket(c,AF_LOCAL) < 0) if (redisCreateSocket(c,AF_UNIX) < 0)
return REDIS_ERR; return REDIS_ERR;
if (redisSetBlocking(c,0) != REDIS_OK) if (redisSetBlocking(c,0) != REDIS_OK)
return REDIS_ERR; return REDIS_ERR;
@ -449,7 +450,6 @@ int redisContextConnectUnix(redisContext *c, const char *path, const struct time
memcpy(c->timeout, timeout, sizeof(struct timeval)); memcpy(c->timeout, timeout, sizeof(struct timeval));
} }
} else { } else {
if (c->timeout)
free(c->timeout); free(c->timeout);
c->timeout = NULL; c->timeout = NULL;
} }
@ -457,7 +457,7 @@ int redisContextConnectUnix(redisContext *c, const char *path, const struct time
if (redisContextTimeoutMsec(c,&timeout_msec) != REDIS_OK) if (redisContextTimeoutMsec(c,&timeout_msec) != REDIS_OK)
return REDIS_ERR; return REDIS_ERR;
sa.sun_family = AF_LOCAL; sa.sun_family = AF_UNIX;
strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1); strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
if (connect(c->fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) { if (connect(c->fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
if (errno == EINPROGRESS && !blocking) { if (errno == EINPROGRESS && !blocking) {

4
net.h
View File

@ -37,10 +37,6 @@
#include "hiredis.h" #include "hiredis.h"
#if defined(__sun) || defined(AIX)
#define AF_LOCAL AF_UNIX
#endif
int redisCheckSocketError(redisContext *c); int redisCheckSocketError(redisContext *c);
int redisContextSetTimeout(redisContext *c, const struct timeval tv); int redisContextSetTimeout(redisContext *c, const struct timeval tv);
int redisContextConnectTcp(redisContext *c, const char *addr, int port, const struct timeval *timeout); int redisContextConnectTcp(redisContext *c, const char *addr, int port, const struct timeval *timeout);

7
read.c
View File

@ -52,11 +52,9 @@ static void __redisReaderSetError(redisReader *r, int type, const char *str) {
} }
/* Clear input buffer on errors. */ /* Clear input buffer on errors. */
if (r->buf != NULL) {
sdsfree(r->buf); sdsfree(r->buf);
r->buf = NULL; r->buf = NULL;
r->pos = r->len = 0; r->pos = r->len = 0;
}
/* Reset task stack. */ /* Reset task stack. */
r->ridx = -1; r->ridx = -1;
@ -420,8 +418,6 @@ redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) {
if (r == NULL) if (r == NULL)
return NULL; return NULL;
r->err = 0;
r->errstr[0] = '\0';
r->fn = fn; r->fn = fn;
r->buf = sdsempty(); r->buf = sdsempty();
r->maxbuf = REDIS_READER_MAX_BUF; r->maxbuf = REDIS_READER_MAX_BUF;
@ -435,9 +431,10 @@ redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) {
} }
void redisReaderFree(redisReader *r) { void redisReaderFree(redisReader *r) {
if (r == NULL)
return;
if (r->reply != NULL && r->fn && r->fn->freeObject) if (r->reply != NULL && r->fn && r->fn->freeObject)
r->fn->freeObject(r->reply); r->fn->freeObject(r->reply);
if (r->buf != NULL)
sdsfree(r->buf); sdsfree(r->buf);
free(r); free(r);
} }