dovecot-2.2: lib-dict: Fixed error handling on initialization.

dovecot at dovecot.org dovecot at dovecot.org
Wed Oct 3 04:14:44 EEST 2012


details:   http://hg.dovecot.org/dovecot-2.2/rev/9f86c18e6b2c
changeset: 15184:9f86c18e6b2c
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Oct 03 04:14:33 2012 +0300
description:
lib-dict: Fixed error handling on initialization.

diffstat:

 src/lib-dict/dict-file.c            |   6 ++-
 src/lib-dict/dict-memcached-ascii.c |  66 +++++++++++++++++++++-------------
 src/lib-dict/dict-memcached.c       |  21 ++++++++--
 src/lib-dict/dict-redis.c           |  70 ++++++++++++++++++++++--------------
 4 files changed, 104 insertions(+), 59 deletions(-)

diffs (256 lines):

diff -r 04cd35e86025 -r 9f86c18e6b2c src/lib-dict/dict-file.c
--- a/src/lib-dict/dict-file.c	Wed Oct 03 04:09:23 2012 +0300
+++ b/src/lib-dict/dict-file.c	Wed Oct 03 04:14:33 2012 +0300
@@ -73,8 +73,12 @@
 			dict->lock_method = FILE_LOCK_METHOD_FCNTL;
 		else if (strcmp(p, "lock=flock") == 0)
 			dict->lock_method = FILE_LOCK_METHOD_FLOCK;
-		else
+		else {
 			i_error("dict file: Invalid parameter: %s", p+1);
+			i_free(dict->path);
+			i_free(dict);
+			return -1;
+		}
 	}
 	dict->dict = *driver;
 	dict->hash_pool = pool_alloconly_create("file dict", 1024);
diff -r 04cd35e86025 -r 9f86c18e6b2c src/lib-dict/dict-memcached-ascii.c
--- a/src/lib-dict/dict-memcached-ascii.c	Wed Oct 03 04:09:23 2012 +0300
+++ b/src/lib-dict/dict-memcached-ascii.c	Wed Oct 03 04:14:33 2012 +0300
@@ -340,6 +340,7 @@
 	struct memcached_ascii_dict *dict;
 	const char *const *args;
 	struct ioloop *old_ioloop = current_ioloop;
+	int ret = 0;
 
 	if (memcached_ascii_connections == NULL) {
 		memcached_ascii_connections =
@@ -352,41 +353,54 @@
 		i_unreached();
 	dict->port = MEMCACHED_DEFAULT_PORT;
 	dict->timeout_msecs = MEMCACHED_DEFAULT_LOOKUP_TIMEOUT_MSECS;
+	dict->key_prefix = i_strdup("");
+
+	args = t_strsplit(uri, ":");
+	for (; *args != NULL; args++) {
+		if (strncmp(*args, "host=", 5) == 0) {
+			if (net_addr2ip(*args+5, &dict->ip) < 0) {
+				i_error("Invalid IP: %s", *args+5);
+				ret = -1;
+			}
+		} else if (strncmp(*args, "port=", 5) == 0) {
+			if (str_to_uint(*args+5, &dict->port) < 0) {
+				i_error("Invalid port: %s", *args+5);
+				ret = -1;
+			}
+		} else if (strncmp(*args, "prefix=", 7) == 0) {
+			i_free(dict->key_prefix);
+			dict->key_prefix = i_strdup(*args + 7);
+		} else if (strncmp(*args, "timeout_msecs=", 14) == 0) {
+			if (str_to_uint(*args+14, &dict->timeout_msecs) < 0) {
+				i_error("Invalid timeout_msecs: %s", *args+14);
+				ret = -1;
+			}
+		} else {
+			i_error("Unknown parameter: %s", *args);
+			ret = -1;
+		}
+	}
+	if (ret < 0) {
+		i_free(dict->key_prefix);
+		i_free(dict);
+		return -1;
+	}
+
+	connection_init_client_ip(memcached_ascii_connections, &dict->conn.conn,
+				  &dict->ip, dict->port);
+	dict->dict = *driver;
+	dict->conn.reply_str = str_new(default_pool, 256);
+	dict->conn.dict = dict;
+
 	if (strchr(username, DICT_USERNAME_SEPARATOR) == NULL)
 		dict->username = i_strdup(username);
 	else {
 		/* escape the username */
 		dict->username = i_strdup(memcached_ascii_escape_username(username));
 	}
-	dict->key_prefix = i_strdup("");
 	i_array_init(&dict->input_states, 4);
 	i_array_init(&dict->replies, 4);
 
-	args = t_strsplit(uri, ":");
-	for (; *args != NULL; args++) {
-		if (strncmp(*args, "host=", 5) == 0) {
-			if (net_addr2ip(*args+5, &dict->ip) < 0)
-				i_error("Invalid IP: %s", *args+5);
-		} else if (strncmp(*args, "port=", 5) == 0) {
-			if (str_to_uint(*args+5, &dict->port) < 0)
-				i_error("Invalid port: %s", *args+5);
-		} else if (strncmp(*args, "prefix=", 7) == 0) {
-			i_free(dict->key_prefix);
-			dict->key_prefix = i_strdup(*args + 7);
-		} else if (strncmp(*args, "timeout_msecs=", 14) == 0) {
-			if (str_to_uint(*args+14, &dict->timeout_msecs) < 0)
-				i_error("Invalid timeout_msecs: %s", *args+14);
-		} else {
-			i_error("Unknown parameter: %s", *args);
-		}
-	}
-	connection_init_client_ip(memcached_ascii_connections, &dict->conn.conn,
-				  &dict->ip, dict->port);
-
-	dict->dict = *driver;
-	dict->conn.reply_str = str_new(default_pool, 256);
-	dict->conn.dict = dict;
-
 	dict->ioloop = io_loop_create();
 	current_ioloop = old_ioloop;
 	*dict_r = &dict->dict;
diff -r 04cd35e86025 -r 9f86c18e6b2c src/lib-dict/dict-memcached.c
--- a/src/lib-dict/dict-memcached.c	Wed Oct 03 04:09:23 2012 +0300
+++ b/src/lib-dict/dict-memcached.c	Wed Oct 03 04:14:33 2012 +0300
@@ -175,6 +175,7 @@
 {
 	struct memcached_dict *dict;
 	const char *const *args;
+	int ret = 0;
 
 	if (memcached_connections == NULL) {
 		memcached_connections =
@@ -192,24 +193,36 @@
 	args = t_strsplit(uri, ":");
 	for (; *args != NULL; args++) {
 		if (strncmp(*args, "host=", 5) == 0) {
-			if (net_addr2ip(*args+5, &dict->ip) < 0)
+			if (net_addr2ip(*args+5, &dict->ip) < 0) {
 				i_error("Invalid IP: %s", *args+5);
+				ret = -1;
+			}
 		} else if (strncmp(*args, "port=", 5) == 0) {
-			if (str_to_uint(*args+5, &dict->port) < 0)
+			if (str_to_uint(*args+5, &dict->port) < 0) {
 				i_error("Invalid port: %s", *args+5);
+				ret = -1;
+			}
 		} else if (strncmp(*args, "prefix=", 7) == 0) {
 			i_free(dict->key_prefix);
 			dict->key_prefix = i_strdup(*args + 7);
 		} else if (strncmp(*args, "timeout_msecs=", 14) == 0) {
-			if (str_to_uint(*args+14, &dict->timeout_msecs) < 0)
+			if (str_to_uint(*args+14, &dict->timeout_msecs) < 0) {
 				i_error("Invalid timeout_msecs: %s", *args+14);
+				ret = -1;
+			}
 		} else {
 			i_error("Unknown parameter: %s", *args);
+			ret = -1;
 		}
 	}
+	if (ret < 0) {
+		i_free(dict->key_prefix);
+		i_free(dict);
+		return -1;
+	}
+
 	connection_init_client_ip(memcached_connections, &dict->conn.conn,
 				  &dict->ip, dict->port);
-
 	dict->dict = *driver;
 	dict->conn.cmd = buffer_create_dynamic(default_pool, 256);
 	dict->conn.dict = dict;
diff -r 04cd35e86025 -r 9f86c18e6b2c src/lib-dict/dict-redis.c
--- a/src/lib-dict/dict-redis.c	Wed Oct 03 04:09:23 2012 +0300
+++ b/src/lib-dict/dict-redis.c	Wed Oct 03 04:14:33 2012 +0300
@@ -306,6 +306,7 @@
 {
 	struct redis_dict *dict;
 	const char *const *args;
+	int ret = 0;
 
 	if (redis_connections == NULL) {
 		redis_connections =
@@ -318,42 +319,55 @@
 		i_unreached();
 	dict->port = REDIS_DEFAULT_PORT;
 	dict->timeout_msecs = REDIS_DEFAULT_LOOKUP_TIMEOUT_MSECS;
+	dict->key_prefix = i_strdup("");
+
+	args = t_strsplit(uri, ":");
+	for (; *args != NULL; args++) {
+		if (strncmp(*args, "host=", 5) == 0) {
+			if (net_addr2ip(*args+5, &dict->ip) < 0) {
+				i_error("Invalid IP: %s", *args+5);
+				ret = -1;
+			}
+		} else if (strncmp(*args, "port=", 5) == 0) {
+			if (str_to_uint(*args+5, &dict->port) < 0) {
+				i_error("Invalid port: %s", *args+5);
+				ret = -1;
+			}
+		} else if (strncmp(*args, "prefix=", 7) == 0) {
+			i_free(dict->key_prefix);
+			dict->key_prefix = i_strdup(*args + 7);
+		} else if (strncmp(*args, "timeout_msecs=", 14) == 0) {
+			if (str_to_uint(*args+14, &dict->timeout_msecs) < 0) {
+				i_error("Invalid timeout_msecs: %s", *args+14);
+				ret = -1;
+			}
+		} else {
+			i_error("Unknown parameter: %s", *args);
+			ret = -1;
+		}
+	}
+	if (ret < 0) {
+		i_free(dict->key_prefix);
+		i_free(dict);
+		return -1;
+	}
+	connection_init_client_ip(redis_connections, &dict->conn.conn,
+				  &dict->ip, dict->port);
+	dict->dict = *driver;
+	dict->conn.last_reply = str_new(default_pool, 256);
+	dict->conn.dict = dict;
+
+	i_array_init(&dict->input_states, 4);
+	i_array_init(&dict->replies, 4);
 	if (strchr(username, DICT_USERNAME_SEPARATOR) == NULL)
 		dict->username = i_strdup(username);
 	else {
 		/* escape the username */
 		dict->username = i_strdup(redis_escape_username(username));
 	}
-	dict->key_prefix = i_strdup("");
-	i_array_init(&dict->input_states, 4);
-	i_array_init(&dict->replies, 4);
 
-	args = t_strsplit(uri, ":");
-	for (; *args != NULL; args++) {
-		if (strncmp(*args, "host=", 5) == 0) {
-			if (net_addr2ip(*args+5, &dict->ip) < 0)
-				i_error("Invalid IP: %s", *args+5);
-		} else if (strncmp(*args, "port=", 5) == 0) {
-			if (str_to_uint(*args+5, &dict->port) < 0)
-				i_error("Invalid port: %s", *args+5);
-		} else if (strncmp(*args, "prefix=", 7) == 0) {
-			i_free(dict->key_prefix);
-			dict->key_prefix = i_strdup(*args + 7);
-		} else if (strncmp(*args, "timeout_msecs=", 14) == 0) {
-			if (str_to_uint(*args+14, &dict->timeout_msecs) < 0)
-				i_error("Invalid timeout_msecs: %s", *args+14);
-		} else {
-			i_error("Unknown parameter: %s", *args);
-		}
-	}
-	connection_init_client_ip(redis_connections, &dict->conn.conn,
-				  &dict->ip, dict->port);
-
-	dict->dict = *driver;
-	dict->conn.last_reply = str_new(default_pool, 256);
-	dict->conn.dict = dict;
 	*dict_r = &dict->dict;
-	return -1;
+	return 0;
 }
 
 static void redis_dict_deinit(struct dict *_dict)


More information about the dovecot-cvs mailing list