Make sure we don't access out of bounds of the match string.
authorColin Leroy <colin@colino.net>
Tue, 17 Jun 2014 18:21:49 +0000 (20:21 +0200)
committerColin Leroy <colin@colino.net>
Tue, 17 Jun 2014 18:21:49 +0000 (20:21 +0200)
src/addr_compl.c
src/common/utils.h

index b35a8bd..21348ab 100644 (file)
@@ -191,7 +191,7 @@ static gint weight_addr_match(const address_entry* addr)
        if (match != NULL) {
                if (match == addr->name)
                        n_weight = -4;
-               else if (*(match - 1) == ' ')
+               else if (match > addr->name && *(match - 1) == ' ')
                        n_weight = -3;
                else
                        n_weight = match - addr->name;
@@ -205,7 +205,8 @@ static gint weight_addr_match(const address_entry* addr)
                        else
                                a_weight = match - addr->address;
 
-                       if (*(match + strlen(g_completion_prefix)) == '@')
+                       if (strlen(match) < strlen(g_completion_prefix)
+                        && *(match + strlen(g_completion_prefix)) == '@')
                                a_weight--;
                }
        }
@@ -213,7 +214,7 @@ static gint weight_addr_match(const address_entry* addr)
        if (n_weight == -4 && a_weight < 0)
                n_weight = -5;
 
-       return a_weight < n_weight ? a_weight : n_weight;
+       return MIN(a_weight, n_weight);
 }
 
 static gint addr_comparison_func(gconstpointer a, gconstpointer b)
index d556b0e..2df4034 100644 (file)
@@ -222,6 +222,13 @@ G_STMT_END
        }                                                               \
 } G_STMT_END
 
+#ifndef MIN
+       #define MIN(a, b) ((a) < (b) ? (a) : (b))
+#endif
+#ifndef MAX
+       #define MAX(a, b) ((a) > (b) ? (a) : (b))
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif