Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add more comments nd tests.
  • Loading branch information
serhiy-storchaka committed Dec 27, 2025
commit 5062ae55c286429e68d5b1271bdab5c9472d6476
13 changes: 13 additions & 0 deletions Lib/test/test_base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import base64
import binascii
import string
import sys
import os
from array import array
from test.support import cpython_only
Expand Down Expand Up @@ -178,6 +179,18 @@ def test_b64encode(self):
eq(base64.b64encode(b"www.python.org", wrapcol=76), b'd3d3LnB5dGhvbi5vcmc=')
eq(base64.b64encode(b"www.python.org", wrapcol=1),
b'd\n3\nd\n3\nL\nn\nB\n5\nd\nG\nh\nv\nb\ni\n5\nv\nc\nm\nc\n=')
eq(base64.b64encode(b"www.python.org", wrapcol=sys.maxsize),
b'd3d3LnB5dGhvbi5vcmc=')
eq(base64.b64encode(b"www.python.org", wrapcol=sys.maxsize*2),
b'd3d3LnB5dGhvbi5vcmc=')
with self.assertRaises(OverflowError):
base64.b64encode(b"www.python.org", wrapcol=2**1000)
with self.assertRaises(ValueError):
base64.b64encode(b"www.python.org", wrapcol=-8)
with self.assertRaises(TypeError):
base64.b64encode(b"www.python.org", wrapcol=8.0)
with self.assertRaises(TypeError):
base64.b64encode(b"www.python.org", wrapcol='8')
eq(base64.b64encode(b"", wrapcol=0), b'')
eq(base64.b64encode(b"", wrapcol=8), b'')

Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import binascii
import array
import re
import sys
from test.support import bigmemtest, _1G, _4G
from test.support.hypothesis_helper import hypothesis

Expand Down Expand Up @@ -498,6 +499,18 @@ def test_b2a_base64_wrapcol(self):
b'd3d3LnB5\ndGhvbi5v\ncmc=')
self.assertEqual(binascii.b2a_base64(b, wrapcol=1),
b'd\n3\nd\n3\nL\nn\nB\n5\nd\nG\nh\nv\nb\ni\n5\nv\nc\nm\nc\n=\n')
self.assertEqual(binascii.b2a_base64(b, wrapcol=sys.maxsize),
b'd3d3LnB5dGhvbi5vcmc=\n')
self.assertEqual(binascii.b2a_base64(b, wrapcol=sys.maxsize*2),
b'd3d3LnB5dGhvbi5vcmc=\n')
with self.assertRaises(OverflowError):
binascii.b2a_base64(b, wrapcol=2**1000)
with self.assertRaises(ValueError):
binascii.b2a_base64(b, wrapcol=-8)
with self.assertRaises(TypeError):
binascii.b2a_base64(b, wrapcol=8.0)
with self.assertRaises(TypeError):
binascii.b2a_base64(b, wrapcol='8')
b = self.type2test(b'')
self.assertEqual(binascii.b2a_base64(b), b'\n')
self.assertEqual(binascii.b2a_base64(b, wrapcol=0), b'\n')
Expand Down
12 changes: 11 additions & 1 deletion Modules/binascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ ascii_buffer_converter(PyObject *arg, Py_buffer *buf)
return Py_CLEANUP_SUPPORTED;
}

/* The function inserts '\n' each width characters, moving the data right.
* It assumes that we allocated enough space for all of the newlines in data.
* Returns the size of the data including the newlines.
*/
static Py_ssize_t
wraplines(unsigned char *data, Py_ssize_t size, size_t width)
Comment thread
gpshead marked this conversation as resolved.
Comment thread
gpshead marked this conversation as resolved.
{
Expand Down Expand Up @@ -564,12 +568,18 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, size_t wrapcol,

assert(bin_len >= 0);

/* Each group of 3 bytes (rounded up) gets encoded as 4 characters,
* not counting newlines.
* Note that 'b' gets encoded as 'Yg==' (1 in, 4 out).
*
* Use unsigned integer arithmetic to avoid signed integer overflow.
*/
size_t out_len = ((size_t)bin_len + 2u) / 3u * 4u;
Comment thread
gpshead marked this conversation as resolved.
if (out_len > PY_SSIZE_T_MAX) {
goto toolong;
}
if (wrapcol && out_len) {
out_len += (out_len - 1) / wrapcol;
out_len += (out_len - 1u) / wrapcol;
if (out_len > PY_SSIZE_T_MAX) {
goto toolong;
}
Expand Down
Loading