From 70ddd012e38184834dde1338c6fd50ad1cd627df Mon Sep 17 00:00:00 2001 From: Max Lv Date: Sat, 18 May 2013 18:43:06 +0800 Subject: [PATCH] fix a bug on OS X 10.6.x --- libasyncns/asyncns.c | 12 +++++++++--- src/jconf.c | 2 +- src/local.c | 4 ++-- src/redir.c | 4 ++-- src/utils.c | 12 +++++++++++- src/utils.h | 1 + 6 files changed, 26 insertions(+), 9 deletions(-) diff --git a/libasyncns/asyncns.c b/libasyncns/asyncns.c index e4993cb3..a270c188 100644 --- a/libasyncns/asyncns.c +++ b/libasyncns/asyncns.c @@ -645,7 +645,9 @@ static int process_worker(int in_fd, int out_fd) { if (!have_death_sig) { fd_set fds; - struct timeval tv = { 0, 5000000 }; + struct timeval tv; + tv.tv_usec = 500000; + tv.tv_sec = 0; FD_ZERO(&fds); FD_SET(in_fd, &fds); @@ -699,13 +701,17 @@ static void* thread_worker(void *p) { if (!asyncns->dead) { fd_set fds; - struct timeval tv = { 0, 5000000 }; + struct timeval tv; + tv.tv_usec = 500000; + tv.tv_sec = 0; FD_ZERO(&fds); FD_SET(in_fd, &fds); - if (select(in_fd+1, &fds, NULL, NULL, &tv) < 0) + if (select(in_fd+1, &fds, NULL, NULL, &tv) < 0) { + perror("test"); break; + } } if (length < 0 && diff --git a/src/jconf.c b/src/jconf.c index c069e1e0..f7e333ff 100644 --- a/src/jconf.c +++ b/src/jconf.c @@ -11,7 +11,7 @@ static char *to_string(const json_value *value) { if (value->type == json_string) { - return strndup(value->u.string.ptr, value->u.string.length); + return ss_strndup(value->u.string.ptr, value->u.string.length); } else if (value->type == json_integer) { return strdup(itoa(value->u.integer)); } else if (value->type == json_null) { diff --git a/src/local.c b/src/local.c index 85c6c2e1..54b9bc40 100644 --- a/src/local.c +++ b/src/local.c @@ -699,9 +699,9 @@ int main (int argc, char **argv) { listen_ctx.remote_host = malloc(sizeof(char *) * remote_num); while (remote_num > 0) { int index = --remote_num; - listen_ctx.remote_host[index] = strdup(remote_host[index]); + listen_ctx.remote_host[index] = remote_host[index]; } - listen_ctx.remote_port = strdup(remote_port); + listen_ctx.remote_port = remote_port; listen_ctx.timeout = atoi(timeout); listen_ctx.fd = listenfd; listen_ctx.iface = iface; diff --git a/src/redir.c b/src/redir.c index 9ebf916d..d71f794e 100644 --- a/src/redir.c +++ b/src/redir.c @@ -624,9 +624,9 @@ int main (int argc, char **argv) { listen_ctx.remote_host = malloc(sizeof(char *) * remote_num); while (remote_num > 0) { int index = --remote_num; - listen_ctx.remote_host[index] = strdup(remote_host[index]); + listen_ctx.remote_host[index] = remote_host[index]; } - listen_ctx.remote_port = strdup(remote_port); + listen_ctx.remote_port = remote_port; listen_ctx.timeout = atoi(timeout); listen_ctx.fd = listenfd; diff --git a/src/utils.c b/src/utils.c index 1fd5a92e..116c02c4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -43,6 +43,17 @@ char *itoa(int i) { return p; } +char *ss_strndup(const char *s, size_t n) { + size_t len = strlen(s); + char *ret; + + if (len <= n) return strdup(s); + + ret = malloc(n + 1); + strncpy(ret, s, n); + ret[n] = '\0'; + return ret; +} void FATAL(const char *msg) { LOGE("%s", msg); @@ -109,6 +120,5 @@ void demonize(const char* path) { close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); - } diff --git a/src/utils.h b/src/utils.h index c9dad763..1b5560af 100644 --- a/src/utils.h +++ b/src/utils.h @@ -35,5 +35,6 @@ void ERROR(const char *s); void usage(void); void demonize(const char* path); char *itoa(int i); +char *ss_strndup(const char *s, size_t n); #endif // _UTILS_H