Mercurial > dovecot > core-2.2
changeset 15184:9f86c18e6b2c
lib-dict: Fixed error handling on initialization.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 03 Oct 2012 04:14:33 +0300 |
parents | 04cd35e86025 |
children | 62a14c9ae6c4 |
files | src/lib-dict/dict-file.c src/lib-dict/dict-memcached-ascii.c src/lib-dict/dict-memcached.c src/lib-dict/dict-redis.c |
diffstat | 4 files changed, 104 insertions(+), 59 deletions(-) [+] |
line wrap: on
line diff
--- 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);
--- 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;
--- 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;
--- 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)