]> source.dussan.org Git - rspamd.git/commitdiff
Fix potential issues as found by coverity.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 5 Feb 2015 18:20:22 +0000 (18:20 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 5 Feb 2015 18:20:22 +0000 (18:20 +0000)
19 files changed:
src/client/rspamc.c
src/controller.c
src/libmime/expressions.c
src/libmime/filter.c
src/libmime/images.c
src/libserver/cfg_rcl.c
src/libserver/cfg_utils.c
src/libserver/dynamic_cfg.c
src/libserver/fuzzy_backend.c
src/libserver/symbols_cache.c
src/libstat/backends/mmaped_file.c
src/libutil/addr.c
src/libutil/diff.c
src/libutil/fstring.c
src/libutil/hash.c
src/libutil/util.c
src/lua/lua_buffer.c
src/lua/lua_config.c
src/lua/lua_ip.c

index f114997cfa617da3e23015f10d2809cec1b39393..42b995630a2272484fd0e9d7e03c888d1298b46d 100644 (file)
@@ -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
index 20a066b808af22b2b642456a99d149685256f613..a8dbdec96fcf6dc759dd2b1d823e2b025821d3e7 100644 (file)
@@ -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");
index be64e45da3ed1aead7ed231c9a99f99af0f13324..d1a5df4e9dca9337975f1c47ef4e1a6002a2c43d 100644 (file)
@@ -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;
        }
index 69c1f7c9fd0975c4cd14c529a43fab0d7af24de3..99e88873050c3c80e4fec9d4c0ef1fa635df6306 100644 (file)
@@ -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;
        }
index 3b2ceecd1ab52a17b21c215b5707baf373cfeb00..de542ea18090b0e71b0d3a70fb13af59bae8c41c 100644 (file)
@@ -250,7 +250,7 @@ image_type_str (enum known_image_types type)
                return "BMP";
                break;
        default:
-               return "unknown";
+               break;
        }
 
        return "unknown";
index 14ee4acca8c3e019edf607919057eaae7d5a883d..f35dee92d81c89bf59a05701b9b4175a3839e49e 100644 (file)
@@ -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;
index fef983758232a18421ecd180488937face4627e2..b34873a2cc45155d33e897c98720d71c72b08907 100644 (file)
@@ -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;
        }
index c9e93a5832e8976a5d9cbe203dc514698a7f1509..42174bcaa4858bfe2c43d009c6fcf514e197506a 100644 (file)
@@ -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;
        }
 
index df35e0ccf67a8707bfa5a37054557d970bd8ec54..8de026fd6b4a78b1203ed7695c45efb5947a4116 100644 (file)
@@ -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;
index a50462a0c0e01ab1594628b1a1e49c44a27fd514..5f6a2cd728a13a55dc07c458699f287867b2029c 100644 (file)
@@ -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 |
index aa99b09022bb65e6d500fe906e423b8dc2778b04..0fb386f61c7dcfde916a61247b190f5de550b79f 100644 (file)
@@ -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;
index a276f0cfda85382df87a1868ed8f26fe62b26b43..cb044defab45caae5610d4206f8b2bd0d7f34fd9 100644 (file)
@@ -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 {
index 135c74624cb34905c1a7de4c3232a0126e64ffb4..1e6af6131ac310da700d637d3065e116f20b1a2b 100644 (file)
@@ -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;
index e45b0ded5e0404a3bfdfbff31de23d286dbbce7b..96c57131aee320e84cab6ce52a1eb063881ce9cf 100644 (file)
@@ -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) {
index 05558078d53506fbe722099bd426432481935087..7610a4a9a95232ea0b2398505a2783936f8e831b 100644 (file)
@@ -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);
                }
 
index 9b9d17fb044bf8961e0e0b1d04419c2718561711..465dd92c6a89d4cbd69b91ac925dcc7d4ada5fd3 100644 (file)
@@ -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++;
        }
 
index 5d00db5852592c23b36d12b3415efb4151d369d7..7a23a9579365f122141bcc10ad4fc0bba24200ca 100644 (file)
@@ -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);
index 8e166ac32e316fb2857df44f9866c89b402958fb..8ca525f0ef2a7188cdb94020d4af0303b145a32b 100644 (file)
@@ -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;
index b541727b2b672dc8d6af16a13255f1672ffc0e28..7dea2bfcce6b1b65e015b613252da6fd801c0d8f 100644 (file)
@@ -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,