aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2020-09-08 01:02:47 +0200
committerPřemysl Eric Janouch <p@janouch.name>2020-09-08 05:01:31 +0200
commit588b6ef8bb855bec17b31493f7e5bdf881bdaff4 (patch)
tree1448bd5d3c29dfcb46fd8dd6f7259250605f7990 /src
parentfda956093c617c293508bbd88a8fd5cc17b42f0c (diff)
downloadtdv-588b6ef8bb855bec17b31493f7e5bdf881bdaff4.tar.gz
tdv-588b6ef8bb855bec17b31493f7e5bdf881bdaff4.tar.xz
tdv-588b6ef8bb855bec17b31493f7e5bdf881bdaff4.zip
Fix a segfault when search goes past the end
Removing a preprocessor macro in favor of a normal function.
Diffstat (limited to 'src')
-rw-r--r--src/stardict.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/stardict.c b/src/stardict.c
index 1125188..2d49aad 100644
--- a/src/stardict.c
+++ b/src/stardict.c
@@ -915,6 +915,15 @@ stardict_dict_cmp_index (StardictDict *sd, const gchar *word, gint i)
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.
/// @param[in] word The word in utf-8 encoding
/// @param[out] success TRUE if found
@@ -936,14 +945,11 @@ stardict_dict_search (StardictDict *sd, const gchar *word, gboolean *success)
BINARY_SEARCH_END
- // 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)
-
+ // Try to find a longer common prefix with a preceding entry.
// We need to take care not to step through the entire dictionary
- // if not a single character matches, because it can be quite costly
- size_t probe, best = PREFIX (imin);
- while (best && imin > 0 && (probe = PREFIX (imin - 1)) >= best)
+ // if not a single character matches, because it can be quite costly.
+ size_t probe, best = prefix (sd, word, imin);
+ while (best && imin > 0 && (probe = prefix (sd, word, imin - 1)) >= best)
{
// TODO: take more care to not screw up exact matches,
// use several "best"s according to quality
@@ -956,8 +962,6 @@ stardict_dict_search (StardictDict *sd, const gchar *word, gboolean *success)
imin--;
}
-#undef PREFIX
-
if (success) *success = FALSE;
return stardict_iterator_new (sd, imin);
}