summaryrefslogtreecommitdiff
blob: 62e3efc1d629e76c68642136b6d294752b2eb558 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
https://bugs.gentoo.org/567608
Fix the extract() function not to access unnecessary array elements;
this avoids an out-of-bounds read when called from btoe() or etob().
Change the insert() function to use similar logic as extract().

--- skey-1.1.5-orig/put.c
+++ skey-1.1.5/put.c
@@ -2221,37 +2221,20 @@
 
 static void insert(char *s, int x, int start, int length)
 {
-	unsigned char cl;
-	unsigned char cc;
-	unsigned char cr;
 	unsigned int y;
-	int shift;
+	int end, i;
 
 	assert(length <= 11);
 	assert(start >= 0);
 	assert(length >= 0);
 	assert(start + length <= 66);
 
-	shift = ((8 - ((start + length) % 8)) % 8);
-	y = (int) x << shift;
-	cl = (y >> 16) & 0xff;
-	cc = (y >> 8) & 0xff;
-	cr = y & 0xff;
-	if (shift + length > 16) 
-	{
-		s[start / 8] |= cl;
-		s[start / 8 + 1] |= cc;
-		s[start / 8 + 2] |= cr;
-	} 
-	else if (shift + length > 8) 
-	{
-		s[start / 8] |= cc;
-		s[start / 8 + 1] |= cr;
-	} 
-	else 
-	{
-		s[start / 8] |= cr;
- 	}
+	end = start + length - 1;
+	y = x << (7 - end % 8);
+	for (i = end / 8; i >= start / 8; i--) {
+		s[i] |= y & 0xff;
+		y >>= 8;
+	}
 }
 
 static void standard(char *word)
@@ -2274,22 +2257,22 @@
 /* Extract 'length' bits from the char array 's' starting with bit 'start' */
 static unsigned int extract(char *s, int start, int length)
 {
-	unsigned char cl;
-	unsigned char cc;
-	unsigned char cr;
 	unsigned int x;
+	int end, i;
 
 	assert(length <= 11);
 	assert(start >= 0);
 	assert(length >= 0);
 	assert(start + length <= 66);
 
-	cl = s[start / 8];
-	cc = s[start / 8 + 1];
-	cr = s[start / 8 + 2];
-	x = ((int)(cl << 8 | cc) << 8 | cr);
-	x = x >> (24 - (length + (start % 8)));
-	x = (x & (0xffff >> (16 - length)));
+	end = start + length - 1;
+	x = 0;
+	for (i = start / 8; i <= end / 8; i++) {
+		x <<= 8;
+		x |= (unsigned char)s[i];
+	}
+	x >>= 7 - end % 8;
+	x &= (1 << length) - 1;
 
 	return x;
 }