Ticket #3637: fix memory leaks in search engine.

(mc_search_set_error): new search API to set the code and message
of search error.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2016-04-27 10:26:38 +03:00
parent 9cd1e165c5
commit 1c22d44970
5 changed files with 55 additions and 30 deletions

View File

@ -31,7 +31,7 @@ typedef mc_search_cbret_t (*mc_update_fn) (const void *user_data, gsize char_off
typedef enum typedef enum
{ {
MC_SEARCH_E_OK, MC_SEARCH_E_OK = 0,
MC_SEARCH_E_INPUT, MC_SEARCH_E_INPUT,
MC_SEARCH_E_REGEX_COMPILE, MC_SEARCH_E_REGEX_COMPILE,
MC_SEARCH_E_REGEX, MC_SEARCH_E_REGEX,
@ -165,4 +165,9 @@ gboolean mc_search (const gchar * pattern, const gchar * pattern_charset, const
int mc_search_getstart_result_by_num (mc_search_t *, int); int mc_search_getstart_result_by_num (mc_search_t *, int);
int mc_search_getend_result_by_num (mc_search_t *, int); int mc_search_getend_result_by_num (mc_search_t *, int);
/* *INDENT-OFF* */
void mc_search_set_error (mc_search_t * mc_search, mc_search_error_t code, const gchar * format, ...)
G_GNUC_PRINTF (3, 4);
/* *INDENT-ON* */
#endif #endif

View File

@ -322,6 +322,7 @@ mc_search__regex_found_cond_one (mc_search_t * lc_mc_search, mc_search_regex_t *
if (mcerror != NULL) if (mcerror != NULL)
{ {
lc_mc_search->error = MC_SEARCH_E_REGEX; lc_mc_search->error = MC_SEARCH_E_REGEX;
g_free (lc_mc_search->error_str);
lc_mc_search->error_str = lc_mc_search->error_str =
str_conv_gerror_message (mcerror, _("Regular expression error")); str_conv_gerror_message (mcerror, _("Regular expression error"));
g_error_free (mcerror); g_error_free (mcerror);
@ -816,6 +817,7 @@ mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_
if (mcerror != NULL) if (mcerror != NULL)
{ {
lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE; lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
g_free (lc_mc_search->error_str);
lc_mc_search->error_str = lc_mc_search->error_str =
str_conv_gerror_message (mcerror, _("Regular expression error")); str_conv_gerror_message (mcerror, _("Regular expression error"));
g_error_free (mcerror); g_error_free (mcerror);
@ -848,21 +850,15 @@ mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_
pcre_compile (mc_search_cond->str->str, pcre_options, &error, &erroffset, NULL); pcre_compile (mc_search_cond->str->str, pcre_options, &error, &erroffset, NULL);
if (mc_search_cond->regex_handle == NULL) if (mc_search_cond->regex_handle == NULL)
{ {
lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE; mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_COMPILE, "%s", error);
lc_mc_search->error_str = g_strdup (error);
return; return;
} }
lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error); lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
if (lc_mc_search->regex_match_info == NULL) if (lc_mc_search->regex_match_info == NULL && error != NULL)
{ {
if (error != NULL) mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_COMPILE, "%s", error);
{ MC_PTR_FREE (mc_search_cond->regex_handle);
lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE; return;
lc_mc_search->error_str = g_strdup (error);
g_free (mc_search_cond->regex_handle);
mc_search_cond->regex_handle = NULL;
return;
}
} }
#endif /* SEARCH_TYPE_GLIB */ #endif /* SEARCH_TYPE_GLIB */
} }
@ -975,12 +971,11 @@ mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
g_string_free (lc_mc_search->regex_buffer, TRUE); g_string_free (lc_mc_search->regex_buffer, TRUE);
lc_mc_search->regex_buffer = NULL; lc_mc_search->regex_buffer = NULL;
lc_mc_search->error = MC_SEARCH_E_NOTFOUND;
if (ret != MC_SEARCH_CB_ABORT) if (ret == MC_SEARCH_CB_ABORT)
lc_mc_search->error_str = g_strdup (_(STR_E_NOTFOUND)); mc_search_set_error (lc_mc_search, MC_SEARCH_E_NOTFOUND, NULL);
else else
lc_mc_search->error_str = NULL; mc_search_set_error (lc_mc_search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
return FALSE; return FALSE;
} }
@ -1006,8 +1001,8 @@ mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * repla
if (num_replace_tokens > lc_mc_search->num_results - 1 if (num_replace_tokens > lc_mc_search->num_results - 1
|| num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS) || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS)
{ {
lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE; mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_REPLACE, "%s",
lc_mc_search->error_str = g_strdup (_(STR_E_RPL_NOT_EQ_TO_FOUND)); _(STR_E_RPL_NOT_EQ_TO_FOUND));
return NULL; return NULL;
} }
@ -1063,8 +1058,8 @@ mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * repla
if (lc_index > lc_mc_search->num_results) if (lc_index > lc_mc_search->num_results)
{ {
g_string_free (ret, TRUE); g_string_free (ret, TRUE);
lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE; mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_REPLACE,
lc_mc_search->error_str = g_strdup_printf (_(STR_E_RPL_INVALID_TOKEN), lc_index); _(STR_E_RPL_INVALID_TOKEN), lc_index);
return NULL; return NULL;
} }

View File

@ -27,6 +27,7 @@
#include <config.h> #include <config.h>
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
@ -272,8 +273,7 @@ mc_search_run (mc_search_t * lc_mc_search, const void *user_data,
return FALSE; return FALSE;
if (!mc_search_is_type_avail (lc_mc_search->search_type)) if (!mc_search_is_type_avail (lc_mc_search->search_type))
{ {
lc_mc_search->error = MC_SEARCH_E_INPUT; mc_search_set_error (lc_mc_search, MC_SEARCH_E_INPUT, "%s", _(STR_E_UNKNOWN_TYPE));
lc_mc_search->error_str = g_strdup (_(STR_E_UNKNOWN_TYPE));
return FALSE; return FALSE;
} }
#ifdef SEARCH_TYPE_GLIB #ifdef SEARCH_TYPE_GLIB
@ -284,8 +284,7 @@ mc_search_run (mc_search_t * lc_mc_search, const void *user_data,
} }
#endif /* SEARCH_TYPE_GLIB */ #endif /* SEARCH_TYPE_GLIB */
lc_mc_search->error = MC_SEARCH_E_OK; mc_search_set_error (lc_mc_search, MC_SEARCH_E_OK, NULL);
MC_PTR_FREE (lc_mc_search->error_str);
if ((lc_mc_search->conditions == NULL) && !mc_search_prepare (lc_mc_search)) if ((lc_mc_search->conditions == NULL) && !mc_search_prepare (lc_mc_search))
return FALSE; return FALSE;
@ -486,3 +485,29 @@ mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index)
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/**
* Replace an old error code and message of an mc_search_t object.
*
* @param mc_search mc_search_t object
* @param code error code, one of mc_search_error_t values
* @param format format of error message. If NULL, the old error string is free'd and become NULL
*/
void
mc_search_set_error (mc_search_t * mc_search, mc_search_error_t code, const gchar * format, ...)
{
mc_search->error = code;
MC_PTR_FREE (mc_search->error_str);
if (format != NULL)
{
va_list args;
va_start (args, format);
mc_search->error_str = g_strdup_vprintf (format, args);
va_end (args);
}
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -829,8 +829,7 @@ editcmd_find (edit_search_status_msg_t * esm, gsize * len)
{ {
if (!eval_marks (edit, &start_mark, &end_mark)) if (!eval_marks (edit, &start_mark, &end_mark))
{ {
edit->search->error = MC_SEARCH_E_NOTFOUND; mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
edit->search->error_str = g_strdup (_(STR_E_NOTFOUND));
return FALSE; return FALSE;
} }
@ -850,8 +849,7 @@ editcmd_find (edit_search_status_msg_t * esm, gsize * len)
edit_calculate_end_of_previous_line (&edit->buffer, end_mark, end_string_symbol); edit_calculate_end_of_previous_line (&edit->buffer, end_mark, end_string_symbol);
if (start_mark >= end_mark) if (start_mark >= end_mark)
{ {
edit->search->error = MC_SEARCH_E_NOTFOUND; mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
edit->search->error_str = g_strdup (_(STR_E_NOTFOUND));
return FALSE; return FALSE;
} }
} }
@ -893,7 +891,8 @@ editcmd_find (edit_search_status_msg_t * esm, gsize * len)
else else
search_start--; search_start--;
} }
edit->search->error_str = g_strdup (_(STR_E_NOTFOUND));
mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
} }
else else
{ {

View File

@ -146,7 +146,8 @@ mcview_find (mcview_search_status_msg_t * ssm, off_t search_start, off_t search_
search_start--; search_start--;
} }
view->search->error_str = g_strdup (_(STR_E_NOTFOUND));
mc_search_set_error (view->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
return FALSE; return FALSE;
} }
view->search_nroff_seq->index = search_start; view->search_nroff_seq->index = search_start;