summaryrefslogtreecommitdiff
path: root/debian/patches/fix-console0002-console-client-Fix-lower-range-of-binary-search.patch
blob: e2ad448f5d10a18240017da2d7ab514b2bde3dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
From e974c4ce05f7dcb5e082084fc69f4b2ff2bc891d Mon Sep 17 00:00:00 2001
From: Diego Nieto Cid <dnietoc@gmail.com>
Date: Thu, 4 Jun 2015 22:58:10 -0300
Subject: [PATCH hurd 2/2] console-client: Fix lower range of binary search

To prevent infinite recursion range checking was introduced
as an exit condition adding two extra comparisons on each
recursive call.

By fixing the range used by the recursive call over the lower
half of the array one can avoid penalizing successful lookups
while still preventing infinite recursion due to `first`
parameter being greater than `last` parameter.

* console-client/xkb/kstoucs.c (find_ucs): don't remove middle from the
lower range. Remove extra comparisons.
---
 console-client/xkb/kstoucs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/console-client/xkb/kstoucs.c b/console-client/xkb/kstoucs.c
index fb62445..eb47bde 100644
--- a/console-client/xkb/kstoucs.c
+++ b/console-client/xkb/kstoucs.c
@@ -17,15 +17,15 @@ find_ucs (int keysym, struct ksmap *first, struct ksmap *last)
 
   if (middle->keysym == keysym)
     return middle->ucs; /* base case: needle found. */
-  else if (first == last		/* empty search space */
-           || keysym < first->keysym	/* lookup failure */
-           || keysym > last->keysym)	/* lookup failure */
+  else if (first == last) /* empty search space */
     return 0;
   /* recursive cases: halve search space. */
   else if (middle->keysym < keysym)
     return find_ucs (keysym, middle+1, last);
   else if (middle->keysym > keysym)
-    return find_ucs (keysym, first, middle-1);
+    /* don't remove middle from the range to compensate
+       for rounding down in it's calculation */
+    return find_ucs (keysym, first, middle);
   return 0;
 }
 
-- 
2.1.4