Skip to content

Commit b6f78c2

Browse files
committed
merge 3.5 (closes #27760)
2 parents c0654d4 + 91060f2 commit b6f78c2

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
@@ -61,6 +61,8 @@ Library
6161
- In the curses module, raise an error if window.getstr() is passed a negative
6262
value.
6363

64+
- Issue #27760: Fix possible integer overflow in binascii.b2a_qp.
65+
6466
- Issue #27758: Fix possible integer overflow in the _csv module for large record
6567
lengths.
6668

Modules/binascii.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,7 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
13701370
/* First, scan to see how many characters need to be encoded */
13711371
in = 0;
13721372
while (in < datalen) {
1373+
Py_ssize_t delta = 0;
13731374
if ((databuf[in] > 126) ||
13741375
(databuf[in] == '=') ||
13751376
(header && databuf[in] == '_') ||
@@ -1384,12 +1385,12 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
13841385
if ((linelen + 3) >= MAXLINESIZE) {
13851386
linelen = 0;
13861387
if (crlf)
1387-
odatalen += 3;
1388+
delta += 3;
13881389
else
1389-
odatalen += 2;
1390+
delta += 2;
13901391
}
13911392
linelen += 3;
1392-
odatalen += 3;
1393+
delta += 3;
13931394
in++;
13941395
}
13951396
else {
@@ -1401,11 +1402,11 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
14011402
linelen = 0;
14021403
/* Protect against whitespace on end of line */
14031404
if (in && ((databuf[in-1] == ' ') || (databuf[in-1] == '\t')))
1404-
odatalen += 2;
1405+
delta += 2;
14051406
if (crlf)
1406-
odatalen += 2;
1407+
delta += 2;
14071408
else
1408-
odatalen += 1;
1409+
delta += 1;
14091410
if (databuf[in] == '\r')
14101411
in += 2;
14111412
else
@@ -1417,15 +1418,20 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
14171418
(linelen + 1) >= MAXLINESIZE) {
14181419
linelen = 0;
14191420
if (crlf)
1420-
odatalen += 3;
1421+
delta += 3;
14211422
else
1422-
odatalen += 2;
1423+
delta += 2;
14231424
}
14241425
linelen++;
1425-
odatalen++;
1426+
delta++;
14261427
in++;
14271428
}
14281429
}
1430+
if (PY_SSIZE_T_MAX - delta < odatalen) {
1431+
PyErr_NoMemory();
1432+
return NULL;
1433+
}
1434+
odatalen += delta;
14291435
}
14301436

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

0 commit comments

Comments
 (0)