From: Vsevolod Stakhov Date: Tue, 6 Oct 2015 14:41:16 +0000 (+0100) Subject: Start miration to the new strings. X-Git-Tag: 1.0.5~67^2~13 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=aac916db77b6ae58b7c0186d764e97a25482017c;p=rspamd.git Start miration to the new strings. --- diff --git a/src/libserver/task.h b/src/libserver/task.h index 16c3d4a70..02f6f9ea0 100644 --- a/src/libserver/task.h +++ b/src/libserver/task.h @@ -128,16 +128,13 @@ struct rspamd_task { gchar *hostname; /**< hostname reported by MTA */ GHashTable *request_headers; /**< HTTP headers in a request */ GHashTable *reply_headers; /**< Custom reply headers */ - struct { - const gchar *start; - gsize len; - } msg; /**< message buffer */ + rspamd_ftok_t msg; /**< message buffer */ struct rspamd_http_connection *http_conn; /**< HTTP server connection */ struct rspamd_async_session * s; /**< async session object */ GMimeMessage *message; /**< message, parsed with GMime */ GPtrArray *parts; /**< list of parsed parts */ GPtrArray *text_parts; /**< list of text parts */ - rspamd_fstring_t raw_headers_content; /**< list of raw headers */ + rspamd_ftok_t raw_headers_content; /**< list of raw headers */ GPtrArray *received; /**< list of received headers */ GHashTable *urls; /**< list of parsed urls */ GHashTable *emails; /**< list of parsed emails */ diff --git a/src/libutil/fstring.c b/src/libutil/fstring.c index 9e96aac3c..ba580cd8b 100644 --- a/src/libutil/fstring.c +++ b/src/libutil/fstring.c @@ -48,7 +48,7 @@ rspamd_fstring_t * rspamd_fstring_sized_new (gsize initial_size) { rspamd_fstring_t *s; - gsize real_size = MAX(default_initial_size, initial_size); + gsize real_size = MAX (default_initial_size, initial_size); g_assert (posix_memalign ((void **)&s, 16, real_size + sizeof (*s)) == 0); s->len = 0; @@ -57,6 +57,20 @@ rspamd_fstring_sized_new (gsize initial_size) return s; } +rspamd_fstring_t * +rspamd_fstring_new_init (const gchar *init, gsize len) +{ + rspamd_fstring_t *s; + gsize real_size = MAX (default_initial_size, len); + + g_assert (posix_memalign ((void **) &s, 16, real_size + sizeof (*s)) == 0); + s->len = len; + s->allocated = real_size; + memcpy (s->str, init, len); + + return s; +} + void rspamd_fstring_free (rspamd_fstring_t *str) { diff --git a/src/libutil/fstring.h b/src/libutil/fstring.h index 9fdf47211..0d257633b 100644 --- a/src/libutil/fstring.h +++ b/src/libutil/fstring.h @@ -55,6 +55,11 @@ rspamd_fstring_t* rspamd_fstring_new (void); */ rspamd_fstring_t *rspamd_fstring_sized_new (gsize initial_size); +/** + * Create new fixed length string and initialize it with the initial data + */ +rspamd_fstring_t *rspamd_fstring_new_init (const gchar *init, gsize len); + /** * Free fixed length string */ diff --git a/src/libutil/mem_pool.c b/src/libutil/mem_pool.c index ea5fa0bd8..6d8c37223 100644 --- a/src/libutil/mem_pool.c +++ b/src/libutil/mem_pool.c @@ -424,7 +424,7 @@ rspamd_mempool_fstrdup (rspamd_mempool_t * pool, const struct f_str_s *src) } newstr = rspamd_mempool_alloc (pool, src->len + 1); - memcpy (newstr, src->begin, src->len); + memcpy (newstr, src->str, src->len); newstr[src->len] = '\0'; return newstr; } diff --git a/src/libutil/printf.c b/src/libutil/printf.c index 328173bd0..7fe7a9c26 100644 --- a/src/libutil/printf.c +++ b/src/libutil/printf.c @@ -453,7 +453,7 @@ rspamd_vprintf_common (rspamd_printf_append_func func, case 'V': v = va_arg (args, rspamd_fstring_t *); - RSPAMD_PRINTF_APPEND (v->begin, v->len); + RSPAMD_PRINTF_APPEND (v->str, v->len); continue; diff --git a/src/libutil/shingles.c b/src/libutil/shingles.c index 776ecfa96..e3bcc0f10 100644 --- a/src/libutil/shingles.c +++ b/src/libutil/shingles.c @@ -41,7 +41,7 @@ rspamd_shingles_generate (GArray *input, guchar shabuf[BLAKE2B_OUTBYTES], *out_key; const guchar *cur_key; GString *row; - rspamd_fstring_t *word; + rspamd_ftok_t *word; blake2b_state bs; guint64 val; gint i, j, beg = 0; @@ -84,7 +84,7 @@ rspamd_shingles_generate (GArray *input, for (i = 0; i <= (gint)input->len; i ++) { if (i - beg >= SHINGLES_WINDOW || i == (gint)input->len) { for (j = beg; j < i; j ++) { - word = &g_array_index (input, rspamd_fstring_t, j); + word = &g_array_index (input, rspamd_ftok_t, j); g_string_append_len (row, word->begin, word->len); } beg++; diff --git a/src/lua/lua_buffer.c b/src/lua/lua_buffer.c index 7a23a9579..eedd7f05b 100644 --- a/src/lua/lua_buffer.c +++ b/src/lua/lua_buffer.c @@ -91,7 +91,7 @@ lua_io_read_cb (rspamd_fstring_t * in, void *arg) lua_newuserdata (cbdata->L, sizeof (struct rspamd_io_dispatcher_s *)); rspamd_lua_setclass (cbdata->L, "rspamd{io_dispatcher}", -1); *pdispatcher = cbdata->d; - lua_pushlstring (cbdata->L, in->begin, in->len); + lua_pushlstring (cbdata->L, in->str, in->len); if (lua_pcall (cbdata->L, 2, 1, 0) != 0) { msg_info ("call to session finalizer failed: %s", diff --git a/src/lua/lua_rsa.c b/src/lua/lua_rsa.c index 394332fd0..a85d0db71 100644 --- a/src/lua/lua_rsa.c +++ b/src/lua/lua_rsa.c @@ -298,16 +298,13 @@ lua_rsa_signature_load (lua_State *L) sig = g_malloc (sizeof (rspamd_fstring_t)); if (fstat (fd, &st) == -1 || (data = - mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, - 0)) == MAP_FAILED) { + mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) + == MAP_FAILED) { msg_err ("cannot mmap file %s: %s", filename, strerror (errno)); lua_pushnil (L); } else { - sig->size = st.st_size; - sig->len = sig->size; - sig->begin = g_malloc (sig->len); - memcpy (sig->begin, data, sig->len); + sig = rspamd_fstring_new_init (data, st.st_size); psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *)); rspamd_lua_setclass (L, "rspamd{rsa_signature}", -1); *psig = sig; @@ -352,7 +349,7 @@ lua_rsa_signature_save (lua_State *L) lua_pushboolean (L, FALSE); } else { - while (write (fd, sig->begin, sig->len) == -1) { + while (write (fd, sig->str, sig->len) == -1) { if (errno == EINTR) { continue; } @@ -378,14 +375,11 @@ lua_rsa_signature_create (lua_State *L) { rspamd_fstring_t *sig, **psig; const gchar *data; + gsize dlen; - data = luaL_checkstring (L, 1); + data = luaL_checklstring (L, 1, &dlen); if (data != NULL) { - sig = g_malloc (sizeof (rspamd_fstring_t)); - sig->len = strlen (data); - sig->size = sig->len; - sig->begin = g_malloc (sig->len); - memcpy (sig->begin, data, sig->len); + sig = rspamd_fstring_new_init (data, dlen); psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *)); rspamd_lua_setclass (L, "rspamd{rsa_signature}", -1); *psig = sig; @@ -399,12 +393,7 @@ lua_rsa_signature_gc (lua_State *L) { rspamd_fstring_t *sig = lua_check_rsa_sign (L, 1); - if (sig != NULL) { - if (sig->begin != NULL) { - g_free (sig->begin); - } - g_free (sig); - } + rspamd_fstring_free (sig); return 0; } @@ -435,7 +424,7 @@ lua_rsa_verify_memory (lua_State *L) if (rsa != NULL && signature != NULL && data != NULL) { data_sig = g_compute_checksum_for_string (G_CHECKSUM_SHA256, data, -1); ret = RSA_verify (NID_sha1, data_sig, strlen (data_sig), - signature->begin, signature->len, rsa); + signature->str, signature->len, rsa); if (ret == 0) { msg_info ("cannot check rsa signature for data: %s", ERR_error_string (ERR_get_error (), NULL)); @@ -496,7 +485,7 @@ lua_rsa_verify_file (lua_State *L) data, st.st_size); ret = RSA_verify (NID_sha1, data_sig, strlen (data_sig), - signature->begin, signature->len, rsa); + signature->str, signature->len, rsa); if (ret == 0) { msg_info ("cannot check rsa signature for file: %s, %s", filename, ERR_error_string (ERR_get_error (), NULL)); @@ -534,26 +523,22 @@ lua_rsa_sign_memory (lua_State *L) RSA *rsa; rspamd_fstring_t *signature, **psig; const gchar *data; - gchar *data_sig; + guchar *data_sig; gint ret; rsa = lua_check_rsa_privkey (L, 1); data = luaL_checkstring (L, 2); if (rsa != NULL && data != NULL) { - signature = g_malloc (sizeof (rspamd_fstring_t)); - signature->len = RSA_size (rsa); - signature->size = signature->len; - signature->begin = g_malloc (signature->len); + signature = rspamd_fstring_sized_new (RSA_size (rsa)); data_sig = g_compute_checksum_for_string (G_CHECKSUM_SHA256, data, -1); ret = RSA_sign (NID_sha1, data_sig, strlen (data_sig), - signature->begin, (guint *)&signature->len, rsa); + signature->str, (guint *)&signature->len, rsa); if (ret == 0) { msg_info ("cannot make a signature for data: %s", ERR_error_string (ERR_get_error (), NULL)); lua_pushnil (L); - g_free (signature->begin); - g_free (signature); + rspamd_fstring_free (signature); } else { psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *)); @@ -607,21 +592,17 @@ lua_rsa_sign_file (lua_State *L) lua_pushnil (L); } else { - signature = g_malloc (sizeof (rspamd_fstring_t)); - signature->len = RSA_size (rsa); - signature->size = signature->len; - signature->begin = g_malloc (signature->len); + signature = rspamd_fstring_sized_new (RSA_size (rsa)); data_sig = g_compute_checksum_for_string (G_CHECKSUM_SHA256, data, st.st_size); ret = RSA_sign (NID_sha1, data_sig, strlen (data_sig), - signature->begin, (guint *)&signature->len, rsa); + signature->str, (guint *)&signature->len, rsa); if (ret == 0) { msg_info ("cannot make a signature for data: %s", ERR_error_string (ERR_get_error (), NULL)); lua_pushnil (L); - g_free (signature->begin); - g_free (signature); + rspamd_fstring_free (signature); } else { psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *)); diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index d52eeb166..4ef772a5c 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -638,8 +638,8 @@ lua_task_create_from_buffer (lua_State *L) ptask = lua_newuserdata (L, sizeof (gpointer)); rspamd_lua_setclass (L, "rspamd{task}", -1); *ptask = task; - task->msg.start = rspamd_mempool_alloc (task->task_pool, len + 1); - memcpy ((gpointer)task->msg.start, data, len); + task->msg.begin = rspamd_mempool_alloc (task->task_pool, len); + memcpy ((gpointer)task->msg.begin, data, len); task->msg.len = len; } return 1; @@ -883,7 +883,7 @@ lua_task_get_content (lua_State * L) t = lua_newuserdata (L, sizeof (*t)); rspamd_lua_setclass (L, "rspamd{text}", -1); t->len = task->msg.len; - t->start = task->msg.start; + t->start = task->msg.begin; t->own = FALSE; return 1; diff --git a/src/lua/lua_trie.c b/src/lua/lua_trie.c index bceda4502..b0b1551e3 100644 --- a/src/lua/lua_trie.c +++ b/src/lua/lua_trie.c @@ -303,7 +303,7 @@ lua_trie_search_rawmsg (lua_State *L) gboolean found = FALSE; if (trie) { - text = task->msg.start; + text = task->msg.begin; len = task->msg.len; if (lua_trie_search_str (L, trie, text, len, &state) != 0) { diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index f9f71c532..3a7d00211 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -244,8 +244,8 @@ lua_util_process_message (lua_State *L) task = rspamd_task_new (NULL); task->cfg = cfg; task->ev_base = base; - task->msg.start = rspamd_mempool_alloc (task->task_pool, mlen + 1); - rspamd_strlcpy ((gpointer)task->msg.start, message, mlen + 1); + task->msg.begin = rspamd_mempool_alloc (task->task_pool, mlen); + rspamd_strlcpy ((gpointer)task->msg.begin, message, mlen); task->msg.len = mlen; task->fin_callback = lua_util_task_fin; task->fin_arg = &res; @@ -397,7 +397,7 @@ lua_util_tokenize_text (lua_State *L) struct rspamd_lua_text *t; struct process_exception *ex; GArray *res; - rspamd_fstring_t *w; + rspamd_ftok_t *w; gboolean compat = FALSE, check_sig = FALSE; if (lua_type (L, 1) == LUA_TSTRING) { @@ -465,7 +465,7 @@ lua_util_tokenize_text (lua_State *L) lua_newtable (L); for (i = 0; i < res->len; i ++) { - w = &g_array_index (res, rspamd_fstring_t, i); + w = &g_array_index (res, rspamd_ftok_t, i); lua_pushlstring (L, w->begin, w->len); lua_rawseti (L, -2, i + 1); }