Skip to content

Commit 13e934a

Browse files
committed
correctly overflow when indexes are too large
1 parent 0b41707 commit 13e934a

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

Lib/test/test_unicode.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,9 @@ def __format__(self, format_spec):
12821282
self.assertRaises(IndexError, u"{:}".format)
12831283
self.assertRaises(IndexError, u"{:s}".format)
12841284
self.assertRaises(IndexError, u"{}".format)
1285+
big = "23098475029384702983476098230754973209482573"
1286+
self.assertRaises(ValueError, ("{" + big + "}").format)
1287+
self.assertRaises(ValueError, ("{[" + big + "]}").format, [0])
12851288

12861289
# issue 6089
12871290
self.assertRaises(ValueError, u"{0[0]x}".format, [None])

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 release candidate 2?
1212
Core and Builtins
1313
-----------------
1414

15+
- In the unicode/str.format(), raise a ValueError when either indexes to
16+
arguments are too large.
17+
1518
Library
1619
-------
1720

Objects/stringlib/string_format.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ FieldNameIterator_next(FieldNameIterator *self, int *is_attribute,
373373
if (_FieldNameIterator_item(self, name) == 0)
374374
return 0;
375375
*name_idx = get_integer(name);
376+
if (*name_idx == -1 && PyErr_Occurred())
377+
return 0;
376378
break;
377379
default:
378380
/* Invalid character follows ']' */
@@ -429,6 +431,8 @@ field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first,
429431

430432
/* see if "first" is an integer, in which case it's used as an index */
431433
*first_idx = get_integer(first);
434+
if (*first_idx == -1 && PyErr_Occurred())
435+
return 0;
432436

433437
field_name_is_empty = first->ptr >= first->end;
434438

0 commit comments

Comments
 (0)