summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormichael-grunder <michael.grunder@gmail.com>2018-05-13 18:08:57 -0700
committermichael-grunder <michael.grunder@gmail.com>2018-05-13 18:08:57 -0700
commit7a1acdfeb58b3e3a84a7b5d34af5ec1daae0f603 (patch)
treec936a04bdcf6ad8079a20a4e61aa1fbfb9def6f7
parentc1af0e849d6387a4297a1e1066c107f6f72d8469 (diff)
parent58e6b87f51ba31b4a2954d0babe6b3cf7019c985 (diff)
Merge remote-tracking branch 'justinbrewer/hiredis-pr1' into posix-build-fixes
-rw-r--r--fmacros.h31
-rw-r--r--hiredis.c38
-rw-r--r--hiredis.h24
-rw-r--r--net.c22
-rw-r--r--net.h4
-rw-r--r--read.c15
6 files changed, 29 insertions, 105 deletions
diff --git a/fmacros.h b/fmacros.h
index f6a6d7d..3227faa 100644
--- a/fmacros.h
+++ b/fmacros.h
@@ -1,39 +1,12 @@
#ifndef __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
-#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
-#endif
#if defined(__APPLE__) && defined(__MACH__)
-#define _OSX
-#endif
-
-#ifndef AIX
-# define _XOPEN_SOURCE_EXTENDED 1
-# define _ALL_SOURCE
+/* Enable TCP_KEEPALIVE */
+#define _DARWIN_C_SOURCE
#endif
#endif
diff --git a/hiredis.c b/hiredis.c
index b344962..98f43c9 100644
--- a/hiredis.c
+++ b/hiredis.c
@@ -84,16 +84,14 @@ void freeReplyObject(void *reply) {
case REDIS_REPLY_ARRAY:
if (r->element != NULL) {
for (j = 0; j < r->elements; j++)
- if (r->element[j] != NULL)
- freeReplyObject(r->element[j]);
+ freeReplyObject(r->element[j]);
free(r->element);
}
break;
case REDIS_REPLY_ERROR:
case REDIS_REPLY_STATUS:
case REDIS_REPLY_STRING:
- if (r->str != NULL)
- free(r->str);
+ free(r->str);
break;
}
free(r);
@@ -432,11 +430,7 @@ cleanup:
}
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;
}
@@ -581,7 +575,7 @@ void __redisSetError(redisContext *c, int type, const char *str) {
} else {
/* Only REDIS_ERR_IO may lack a description! */
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)
return NULL;
- c->err = 0;
- c->errstr[0] = '\0';
c->obuf = sdsempty();
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) {
redisFree(c);
@@ -618,18 +606,12 @@ void redisFree(redisContext *c) {
return;
if (c->fd > 0)
close(c->fd);
- if (c->obuf != NULL)
- sdsfree(c->obuf);
- if (c->reader != NULL)
- redisReaderFree(c->reader);
- if (c->tcp.host)
- free(c->tcp.host);
- if (c->tcp.source_addr)
- free(c->tcp.source_addr);
- if (c->unix_sock.path)
- free(c->unix_sock.path);
- if (c->timeout)
- free(c->timeout);
+ sdsfree(c->obuf);
+ redisReaderFree(c->reader);
+ free(c->tcp.host);
+ free(c->tcp.source_addr);
+ free(c->unix_sock.path);
+ free(c->timeout);
free(c);
}
diff --git a/hiredis.h b/hiredis.h
index 77d5797..a743760 100644
--- a/hiredis.h
+++ b/hiredis.h
@@ -80,30 +80,6 @@
* SO_REUSEADDR is being used. */
#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
extern "C" {
#endif
diff --git a/net.c b/net.c
index 33d9a82..5d50f7c 100644
--- a/net.c
+++ b/net.c
@@ -71,7 +71,7 @@ static void __redisSetErrorFromErrno(redisContext *c, int type, const char *pref
if (prefix != NULL)
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);
}
@@ -136,7 +136,7 @@ int redisKeepAlive(redisContext *c, int interval) {
val = interval;
-#ifdef _OSX
+#if defined(__APPLE__) && defined(__MACH__)
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &val, sizeof(val)) < 0) {
__redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
return REDIS_ERR;
@@ -285,8 +285,7 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
* This is a bit ugly, but atleast it works and doesn't leak memory.
**/
if (c->tcp.host != addr) {
- if (c->tcp.host)
- free(c->tcp.host);
+ free(c->tcp.host);
c->tcp.host = strdup(addr);
}
@@ -299,8 +298,7 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
memcpy(c->timeout, timeout, sizeof(struct timeval));
}
} else {
- if (c->timeout)
- free(c->timeout);
+ free(c->timeout);
c->timeout = NULL;
}
@@ -412,7 +410,10 @@ addrretry:
error:
rv = REDIS_ERR;
end:
- freeaddrinfo(servinfo);
+ if(servinfo) {
+ freeaddrinfo(servinfo);
+ }
+
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;
long timeout_msec = -1;
- if (redisCreateSocket(c,AF_LOCAL) < 0)
+ if (redisCreateSocket(c,AF_UNIX) < 0)
return REDIS_ERR;
if (redisSetBlocking(c,0) != REDIS_OK)
return REDIS_ERR;
@@ -449,15 +450,14 @@ int redisContextConnectUnix(redisContext *c, const char *path, const struct time
memcpy(c->timeout, timeout, sizeof(struct timeval));
}
} else {
- if (c->timeout)
- free(c->timeout);
+ free(c->timeout);
c->timeout = NULL;
}
if (redisContextTimeoutMsec(c,&timeout_msec) != REDIS_OK)
return REDIS_ERR;
- sa.sun_family = AF_LOCAL;
+ sa.sun_family = AF_UNIX;
strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
if (connect(c->fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
if (errno == EINPROGRESS && !blocking) {
diff --git a/net.h b/net.h
index fc8ddd3..d9dc362 100644
--- a/net.h
+++ b/net.h
@@ -37,10 +37,6 @@
#include "hiredis.h"
-#if defined(__sun) || defined(AIX)
-#define AF_LOCAL AF_UNIX
-#endif
-
int redisCheckSocketError(redisContext *c);
int redisContextSetTimeout(redisContext *c, const struct timeval tv);
int redisContextConnectTcp(redisContext *c, const char *addr, int port, const struct timeval *timeout);
diff --git a/read.c b/read.c
index 061bbda..2bad85e 100644
--- a/read.c
+++ b/read.c
@@ -52,11 +52,9 @@ static void __redisReaderSetError(redisReader *r, int type, const char *str) {
}
/* Clear input buffer on errors. */
- if (r->buf != NULL) {
- sdsfree(r->buf);
- r->buf = NULL;
- r->pos = r->len = 0;
- }
+ sdsfree(r->buf);
+ r->buf = NULL;
+ r->pos = r->len = 0;
/* Reset task stack. */
r->ridx = -1;
@@ -420,8 +418,6 @@ redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) {
if (r == NULL)
return NULL;
- r->err = 0;
- r->errstr[0] = '\0';
r->fn = fn;
r->buf = sdsempty();
r->maxbuf = REDIS_READER_MAX_BUF;
@@ -435,10 +431,11 @@ redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) {
}
void redisReaderFree(redisReader *r) {
+ if (r == NULL)
+ return;
if (r->reply != NULL && r->fn && r->fn->freeObject)
r->fn->freeObject(r->reply);
- if (r->buf != NULL)
- sdsfree(r->buf);
+ sdsfree(r->buf);
free(r);
}