[sheepdog] [PATCH 2/4] sheep: don't modify the option as it will be used in future

Robin Dong robin.k.dong at gmail.com
Sun Nov 24 09:40:24 CET 2013


The old zk_init() use strtok() and will set ',' to '\0' before 'timeout' argument,
this will change the content of argument 'option' which we should not modify because
it will be used by sheep/http.c

Signed-off-by: Robin Dong <sanbai at taobao.com>
---
 sheep/cluster/zookeeper.c |   24 ++++++++++++++++++------
 1 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/sheep/cluster/zookeeper.c b/sheep/cluster/zookeeper.c
index fa89c46..a6864e0 100644
--- a/sheep/cluster/zookeeper.c
+++ b/sheep/cluster/zookeeper.c
@@ -1060,7 +1060,8 @@ kick_block_event:
 
 static int zk_init(const char *option)
 {
-	char *hosts, *to, *p;
+	char *hosts, *to;
+	const char *p = option;
 	int ret, timeout = SESSION_TIMEOUT;
 
 	if (!option) {
@@ -1068,22 +1069,33 @@ static int zk_init(const char *option)
 		return -1;
 	}
 
-	hosts = strtok((char *)option, "=");
-	if ((to = strtok(NULL, "="))) {
-		if (sscanf(to, "%u", &timeout) != 1) {
+	to = strstr(option, "=");
+	if (to) {
+		if (sscanf(++to, "%u", &timeout) != 1) {
 			sd_err("Invalid paramter for timeout");
 			return -1;
 		}
-		p = strstr(hosts, "timeout");
-		*--p = '\0';
+		p = strstr(option, "timeout");
+		if (!p) {
+			sd_err("Invalid parameter for timeout");
+			return -1;
+		}
+		p--;
+	}
+	hosts = strndup(option, p - option);
+	if (hosts == NULL) {
+		sd_emerg("OOM");
+		return -1;
 	}
 	sd_debug("version %d.%d.%d, address %s, timeout %d", ZOO_MAJOR_VERSION,
 		 ZOO_MINOR_VERSION, ZOO_PATCH_VERSION, hosts, timeout);
 	zhandle = zookeeper_init(hosts, zk_watcher, timeout, NULL, NULL, 0);
 	if (!zhandle) {
 		sd_err("failed to connect to zk server %s", option);
+		free(hosts);
 		return -1;
 	}
+	free(hosts);
 
 	uatomic_set_false(&stop);
 	uatomic_set_false(&is_master);
-- 
1.7.1




More information about the sheepdog mailing list