$OpenBSD: patch-folks_potential-match_vala,v 1.4 2018/01/06 18:17:46 ajacoutot Exp $

From 765febf7a02adf62b01f86eb4e7dbfd548b4e99d Mon Sep 17 00:00:00 2001
From: Niels De Graef <nielsdegraef@gmail.com>
Date: Wed, 3 Jan 2018 23:19:31 +0100
Subject: PotentialMatch: use a correct index upper bound.

From 7a71777f7ef9b7780ad24c225da47c01b7bdb9e6 Mon Sep 17 00:00:00 2001
From: Niels De Graef <nielsdegraef@gmail.com>
Date: Thu, 4 Jan 2018 13:06:10 +0100
Subject: PotentialMatch: prevent out-of-bounds indexing.

From 9c4e87e5273f241e45d8bb93369b10903dffeb58 Mon Sep 17 00:00:00 2001
From: Niels De Graef <nielsdegraef@gmail.com>
Date: Fri, 5 Jan 2018 12:22:33 +0100
Subject: PotentialMatch: fix end-of-string check.

From eaaa6fac6ec7d143148cbe9b8f78fe569d4fef4e Mon Sep 17 00:00:00 2001
From: Niels De Graef <nielsdegraef@gmail.com>
Date: Sat, 6 Jan 2018 13:11:03 +0100
Subject: PotentialMatch: fix end-of-string check, part 2.

Index: folks/potential-match.vala
--- folks/potential-match.vala.orig
+++ folks/potential-match.vala
@@ -633,10 +633,11 @@ public class Folks.PotentialMatch : Object
     {
       int matches = 0;
       t = 0.0;
+      var len_s1 = s1.length;
 
       unichar look_for = 0;
 
-      for (uint idx = 0; (look_for = s1[idx]) != 0; idx++)
+      for (uint idx = 0; idx < len_s1 && (look_for = s1[idx]) != 0; idx++)
         {
           int contains = this._contains (s2, look_for, idx, max_dist);
           if (contains >= 0)
@@ -665,14 +666,14 @@ public class Folks.PotentialMatch : Object
     {
       var haystack_len = haystack.length; /* in unichars */
 
-      unichar ch = haystack[pos];
-      if (pos < haystack_len && ch == c)
+      if (pos < haystack_len && haystack[pos] == c)
         return 0;
 
-      uint idx = ((int) pos - (int) max_dist).clamp (0, haystack_len);
-      ch = 0;
+      uint idx = ((int) pos - (int) max_dist).clamp (0, haystack_len - 1);
+      unichar ch = 0;
 
-      while (idx < pos + max_dist && (ch = haystack[idx]) != 0)
+      while (idx < pos + max_dist && idx < haystack_len &&
+          (ch = haystack[idx]) != 0)
         {
           if (ch == c)
             return ((int) pos - (int) idx).abs ();
