Skip to content

Commit 91060f2

Browse files
committed
merge 3.4 (closes #27760)
2 parents f17a8e9 + 5295532 commit 91060f2

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Library
4040
- In the curses module, raise an error if window.getstr() is passed a negative
4141
value.
4242

43+
- Issue #27760: Fix possible integer overflow in binascii.b2a_qp.
44+
4345
- Issue #27758: Fix possible integer overflow in the _csv module for large record
4446
lengths.
4547

Modules/binascii.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,7 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
13831383
/* First, scan to see how many characters need to be encoded */
13841384
in = 0;
13851385
while (in < datalen) {
1386+
Py_ssize_t delta = 0;
13861387
if ((databuf[in] > 126) ||
13871388
(databuf[in] == '=') ||
13881389
(header && databuf[in] == '_') ||
@@ -1397,12 +1398,12 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
13971398
if ((linelen + 3) >= MAXLINESIZE) {
13981399
linelen = 0;
13991400
if (crlf)
1400-
odatalen += 3;
1401+
delta += 3;
14011402
else
1402-
odatalen += 2;
1403+
delta += 2;
14031404
}
14041405
linelen += 3;
1405-
odatalen += 3;
1406+
delta += 3;
14061407
in++;
14071408
}
14081409
else {
@@ -1414,11 +1415,11 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
14141415
linelen = 0;
14151416
/* Protect against whitespace on end of line */
14161417
if (in && ((databuf[in-1] == ' ') || (databuf[in-1] == '\t')))
1417-
odatalen += 2;
1418+
delta += 2;
14181419
if (crlf)
1419-
odatalen += 2;
1420+
delta += 2;
14201421
else
1421-
odatalen += 1;
1422+
delta += 1;
14221423
if (databuf[in] == '\r')
14231424
in += 2;
14241425
else
@@ -1430,15 +1431,20 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
14301431
(linelen + 1) >= MAXLINESIZE) {
14311432
linelen = 0;
14321433
if (crlf)
1433-
odatalen += 3;
1434+
delta += 3;
14341435
else
1435-
odatalen += 2;
1436+
delta += 2;
14361437
}
14371438
linelen++;
1438-
odatalen++;
1439+
delta++;
14391440
in++;
14401441
}
14411442
}
1443+
if (PY_SSIZE_T_MAX - delta < odatalen) {
1444+
PyErr_NoMemory();
1445+
return NULL;
1446+
}
1447+
odatalen += delta;
14421448
}
14431449

14441450
/* We allocate the output same size as input, this is overkill.

0 commit comments

Comments
 (0)