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

Skip to content

Commit 8eeae21

Browse files
Issue #18184: PyUnicode_FromFormat() and PyUnicode_FromFormatV() now raise
OverflowError when an argument of %c format is out of range.
1 parent 36a7e4f commit 8eeae21

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

Lib/test/test_unicode.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,6 +2024,8 @@ def PyUnicode_FromFormat(format, *args):
20242024
# test "%c"
20252025
self.assertEqual(PyUnicode_FromFormat(b'%c', c_int(0xabcd)), '\uabcd')
20262026
self.assertEqual(PyUnicode_FromFormat(b'%c', c_int(0x10ffff)), '\U0010ffff')
2027+
with self.assertRaises(OverflowError):
2028+
PyUnicode_FromFormat(b'%c', c_int(0x110000))
20272029
# Issue #18183
20282030
self.assertEqual(
20292031
PyUnicode_FromFormat(b'%c%c', c_int(0x10000), c_int(0x100000)),

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.3.3 release candidate 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #18184: PyUnicode_FromFormat() and PyUnicode_FromFormatV() now raise
16+
OverflowError when an argument of %c format is out of range.
17+
1518
- Issue #18137: Detect integer overflow on precision in float.__format__()
1619
and complex.__format__().
1720

Objects/unicodeobject.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,8 +2489,13 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
24892489
switch (*f) {
24902490
case 'c':
24912491
{
2492-
Py_UCS4 ordinal = va_arg(count, int);
2493-
maxchar = Py_MAX(maxchar, ordinal);
2492+
int ordinal = va_arg(count, int);
2493+
if (ordinal < 0 || ordinal > MAX_UNICODE) {
2494+
PyErr_SetString(PyExc_OverflowError,
2495+
"%c arg not in range(0x110000)");
2496+
goto fail;
2497+
}
2498+
maxchar = Py_MAX(maxchar, (Py_UCS4)ordinal);
24942499
n++;
24952500
break;
24962501
}

0 commit comments

Comments
 (0)