Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5295532

Browse files
committed
merge 3.3 (closes #27760)
2 parents 40a77c3 + 4f97651 commit 5295532

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

19+
- Issue #27760: Fix possible integer overflow in binascii.b2a_qp.
20+
1921
- Issue #27758: Fix possible integer overflow in the _csv module for large record
2022
lengths.
2123

Modules/binascii.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,7 @@ binascii_b2a_qp_impl(PyModuleDef *module, Py_buffer *data, int quotetabs, int is
14081408
/* First, scan to see how many characters need to be encoded */
14091409
in = 0;
14101410
while (in < datalen) {
1411+
Py_ssize_t delta = 0;
14111412
if ((databuf[in] > 126) ||
14121413
(databuf[in] == '=') ||
14131414
(header && databuf[in] == '_') ||
@@ -1422,12 +1423,12 @@ binascii_b2a_qp_impl(PyModuleDef *module, Py_buffer *data, int quotetabs, int is
14221423
if ((linelen + 3) >= MAXLINESIZE) {
14231424
linelen = 0;
14241425
if (crlf)
1425-
odatalen += 3;
1426+
delta += 3;
14261427
else
1427-
odatalen += 2;
1428+
delta += 2;
14281429
}
14291430
linelen += 3;
1430-
odatalen += 3;
1431+
delta += 3;
14311432
in++;
14321433
}
14331434
else {
@@ -1439,11 +1440,11 @@ binascii_b2a_qp_impl(PyModuleDef *module, Py_buffer *data, int quotetabs, int is
14391440
linelen = 0;
14401441
/* Protect against whitespace on end of line */
14411442
if (in && ((databuf[in-1] == ' ') || (databuf[in-1] == '\t')))
1442-
odatalen += 2;
1443+
delta += 2;
14431444
if (crlf)
1444-
odatalen += 2;
1445+
delta += 2;
14451446
else
1446-
odatalen += 1;
1447+
delta += 1;
14471448
if (databuf[in] == '\r')
14481449
in += 2;
14491450
else
@@ -1455,15 +1456,20 @@ binascii_b2a_qp_impl(PyModuleDef *module, Py_buffer *data, int quotetabs, int is
14551456
(linelen + 1) >= MAXLINESIZE) {
14561457
linelen = 0;
14571458
if (crlf)
1458-
odatalen += 3;
1459+
delta += 3;
14591460
else
1460-
odatalen += 2;
1461+
delta += 2;
14611462
}
14621463
linelen++;
1463-
odatalen++;
1464+
delta++;
14641465
in++;
14651466
}
14661467
}
1468+
if (PY_SSIZE_T_MAX - delta < odatalen) {
1469+
PyErr_NoMemory();
1470+
return NULL;
1471+
}
1472+
odatalen += delta;
14671473
}
14681474

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

0 commit comments

Comments
 (0)