Skip to content

Commit f420755

Browse files
authored
Merge pull request #5951 from FrancisTheCat/master
Increase base64 decoding table size to 256, preventing out of bounds reads
2 parents 17f1669 + cd4bec0 commit f420755

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

core/encoding/base64/base64.odin

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,39 @@ ENC_TABLE := [64]byte {
2626

2727
PADDING :: '='
2828

29-
DEC_TABLE := [128]int {
30-
-1, -1, -1, -1, -1, -1, -1, -1,
31-
-1, -1, -1, -1, -1, -1, -1, -1,
32-
-1, -1, -1, -1, -1, -1, -1, -1,
33-
-1, -1, -1, -1, -1, -1, -1, -1,
34-
-1, -1, -1, -1, -1, -1, -1, -1,
35-
-1, -1, -1, 62, -1, -1, -1, 63,
29+
DEC_TABLE := [256]u8 {
30+
0, 0, 0, 0, 0, 0, 0, 0,
31+
0, 0, 0, 0, 0, 0, 0, 0,
32+
0, 0, 0, 0, 0, 0, 0, 0,
33+
0, 0, 0, 0, 0, 0, 0, 0,
34+
0, 0, 0, 0, 0, 0, 0, 0,
35+
0, 0, 0, 62, 0, 0, 0, 63,
3636
52, 53, 54, 55, 56, 57, 58, 59,
37-
60, 61, -1, -1, -1, -1, -1, -1,
38-
-1, 0, 1, 2, 3, 4, 5, 6,
37+
60, 61, 0, 0, 0, 0, 0, 0,
38+
0, 0, 1, 2, 3, 4, 5, 6,
3939
7, 8, 9, 10, 11, 12, 13, 14,
4040
15, 16, 17, 18, 19, 20, 21, 22,
41-
23, 24, 25, -1, -1, -1, -1, -1,
42-
-1, 26, 27, 28, 29, 30, 31, 32,
41+
23, 24, 25, 0, 0, 0, 0, 0,
42+
0, 26, 27, 28, 29, 30, 31, 32,
4343
33, 34, 35, 36, 37, 38, 39, 40,
4444
41, 42, 43, 44, 45, 46, 47, 48,
45-
49, 50, 51, -1, -1, -1, -1, -1,
45+
49, 50, 51, 0, 0, 0, 0, 0,
46+
0, 0, 0, 0, 0, 0, 0, 0,
47+
0, 0, 0, 0, 0, 0, 0, 0,
48+
0, 0, 0, 0, 0, 0, 0, 0,
49+
0, 0, 0, 0, 0, 0, 0, 0,
50+
0, 0, 0, 0, 0, 0, 0, 0,
51+
0, 0, 0, 0, 0, 0, 0, 0,
52+
0, 0, 0, 0, 0, 0, 0, 0,
53+
0, 0, 0, 0, 0, 0, 0, 0,
54+
0, 0, 0, 0, 0, 0, 0, 0,
55+
0, 0, 0, 0, 0, 0, 0, 0,
56+
0, 0, 0, 0, 0, 0, 0, 0,
57+
0, 0, 0, 0, 0, 0, 0, 0,
58+
0, 0, 0, 0, 0, 0, 0, 0,
59+
0, 0, 0, 0, 0, 0, 0, 0,
60+
0, 0, 0, 0, 0, 0, 0, 0,
61+
0, 0, 0, 0, 0, 0, 0, 0,
4662
}
4763

4864
encode :: proc(data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocator) -> (encoded: string, err: mem.Allocator_Error) #optional_allocator_error {
@@ -120,10 +136,10 @@ decode_into :: proc(w: io.Writer, data: string, DEC_TBL := DEC_TABLE) -> io.Erro
120136
i, j: int
121137
for ; j + 3 <= length; i, j = i + 4, j + 3 {
122138
#no_bounds_check {
123-
c0 = DEC_TBL[data[i]]
124-
c1 = DEC_TBL[data[i + 1]]
125-
c2 = DEC_TBL[data[i + 2]]
126-
c3 = DEC_TBL[data[i + 3]]
139+
c0 = int(DEC_TBL[data[i]])
140+
c1 = int(DEC_TBL[data[i + 1]])
141+
c2 = int(DEC_TBL[data[i + 2]])
142+
c3 = int(DEC_TBL[data[i + 3]])
127143

128144
b0 = (c0 << 2) | (c1 >> 4)
129145
b1 = (c1 << 4) | (c2 >> 2)
@@ -140,9 +156,9 @@ decode_into :: proc(w: io.Writer, data: string, DEC_TBL := DEC_TABLE) -> io.Erro
140156
rest := length - j
141157
if rest > 0 {
142158
#no_bounds_check {
143-
c0 = DEC_TBL[data[i]]
144-
c1 = DEC_TBL[data[i + 1]]
145-
c2 = DEC_TBL[data[i + 2]]
159+
c0 = int(DEC_TBL[data[i]])
160+
c1 = int(DEC_TBL[data[i + 1]])
161+
c2 = int(DEC_TBL[data[i + 2]])
146162

147163
b0 = (c0 << 2) | (c1 >> 4)
148164
b1 = (c1 << 4) | (c2 >> 2)

0 commit comments

Comments
 (0)