From: Stephan Bosch Date: Sat, 29 Aug 2015 11:30:37 +0000 (+0300) Subject: Removed all invocations of atoi(). X-Git-Tag: 2.2.19.rc1~158 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c93aca832ee532010ead91b85fa9f614132e1be2;p=thirdparty%2Fdovecot%2Fcore.git Removed all invocations of atoi(). --- diff --git a/src/auth/db-ldap.c b/src/auth/db-ldap.c index 1476fa9612..08a1c596f6 100644 --- a/src/auth/db-ldap.c +++ b/src/auth/db-ldap.c @@ -1103,8 +1103,7 @@ static void db_ldap_set_options(struct ldap_connection *conn) db_ldap_set_opt(conn, conn->ld, LDAP_OPT_DEREF, &conn->set.ldap_deref, "deref", conn->set.deref); #ifdef LDAP_OPT_DEBUG_LEVEL - value = atoi(conn->set.debug_level); - if (value != 0) { + if (str_to_int(conn->set.debug_level, &value) >= 0 && value != 0) { db_ldap_set_opt(conn, NULL, LDAP_OPT_DEBUG_LEVEL, &value, "debug_level", conn->set.debug_level); } @@ -1143,10 +1142,15 @@ static void db_ldap_init_ld(struct ldap_connection *conn) int db_ldap_connect(struct ldap_connection *conn) { - bool debug = atoi(conn->set.debug_level) > 0; + int debug_level; + bool debug; struct timeval start, end; int ret; + debug = FALSE; + if (str_to_int(conn->set.debug_level, &debug_level) >= 0) + debug = debug_level > 0; + if (conn->conn_state != LDAP_CONN_STATE_DISCONNECTED) return 0; diff --git a/src/auth/mech-digest-md5.c b/src/auth/mech-digest-md5.c index 237817cb7c..98f7326549 100644 --- a/src/auth/mech-digest-md5.c +++ b/src/auth/mech-digest-md5.c @@ -334,12 +334,19 @@ static bool auth_handle_response(struct digest_auth_request *request, } if (strcmp(key, "nc") == 0) { + unsigned int nc; + if (request->nonce_count != NULL) { *error = "nonce-count must not exist more than once"; return FALSE; } - if (atoi(value) != 1) { + if (str_to_uint(value, &nc) < 0) { + *error = "nonce-count value invalid"; + return FALSE; + } + + if (nc != 1) { *error = "re-auth not supported currently"; return FALSE; } diff --git a/src/auth/passdb-blocking.c b/src/auth/passdb-blocking.c index 6cc4eb33bc..27841302c8 100644 --- a/src/auth/passdb-blocking.c +++ b/src/auth/passdb-blocking.c @@ -38,20 +38,25 @@ auth_worker_reply_parse(struct auth_request *request, const char *reply) } if (strcmp(*args, "FAIL") == 0 && args[1] != NULL) { + int result; /* FAIL \t result [\t user \t password [\t extra]] */ - ret = atoi(args[1]); - if (ret == PASSDB_RESULT_OK) { + if (str_to_int(args[1], &result) < 0) { /* shouldn't happen */ - } else if (args[2] == NULL) { - /* internal failure most likely */ - return ret; - } else if (args[3] != NULL) { - if (*args[2] != '\0') { - auth_request_set_field(request, "user", - args[2], NULL); + } else { + ret = (enum passdb_result)result; + if (ret == PASSDB_RESULT_OK) { + /* shouldn't happen */ + } else if (args[2] == NULL) { + /* internal failure most likely */ + return ret; + } else if (args[3] != NULL) { + if (*args[2] != '\0') { + auth_request_set_field(request, "user", + args[2], NULL); + } + auth_worker_reply_parse_args(request, args + 3); + return ret; } - auth_worker_reply_parse_args(request, args + 3); - return ret; } } diff --git a/src/dict/dict-connection.c b/src/dict/dict-connection.c index 81b95de4d1..a9860cfa1c 100644 --- a/src/dict/dict-connection.c +++ b/src/dict/dict-connection.c @@ -21,6 +21,7 @@ static int dict_connection_parse_handshake(struct dict_connection *conn, const char *line) { const char *username, *name, *value_type; + unsigned int value_type_num; if (*line++ != DICT_PROTOCOL_CMD_HELLO) return -1; @@ -42,7 +43,11 @@ static int dict_connection_parse_handshake(struct dict_connection *conn, if (*line++ != '\t') return -1; - conn->value_type = atoi(t_strdup_until(value_type, line - 1)); + if (str_to_uint(t_strdup_until(value_type, line - 1), &value_type_num) < 0) + return -1; + if (value_type_num >= DICT_DATA_TYPE_LAST) + return -1; + conn->value_type = (enum dict_data_type)value_type_num; /* get username */ username = line; diff --git a/src/director/director-connection.c b/src/director/director-connection.c index 06714169e9..a068fcb030 100644 --- a/src/director/director-connection.c +++ b/src/director/director-connection.c @@ -44,7 +44,6 @@ #include "user-directory.h" #include "director-connection.h" -#include #include #define MAX_INBUF_SIZE 1024 @@ -1145,6 +1144,8 @@ static int director_connection_handle_handshake(struct director_connection *conn, const char *cmd, const char *const *args) { + unsigned int major_version; + /* both incoming and outgoing connections get VERSION and ME */ if (strcmp(cmd, "VERSION") == 0 && str_array_length(args) >= 3) { if (strcmp(args[0], DIRECTOR_VERSION_NAME) != 0) { @@ -1152,13 +1153,17 @@ director_connection_handle_handshake(struct director_connection *conn, "(%s vs %s)", conn->name, args[0], DIRECTOR_VERSION_NAME); return -1; - } else if (atoi(args[1]) != DIRECTOR_VERSION_MAJOR) { + } else if (str_to_uint(args[1], &major_version) < 0 || + str_to_uint(args[2], &conn->minor_version) < 0) { + i_error("director(%s): Invalid protocol version: " + "%s.%s", conn->name, args[1], args[2]); + return -1; + } else if (major_version != DIRECTOR_VERSION_MAJOR) { i_error("director(%s): Incompatible protocol version: " - "%u vs %u", conn->name, atoi(args[1]), + "%u vs %u", conn->name, major_version, DIRECTOR_VERSION_MAJOR); return -1; } - conn->minor_version = atoi(args[2]); conn->version_received = TRUE; if (conn->done_pending) { if (director_connection_send_done(conn) < 0) @@ -1306,7 +1311,10 @@ static bool director_connection_sync(struct director_connection *conn, return FALSE; } if (args[3] != NULL) { - minor_version = atoi(args[3]); + if (str_to_uint(args[3], &minor_version) < 0) { + director_cmd_error(conn, "Invalid parameters"); + return FALSE; + } if (args[4] != NULL && str_to_uint(args[4], ×tamp) < 0) { director_cmd_error(conn, "Invalid parameters"); return FALSE; diff --git a/src/doveadm/doveadm-dump-index.c b/src/doveadm/doveadm-dump-index.c index d0ccc7a0f7..a87ae0d032 100644 --- a/src/doveadm/doveadm-dump-index.c +++ b/src/doveadm/doveadm-dump-index.c @@ -668,8 +668,10 @@ static void cmd_dump_index(int argc ATTR_UNUSED, char *argv[]) if (index == NULL || mail_index_open(index, MAIL_INDEX_OPEN_FLAG_READONLY) <= 0) i_fatal("Couldn't open index %s", argv[1]); - if (argv[2] != NULL) - uid = atoi(argv[2]); + if (argv[2] != NULL) { + if (str_to_uint(argv[2], &uid) < 0) + i_fatal("Invalid uid number %s", argv[2]); + } view = mail_index_view_open(index); cache_view = mail_cache_view_open(index->cache, view); diff --git a/src/doveadm/doveadm-who.c b/src/doveadm/doveadm-who.c index b237bfc7a0..2e689fbbb6 100644 --- a/src/doveadm/doveadm-who.c +++ b/src/doveadm/doveadm-who.c @@ -68,7 +68,8 @@ static int who_parse_line(const char *line, struct who_line *line_r) line_r->username = strchr(p, '/'); if (line_r->username == NULL) return -1; - line_r->refcount = atoi(refcount_str); + if (str_to_uint(refcount_str, &line_r->refcount) < 0) + return -1; ip_str = t_strdup_until(p, line_r->username++); (void)net_addr2ip(ip_str, &line_r->ip); return 0; diff --git a/src/imap-login/client.c b/src/imap-login/client.c index 26ffc4cfa4..fa7745f8b8 100644 --- a/src/imap-login/client.c +++ b/src/imap-login/client.c @@ -21,8 +21,6 @@ #include "imap-proxy.h" #include "imap-login-settings.h" -#include - #if LOGIN_MAX_INBUF_SIZE < 1024+2 # error LOGIN_MAX_INBUF_SIZE too short to fit all ID command parameters #endif @@ -158,7 +156,9 @@ client_update_info(struct imap_client *client, } else if (strcasecmp(key, "x-connected-port") == 0) { (void)net_str2port(value, &client->common.local_port); } else if (strcasecmp(key, "x-proxy-ttl") == 0) { - client->common.proxy_ttl = atoi(value); + if (str_to_uint(value, &client->common.proxy_ttl) < 0) { + /* nothing */ + } } else if (strcasecmp(key, "x-session-id") == 0 || strcasecmp(key, "x-session-ext-id") == 0) { if (strlen(value) <= LOGIN_MAX_SESSION_ID_LEN) { diff --git a/src/lib-dict/dict.h b/src/lib-dict/dict.h index b7afc72ead..66759fe63a 100644 --- a/src/lib-dict/dict.h +++ b/src/lib-dict/dict.h @@ -23,7 +23,8 @@ enum dict_iterate_flags { enum dict_data_type { DICT_DATA_TYPE_STRING = 0, - DICT_DATA_TYPE_UINT32 + DICT_DATA_TYPE_UINT32, + DICT_DATA_TYPE_LAST }; struct dict_settings { diff --git a/src/lib-master/master-service.c b/src/lib-master/master-service.c index c7d87bb6aa..296e5b0faf 100644 --- a/src/lib-master/master-service.c +++ b/src/lib-master/master-service.c @@ -164,7 +164,8 @@ master_service_init(const char *name, enum master_service_flags flags, int count; value = getenv("SOCKET_COUNT"); - count = value == NULL ? 0 : atoi(value); + if (value == NULL || str_to_uint(value, &count) < 0) + count = 0; fd_debug_verify_leaks(MASTER_LISTEN_FD_FIRST + count, 1024); } #endif @@ -222,8 +223,8 @@ master_service_init(const char *name, enum master_service_flags flags, /* listener configuration */ value = getenv("SOCKET_COUNT"); - if (value != NULL) - service->socket_count = atoi(value); + if (value != NULL && str_to_uint(value, &service->socket_count) < 0) + i_fatal("Invalid SOCKET_COUNT environment"); T_BEGIN { master_service_init_socket_listeners(service); } T_END; diff --git a/src/lib-sql/driver-mysql.c b/src/lib-sql/driver-mysql.c index 348e840c6a..1285367bb6 100644 --- a/src/lib-sql/driver-mysql.c +++ b/src/lib-sql/driver-mysql.c @@ -184,9 +184,10 @@ static void driver_mysql_parse_connect_string(struct mysql_db *db, else if (strcmp(name, "port") == 0) { if (net_str2port(value, &db->port) < 0) i_fatal("mysql: Invalid port number: %s", value); - } else if (strcmp(name, "client_flags") == 0) - db->client_flags = atoi(value); - else if (strcmp(name, "ssl_cert") == 0) + } else if (strcmp(name, "client_flags") == 0) { + if (str_to_uint(value, &db->client_flags) < 9) + i_fatal("mysql: Invalid client flags: %s", value); + } else if (strcmp(name, "ssl_cert") == 0) field = &db->ssl_cert; else if (strcmp(name, "ssl_key") == 0) field = &db->ssl_key; diff --git a/src/lib-sql/driver-pgsql.c b/src/lib-sql/driver-pgsql.c index 9b0e22f7a6..95fa0052be 100644 --- a/src/lib-sql/driver-pgsql.c +++ b/src/lib-sql/driver-pgsql.c @@ -898,7 +898,9 @@ transaction_update_callback(struct sql_result *result, } else if (query->affected_rows != NULL) { struct pgsql_result *pg_result = (struct pgsql_result *)result; - *query->affected_rows = atoi(PQcmdTuples(pg_result->pgres)); + if (str_to_uint(PQcmdTuples(pg_result->pgres), + query->affected_rows) < 0) + i_unreached(); } driver_pgsql_transaction_unref(ctx); } @@ -969,8 +971,9 @@ driver_pgsql_transaction_commit_multi(struct pgsql_transaction_context *ctx) struct pgsql_result *pg_result = (struct pgsql_result *)result; - *query->affected_rows = - atoi(PQcmdTuples(pg_result->pgres)); + if (str_to_uint(PQcmdTuples(pg_result->pgres), + query->affected_rows) < 0) + i_unreached(); } sql_result_unref(result); } @@ -1010,8 +1013,9 @@ driver_pgsql_try_commit_s(struct pgsql_transaction_context *ctx, struct pgsql_result *pg_result = (struct pgsql_result *)result; - *single_query->affected_rows = - atoi(PQcmdTuples(pg_result->pgres)); + if (str_to_uint(PQcmdTuples(pg_result->pgres), + single_query->affected_rows) < 0) + i_unreached(); } } if (result != NULL) diff --git a/src/lib-storage/mail-storage-service.c b/src/lib-storage/mail-storage-service.c index 5c82f972b2..9a9c94e618 100644 --- a/src/lib-storage/mail-storage-service.c +++ b/src/lib-storage/mail-storage-service.c @@ -270,9 +270,11 @@ user_reply_handle(struct mail_storage_service_ctx *ctx, p_strdup(user->pool, line + 19); } else if (strncmp(line, "nice=", 5) == 0) { #ifdef HAVE_SETPRIORITY - int n = atoi(line + 5); - - if (n != 0) { + int n; + if (str_to_int(line + 5, &n) < 0) { + i_error("userdb returned invalid nice value %s", + line + 5); + } else if (n != 0) { if (setpriority(PRIO_PROCESS, 0, n) < 0) i_error("setpriority(%d) failed: %m", n); } diff --git a/src/lmtp/commands.c b/src/lmtp/commands.c index 9fdab4e240..bc66436296 100644 --- a/src/lmtp/commands.c +++ b/src/lmtp/commands.c @@ -34,8 +34,6 @@ #include "commands.h" #include "lmtp-proxy.h" -#include - #define ERRSTR_TEMP_MAILBOX_FAIL "451 4.3.0 <%s> Temporary internal error" #define ERRSTR_TEMP_USERDB_FAIL_PREFIX "451 4.3.0 <%s> " #define ERRSTR_TEMP_USERDB_FAIL \ @@ -245,9 +243,13 @@ client_proxy_rcpt_parse_fields(struct lmtp_proxy_rcpt_settings *set, return FALSE; } port_set = TRUE; - } else if (strcmp(key, "proxy_timeout") == 0) - set->timeout_msecs = atoi(value)*1000; - else if (strcmp(key, "protocol") == 0) { + } else if (strcmp(key, "proxy_timeout") == 0) { + if (str_to_uint(value, &set->timeout_msecs) < 0) { + i_error("proxy: Invalid proxy_timeout value %s", value); + return FALSE; + } + set->timeout_msecs *= 1000; + } else if (strcmp(key, "protocol") == 0) { if (strcmp(value, "lmtp") == 0) set->protocol = LMTP_CLIENT_PROTOCOL_LMTP; else if (strcmp(value, "smtp") == 0) { diff --git a/src/login-common/client-common-auth.c b/src/login-common/client-common-auth.c index 677c1935a8..90941c56d0 100644 --- a/src/login-common/client-common-auth.c +++ b/src/login-common/client-common-auth.c @@ -14,8 +14,6 @@ #include "master-service-ssl-settings.h" #include "client-common.h" -#include - #define PROXY_FAILURE_MSG "Account is temporarily unavailable." #define PROXY_DEFAULT_TIMEOUT_MSECS (1000*30) @@ -107,11 +105,18 @@ static void client_auth_parse_args(struct client *client, reply_r->destuser = value; else if (strcmp(key, "pass") == 0) reply_r->password = value; - else if (strcmp(key, "proxy_timeout") == 0) - reply_r->proxy_timeout_msecs = 1000*atoi(value); - else if (strcmp(key, "proxy_refresh") == 0) - reply_r->proxy_refresh_secs = atoi(value); - else if (strcmp(key, "proxy_mech") == 0) + else if (strcmp(key, "proxy_timeout") == 0) { + if (str_to_uint(value, &reply_r->proxy_timeout_msecs) < 0) { + i_error("BUG: Auth service returned invalid " + "proxy_timeout value: %s", value); + } + reply_r->proxy_timeout_msecs *= 1000; + } else if (strcmp(key, "proxy_refresh") == 0) { + if (str_to_uint(value, &reply_r->proxy_refresh_secs) < 0) { + i_error("BUG: Auth service returned invalid " + "proxy_refresh value: %s", value); + } + } else if (strcmp(key, "proxy_mech") == 0) reply_r->proxy_mech = value; else if (strcmp(key, "proxy_nopipelining") == 0) reply_r->proxy_nopipelining = TRUE; diff --git a/src/master/main.c b/src/master/main.c index af21c92c27..eb300bf43e 100644 --- a/src/master/main.c +++ b/src/master/main.c @@ -245,10 +245,13 @@ static bool pid_file_read(const char *path, pid_t *pid_r) if (buf[ret-1] == '\n') ret--; buf[ret] = '\0'; - *pid_r = atoi(buf); - - found = !(*pid_r == getpid() || - (kill(*pid_r, 0) < 0 && errno == ESRCH)); + if (str_to_pid(buf, pid_r) < 0) { + i_error("PID file contains invalid PID value"); + found = FALSE; + } else { + found = !(*pid_r == getpid() || + (kill(*pid_r, 0) < 0 && errno == ESRCH)); + } } i_close_fd(&fd); return found; diff --git a/src/util/script.c b/src/util/script.c index 72f41bed38..15d9ed624c 100644 --- a/src/util/script.c +++ b/src/util/script.c @@ -144,7 +144,10 @@ static bool client_exec_script(struct master_service_connection *conn) script_verify_version(*args); args++; if (*args != NULL) { if (strncmp(*args, "alarm=", 6) == 0) { - alarm(atoi(*args + 6)); + unsigned int seconds; + if (str_to_uint(*args + 6, &seconds) < 0) + i_fatal("invalid alarm option"); + alarm(seconds); args++; } if (strcmp(*args, "noreply") == 0) {