Skip to content

Commit 102e8dd

Browse files
Fix cache index calculation error.
1 parent 959d5b3 commit 102e8dd

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

Lib/test/test_binascii.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ def assertNonBase64Data(data, expected, ignorechars):
202202
assertNonBase64Data(b'a\nb==', b'i', ignorechars=bytearray(b'\n'))
203203
assertNonBase64Data(b'a\nb==', b'i', ignorechars=memoryview(b'\n'))
204204

205+
# Same byte in the bit cache: '\r' >> 3 == '\n' >> 3.
206+
data = self.type2test(b'\r\n')
207+
with self.assertRaises(binascii.Error):
208+
binascii.a2b_base64(data, ignorechars=b'\r')
209+
self.assertEqual(binascii.a2b_base64(data, ignorechars=b'\r\n'), b'')
210+
# Same mask in the bit cache: ':' & 7 == '\n' & 7.
211+
data = self.type2test(b':\n')
212+
with self.assertRaises(binascii.Error):
213+
binascii.a2b_base64(data, ignorechars=b':')
214+
self.assertEqual(binascii.a2b_base64(data, ignorechars=b':\n'), b'')
215+
205216
data = self.type2test(b'a\nb==')
206217
with self.assertRaises(TypeError):
207218
binascii.a2b_base64(data, ignorechars='')

Modules/binascii.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,11 @@ ignorechar(unsigned char c, Py_buffer *ignorechars, char ignorecache[32])
476476
if (ignorechars->buf == NULL) {
477477
return 0;
478478
}
479-
if (ignorecache[c >> 8] & (1 << (c & 7))) {
479+
if (ignorecache[c >> 3] & (1 << (c & 7))) {
480480
return 1;
481481
}
482482
if (memchr(ignorechars->buf, c, ignorechars->len)) {
483-
ignorecache[c >> 8] |= 1 << (c & 7);
483+
ignorecache[c >> 3] |= 1 << (c & 7);
484484
return 1;
485485
}
486486
return 0;

0 commit comments

Comments
 (0)