Skip to content

Commit 5062ae5

Browse files
Add more comments nd tests.
1 parent 46c6a25 commit 5062ae5

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

Lib/test/test_base64.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import base64
33
import binascii
44
import string
5+
import sys
56
import os
67
from array import array
78
from test.support import cpython_only
@@ -178,6 +179,18 @@ def test_b64encode(self):
178179
eq(base64.b64encode(b"www.python.org", wrapcol=76), b'd3d3LnB5dGhvbi5vcmc=')
179180
eq(base64.b64encode(b"www.python.org", wrapcol=1),
180181
b'd\n3\nd\n3\nL\nn\nB\n5\nd\nG\nh\nv\nb\ni\n5\nv\nc\nm\nc\n=')
182+
eq(base64.b64encode(b"www.python.org", wrapcol=sys.maxsize),
183+
b'd3d3LnB5dGhvbi5vcmc=')
184+
eq(base64.b64encode(b"www.python.org", wrapcol=sys.maxsize*2),
185+
b'd3d3LnB5dGhvbi5vcmc=')
186+
with self.assertRaises(OverflowError):
187+
base64.b64encode(b"www.python.org", wrapcol=2**1000)
188+
with self.assertRaises(ValueError):
189+
base64.b64encode(b"www.python.org", wrapcol=-8)
190+
with self.assertRaises(TypeError):
191+
base64.b64encode(b"www.python.org", wrapcol=8.0)
192+
with self.assertRaises(TypeError):
193+
base64.b64encode(b"www.python.org", wrapcol='8')
181194
eq(base64.b64encode(b"", wrapcol=0), b'')
182195
eq(base64.b64encode(b"", wrapcol=8), b'')
183196

Lib/test/test_binascii.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import binascii
55
import array
66
import re
7+
import sys
78
from test.support import bigmemtest, _1G, _4G
89
from test.support.hypothesis_helper import hypothesis
910

@@ -498,6 +499,18 @@ def test_b2a_base64_wrapcol(self):
498499
b'd3d3LnB5\ndGhvbi5v\ncmc=')
499500
self.assertEqual(binascii.b2a_base64(b, wrapcol=1),
500501
b'd\n3\nd\n3\nL\nn\nB\n5\nd\nG\nh\nv\nb\ni\n5\nv\nc\nm\nc\n=\n')
502+
self.assertEqual(binascii.b2a_base64(b, wrapcol=sys.maxsize),
503+
b'd3d3LnB5dGhvbi5vcmc=\n')
504+
self.assertEqual(binascii.b2a_base64(b, wrapcol=sys.maxsize*2),
505+
b'd3d3LnB5dGhvbi5vcmc=\n')
506+
with self.assertRaises(OverflowError):
507+
binascii.b2a_base64(b, wrapcol=2**1000)
508+
with self.assertRaises(ValueError):
509+
binascii.b2a_base64(b, wrapcol=-8)
510+
with self.assertRaises(TypeError):
511+
binascii.b2a_base64(b, wrapcol=8.0)
512+
with self.assertRaises(TypeError):
513+
binascii.b2a_base64(b, wrapcol='8')
501514
b = self.type2test(b'')
502515
self.assertEqual(binascii.b2a_base64(b), b'\n')
503516
self.assertEqual(binascii.b2a_base64(b, wrapcol=0), b'\n')

Modules/binascii.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ ascii_buffer_converter(PyObject *arg, Py_buffer *buf)
189189
return Py_CLEANUP_SUPPORTED;
190190
}
191191

192+
/* The function inserts '\n' each width characters, moving the data right.
193+
* It assumes that we allocated enough space for all of the newlines in data.
194+
* Returns the size of the data including the newlines.
195+
*/
192196
static Py_ssize_t
193197
wraplines(unsigned char *data, Py_ssize_t size, size_t width)
194198
{
@@ -564,12 +568,18 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, size_t wrapcol,
564568

565569
assert(bin_len >= 0);
566570

571+
/* Each group of 3 bytes (rounded up) gets encoded as 4 characters,
572+
* not counting newlines.
573+
* Note that 'b' gets encoded as 'Yg==' (1 in, 4 out).
574+
*
575+
* Use unsigned integer arithmetic to avoid signed integer overflow.
576+
*/
567577
size_t out_len = ((size_t)bin_len + 2u) / 3u * 4u;
568578
if (out_len > PY_SSIZE_T_MAX) {
569579
goto toolong;
570580
}
571581
if (wrapcol && out_len) {
572-
out_len += (out_len - 1) / wrapcol;
582+
out_len += (out_len - 1u) / wrapcol;
573583
if (out_len > PY_SSIZE_T_MAX) {
574584
goto toolong;
575585
}

0 commit comments

Comments
 (0)