Fix a segfault when search goes past the end

Removing a preprocessor macro in favor of a normal function.
This commit is contained in:
Přemysl Eric Janouch 2020-09-08 01:02:47 +02:00
parent fda956093c
commit 588b6ef8bb
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 13 additions and 9 deletions

View File

@ -915,6 +915,15 @@ stardict_dict_cmp_index (StardictDict *sd, const gchar *word, gint i)
return g_ascii_strcasecmp (word, target); return g_ascii_strcasecmp (word, target);
} }
static size_t
prefix (StardictDict *sd, const gchar *word, gint i)
{
GArray *index = sd->priv->index;
return (guint) i >= index->len ? 0 :
stardict_longest_common_collation_prefix
(sd, word, g_array_index (index, StardictIndexEntry, i).name);
}
/// Search for a word. The search is ASCII-case-insensitive. /// Search for a word. The search is ASCII-case-insensitive.
/// @param[in] word The word in utf-8 encoding /// @param[in] word The word in utf-8 encoding
/// @param[out] success TRUE if found /// @param[out] success TRUE if found
@ -936,14 +945,11 @@ stardict_dict_search (StardictDict *sd, const gchar *word, gboolean *success)
BINARY_SEARCH_END BINARY_SEARCH_END
// Try to find a longer common prefix with a preceding entry // Try to find a longer common prefix with a preceding entry.
#define PREFIX(i) stardict_longest_common_collation_prefix \
(sd, word, g_array_index (index, StardictIndexEntry, i).name)
// We need to take care not to step through the entire dictionary // We need to take care not to step through the entire dictionary
// if not a single character matches, because it can be quite costly // if not a single character matches, because it can be quite costly.
size_t probe, best = PREFIX (imin); size_t probe, best = prefix (sd, word, imin);
while (best && imin > 0 && (probe = PREFIX (imin - 1)) >= best) while (best && imin > 0 && (probe = prefix (sd, word, imin - 1)) >= best)
{ {
// TODO: take more care to not screw up exact matches, // TODO: take more care to not screw up exact matches,
// use several "best"s according to quality // use several "best"s according to quality
@ -956,8 +962,6 @@ stardict_dict_search (StardictDict *sd, const gchar *word, gboolean *success)
imin--; imin--;
} }
#undef PREFIX
if (success) *success = FALSE; if (success) *success = FALSE;
return stardict_iterator_new (sd, imin); return stardict_iterator_new (sd, imin);
} }