From 1e08514471896e3b3069cdc25f457036d257cc68 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 5 Feb 2015 18:20:22 +0000 Subject: [PATCH] Fix potential issues as found by coverity. --- src/client/rspamc.c | 14 ++++++----- src/controller.c | 1 + src/libmime/expressions.c | 7 +++--- src/libmime/filter.c | 35 ++++++++++++++-------------- src/libmime/images.c | 2 +- src/libserver/cfg_rcl.c | 8 +------ src/libserver/cfg_utils.c | 3 ++- src/libserver/dynamic_cfg.c | 6 +---- src/libserver/fuzzy_backend.c | 2 ++ src/libserver/symbols_cache.c | 3 ++- src/libstat/backends/mmaped_file.c | 4 ++++ src/libutil/addr.c | 2 +- src/libutil/diff.c | 7 +++--- src/libutil/fstring.c | 7 ++++-- src/libutil/hash.c | 2 +- src/libutil/util.c | 12 +++++++--- src/lua/lua_buffer.c | 2 +- src/lua/lua_config.c | 37 +++++++++++++++--------------- src/lua/lua_ip.c | 4 ++-- 19 files changed, 84 insertions(+), 74 deletions(-) diff --git a/src/client/rspamc.c b/src/client/rspamc.c index f114997cf..42b995630 100644 --- a/src/client/rspamc.c +++ b/src/client/rspamc.c @@ -567,9 +567,8 @@ rspamc_uptime_output (ucl_object_t *obj) hours = seconds / 3600; minutes = seconds / 60 - hours * 60; seconds -= hours * 3600 + minutes * 60; - rspamd_printf ("%L hour%s %L minute%s %L second%s\n", hours, - hours > 1 ? "s" : "", minutes, - minutes > 1 ? "s" : "", + rspamd_printf ("%L hour %L minute%s %L second%s\n", hours, + minutes, minutes > 1 ? "s" : "", seconds, seconds > 1 ? "s" : ""); } } @@ -678,9 +677,12 @@ rspamc_stat_statfile (const ucl_object_t *obj, GString *out) else { rspamd_printf_gstring (out, "Statfile: %s ", symbol); } - rspamd_printf_gstring (out, "length: %HL; free blocks: %HL; total blocks: %HL; " - "free: %.2f%%; learned: %L\n", size, blocks - used_blocks, blocks, - (blocks - used_blocks) * 100.0 / (gdouble)blocks, version); + + if (blocks != 0) { + rspamd_printf_gstring (out, "length: %HL; free blocks: %HL; total blocks: %HL; " + "free: %.2f%%; learned: %L\n", size, blocks - used_blocks, blocks, + (blocks - used_blocks) * 100.0 / (gdouble)blocks, version); + } } static void diff --git a/src/controller.c b/src/controller.c index 20a066b80..a8dbdec96 100644 --- a/src/controller.c +++ b/src/controller.c @@ -528,6 +528,7 @@ rspamd_controller_handle_get_map (struct rspamd_http_connection_entry *conn_ent, /* Read the whole buffer */ if (read (fd, reply->body->str, st.st_size) == -1) { + close (fd); rspamd_http_message_free (reply); msg_err ("cannot read map %s: %s", map->uri, strerror (errno)); rspamd_controller_send_error (conn_ent, 500, "500 map read error"); diff --git a/src/libmime/expressions.c b/src/libmime/expressions.c index be64e45da..d1a5df4e9 100644 --- a/src/libmime/expressions.c +++ b/src/libmime/expressions.c @@ -438,7 +438,8 @@ parse_expression (rspamd_mempool_t * pool, gchar *line) } } if (stack) { - op = delete_expression_stack (&stack); + /* Remove open brace itself */ + delete_expression_stack (&stack); } } else if (*p == '(') { @@ -844,14 +845,14 @@ parse_regexp (rspamd_mempool_t * pool, const gchar *line, gboolean raw_mode) if (result->regexp == NULL || err != NULL) { msg_warn ("could not read regexp: %s while reading regexp %s", - err->message, + err ? err->message : "unknown error", src); return NULL; } if (result->raw_regexp == NULL || err != NULL) { msg_warn ("could not read raw regexp: %s while reading regexp %s", - err->message, + err ? err->message : "unknown error", src); return NULL; } diff --git a/src/libmime/filter.c b/src/libmime/filter.c index 69c1f7c9f..99e888730 100644 --- a/src/libmime/filter.c +++ b/src/libmime/filter.c @@ -827,30 +827,29 @@ rspamd_check_action_metric (struct rspamd_task *task, const ucl_object_t *ms = NULL; int i; - if (metric->actions != NULL) { - if (task->settings) { - ms = ucl_object_find_key (task->settings, metric->name); - } + if (task->settings) { + ms = ucl_object_find_key (task->settings, metric->name); + } - for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) { - double sc; + for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) { + double sc; - action = &metric->actions[i]; - sc = get_specific_action_score (ms, action); + action = &metric->actions[i]; + sc = get_specific_action_score (ms, action); - if (sc < 0) { - continue; - } - if (score >= sc && sc > max_score) { - selected_action = action; - max_score = sc; - } + if (sc < 0) { + continue; + } + if (score >= sc && sc > max_score) { + selected_action = action; + max_score = sc; + } - if (rscore != NULL && i == METRIC_ACTION_REJECT) { - *rscore = sc; - } + if (rscore != NULL && i == METRIC_ACTION_REJECT) { + *rscore = sc; } } + if (selected_action) { return selected_action->action; } diff --git a/src/libmime/images.c b/src/libmime/images.c index 3b2ceecd1..de542ea18 100644 --- a/src/libmime/images.c +++ b/src/libmime/images.c @@ -250,7 +250,7 @@ image_type_str (enum known_image_types type) return "BMP"; break; default: - return "unknown"; + break; } return "unknown"; diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c index 14ee4acca..f35dee92d 100644 --- a/src/libserver/cfg_rcl.c +++ b/src/libserver/cfg_rcl.c @@ -306,7 +306,7 @@ rspamd_rcl_insert_symbol (struct rspamd_config *cfg, struct metric *metric, } val = ucl_object_find_key (obj, "group"); if (val != NULL) { - ucl_object_tostring_safe (val, &group); + group = ucl_object_tostring (val); } val = ucl_object_find_key (obj, "one_shot"); if (val != NULL) { @@ -2017,7 +2017,6 @@ rspamd_config_read (struct rspamd_config *cfg, const gchar *filename, gchar *data; GError *err = NULL; struct rspamd_rcl_section *top, *logger; - gboolean res; struct ucl_parser *parser; if (stat (filename, &st) == -1) { @@ -2050,11 +2049,6 @@ rspamd_config_read (struct rspamd_config *cfg, const gchar *filename, munmap (data, st.st_size); cfg->rcl_obj = ucl_parser_get_object (parser); ucl_parser_free (parser); - res = TRUE; - - if (!res) { - return FALSE; - } top = rspamd_rcl_config_init (); err = NULL; diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c index fef983758..b34873a2c 100644 --- a/src/libserver/cfg_utils.c +++ b/src/libserver/cfg_utils.c @@ -325,6 +325,7 @@ rspamd_config_calculate_checksum (struct rspamd_config *cfg) } if (stat (cfg->cfg_name, &st) == -1) { msg_err ("cannot stat %s: %s", cfg->cfg_name, strerror (errno)); + close (fd); return FALSE; } @@ -365,7 +366,7 @@ rspamd_config_post_load (struct rspamd_config *cfg) clock_getres (CLOCK_REALTIME, &ts); # endif - cfg->clock_res = (gint)log10 (1000000 / ts.tv_nsec); + cfg->clock_res = log10 (1000000. / ts.tv_nsec); if (cfg->clock_res < 0) { cfg->clock_res = 0; } diff --git a/src/libserver/dynamic_cfg.c b/src/libserver/dynamic_cfg.c index c9e93a583..42174bcaa 100644 --- a/src/libserver/dynamic_cfg.c +++ b/src/libserver/dynamic_cfg.c @@ -289,11 +289,7 @@ dump_dynamic_config (struct rspamd_config *cfg) dir = g_path_get_dirname (cfg->dynamic_conf); if (dir == NULL) { - /* Inaccessible path */ - if (dir != NULL) { - g_free (dir); - } - msg_err ("invalid file: %s", cfg->dynamic_conf); + msg_err ("invalid path: %s", cfg->dynamic_conf); return FALSE; } diff --git a/src/libserver/fuzzy_backend.c b/src/libserver/fuzzy_backend.c index df35e0ccf..8de026fd6 100644 --- a/src/libserver/fuzzy_backend.c +++ b/src/libserver/fuzzy_backend.c @@ -524,6 +524,8 @@ rspamd_fuzzy_backend_open (const gchar *path, GError **err) } } + close (fd); + /* Open database */ if ((res = rspamd_fuzzy_backend_open_db (path, err)) == NULL) { GError *tmp = NULL; diff --git a/src/libserver/symbols_cache.c b/src/libserver/symbols_cache.c index a50462a0c..5f6a2cd72 100644 --- a/src/libserver/symbols_cache.c +++ b/src/libserver/symbols_cache.c @@ -366,7 +366,7 @@ register_symbol_common (struct symbols_cache **cache, /* Check whether this item is skipped */ skipped = TRUE; - if (!item->is_callback && + if (!item->is_callback && pcache->cfg && g_hash_table_lookup (pcache->cfg->metrics_symbols, name) == NULL) { cur = g_list_first (pcache->cfg->metrics_list); while (cur) { @@ -595,6 +595,7 @@ init_symbols_cache (rspamd_mempool_t * pool, if (lseek (fd, -(cklen), SEEK_END) == -1) { if (errno == EINVAL) { /* Try to create file */ + close (fd); msg_info ("recreate cache file"); if ((fd = open (filename, O_RDWR | O_TRUNC | O_CREAT, S_IWUSR | diff --git a/src/libstat/backends/mmaped_file.c b/src/libstat/backends/mmaped_file.c index aa99b0902..0fb386f61 100644 --- a/src/libstat/backends/mmaped_file.c +++ b/src/libstat/backends/mmaped_file.c @@ -444,6 +444,10 @@ rspamd_mmaped_file_reindex (rspamd_mmaped_file_ctx * pool, new = rspamd_mmaped_file_open (pool, filename, size, stcf); if (fd == -1 || new == NULL) { + if (fd != -1) { + close (fd); + } + msg_err ("cannot open file: %s", strerror (errno)); g_free (backup); return NULL; diff --git a/src/libutil/addr.c b/src/libutil/addr.c index a276f0cfd..cb044defa 100644 --- a/src/libutil/addr.c +++ b/src/libutil/addr.c @@ -63,7 +63,7 @@ rspamd_ip_check_ipv6 (void) const struct in6_addr ip6_local = IN6ADDR_LOOPBACK_INIT; s = socket (AF_INET6, SOCK_STREAM, 0); - if (s == -1 && errno == EAFNOSUPPORT) { + if (s == -1) { ipv6_status = RSPAMD_IPV6_UNSUPPORTED; } else { diff --git a/src/libutil/diff.c b/src/libutil/diff.c index 135c74624..1e6af6131 100644 --- a/src/libutil/diff.c +++ b/src/libutil/diff.c @@ -268,6 +268,9 @@ _ses (const void *a, gint aoff, gint n, const void *b, gint boff, * - | */ + /* + * XXX: coverity found this code suspicious, needs checking + */ if (m > n) { if (x == u) { _edit (ctx, DIFF_MATCH, aoff, n); @@ -300,7 +303,6 @@ rspamd_diff (const void *a, gint aoff, gint n, const void *b, gint boff, gint m, { struct _ctx ctx; gint d, x, y; - struct diff_edit *e = NULL; GArray *tmp; tmp = g_array_sized_new (FALSE, TRUE, sizeof(gint), dmax); @@ -327,9 +329,6 @@ rspamd_diff (const void *a, gint aoff, gint n, const void *b, gint boff, gint m, g_array_free (tmp, TRUE); return -1; } - if (ses && sn && e) { - *sn = e->op ? ctx.si + 1 : 0; - } g_array_free (tmp, TRUE); return d; diff --git a/src/libutil/fstring.c b/src/libutil/fstring.c index e45b0ded5..96c57131a 100644 --- a/src/libutil/fstring.c +++ b/src/libutil/fstring.c @@ -370,11 +370,12 @@ rspamd_fstrhash (rspamd_fstring_t * str) { size_t i; guint32 hval; - gchar *c = str->begin; + gchar *c; if (str == NULL) { return 0; } + c = str->begin; hval = str->len; for (i = 0; i < str->len; i++, c++) { @@ -391,13 +392,15 @@ rspamd_fstrhash_lc (rspamd_fstring_t * str, gboolean is_utf) { gsize i; guint32 j, hval; - const gchar *p = str->begin, *end = NULL; + const gchar *p, *end = NULL; gchar t; gunichar uc; if (str == NULL) { return 0; } + + p = str->begin; hval = str->len; if (is_utf) { diff --git a/src/libutil/hash.c b/src/libutil/hash.c index 05558078d..7610a4a9a 100644 --- a/src/libutil/hash.c +++ b/src/libutil/hash.c @@ -62,7 +62,7 @@ rspamd_lru_destroy_node (gpointer value) if (elt->hash && elt->hash->value_destroy) { elt->hash->value_destroy (elt->data); } - if (elt->link) { + if (elt->hash && elt->link) { g_queue_delete_link (elt->hash->exp, elt->link); } diff --git a/src/libutil/util.c b/src/libutil/util.c index 9b9d17fb0..465dd92c6 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -129,7 +129,7 @@ static gint rspamd_inet_socket_create (gint type, struct addrinfo *addr, gboolean is_server, gboolean async, GList **list) { - gint fd, r, optlen, on = 1, s_error; + gint fd = -1, r, optlen, on = 1, s_error; struct addrinfo *cur; cur = addr; @@ -425,7 +425,7 @@ rspamd_sockets_list (const gchar *credits, guint16 port, struct sockaddr_un un; struct stat st; struct addrinfo hints, *res; - gint r, fd, serrno; + gint r, fd = -1, serrno; gchar portbuf[8], **strv, **cur; GList *result = NULL, *rcur; @@ -486,9 +486,14 @@ rspamd_sockets_list (const gchar *credits, guint16 port, rspamd_snprintf (portbuf, sizeof (portbuf), "%d", (int)port); if ((r = getaddrinfo (credits, portbuf, &hints, &res)) == 0) { - r = rspamd_inet_socket_create (type, res, is_server, async, &result); + fd = rspamd_inet_socket_create (type, res, is_server, async, &result); freeaddrinfo (res); + if (result == NULL) { + if (fd != -1) { + close (fd); + } + goto err; } } @@ -499,6 +504,7 @@ rspamd_sockets_list (const gchar *credits, guint16 port, goto err; } } + cur++; } diff --git a/src/lua/lua_buffer.c b/src/lua/lua_buffer.c index 5d00db585..7a23a9579 100644 --- a/src/lua/lua_buffer.c +++ b/src/lua/lua_buffer.c @@ -240,7 +240,7 @@ static int lua_io_dispatcher_set_policy (lua_State *L) { struct rspamd_io_dispatcher_s *io_dispatcher = lua_check_io_dispatcher (L); - gint policy, limit = -1; + gint policy, limit = 0; if (io_dispatcher) { policy = lua_tonumber (L, 2); diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index 8e166ac32..8ca525f0e 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -895,25 +895,27 @@ rspamd_register_symbol_fromlua (lua_State *L, { struct lua_callback_data *cd; - cd = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct lua_callback_data)); - cd->cb_is_ref = TRUE; - cd->callback.ref = ref; - cd->L = L; if (name) { + cd = rspamd_mempool_alloc0 (cfg->cfg_pool, + sizeof (struct lua_callback_data)); + cd->cb_is_ref = TRUE; + cd->callback.ref = ref; + cd->L = L; cd->symbol = rspamd_mempool_strdup (cfg->cfg_pool, name); + + register_symbol_common (&cfg->cache, + name, + weight, + priority, + lua_metric_symbol_callback, + cd, + type); + rspamd_mempool_add_destructor (cfg->cfg_pool, + (rspamd_mempool_destruct_t)lua_destroy_cfg_symbol, + cd); } - register_symbol_common (&cfg->cache, - name, - weight, - priority, - lua_metric_symbol_callback, - cd, - type); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)lua_destroy_cfg_symbol, - cd); + } static gint @@ -1097,7 +1099,7 @@ lua_config_newindex (lua_State *L) name = luaL_checkstring (L, 2); - if (name != NULL && lua_gettop (L) > 2) { + if (cfg != NULL && name != NULL && lua_gettop (L) > 2) { if (lua_type (L, 3) == LUA_TFUNCTION) { /* Normal symbol from just a function */ lua_pushvalue (L, 3); @@ -1391,8 +1393,7 @@ lua_trie_search_text (lua_State *L) gboolean found = FALSE; if (trie) { - text = luaL_checkstring (L, 2); - len = strlen (text); + text = luaL_checklstring (L, 2, &len); if (text) { lua_newtable (L); pos = text; diff --git a/src/lua/lua_ip.c b/src/lua/lua_ip.c index b541727b2..7dea2bfcc 100644 --- a/src/lua/lua_ip.c +++ b/src/lua/lua_ip.c @@ -501,7 +501,7 @@ lua_ip_apply_mask (lua_State *L) guint32 umsk, *p; mask = lua_tonumber (L, 2); - if (mask > 0 && ip->is_valid) { + if (mask > 0 && ip != NULL && ip->is_valid) { if (ip->addr.af == AF_INET && mask <= 32) { nip = lua_ip_new (L, ip); umsk = htonl (G_MAXUINT32 << (32 - mask)); @@ -536,7 +536,7 @@ lua_ip_equal (lua_State *L) *ip2 = lua_check_ip (L, 2); gboolean res = FALSE; - if (ip1->is_valid && ip2->is_valid) { + if (ip1 && ip2 && ip1->is_valid && ip2->is_valid) { if (ip1->addr.af == ip2->addr.af) { if (ip1->addr.af == AF_INET) { if (memcmp(&ip1->addr.addr.s4.sin_addr, -- 2.39.5