]> granicus.if.org Git - python/commitdiff
correctly overflow when indexes are too large
authorBenjamin Peterson <benjamin@python.org>
Mon, 7 Jun 2010 22:23:23 +0000 (22:23 +0000)
committerBenjamin Peterson <benjamin@python.org>
Mon, 7 Jun 2010 22:23:23 +0000 (22:23 +0000)
Lib/test/test_unicode.py
Misc/NEWS
Objects/stringlib/string_format.h

index 4550be850ad2e75d2634aa0858797316f9097a1a..b3b24de94f525585a2761cb4e853860746fb69ff 100644 (file)
@@ -1282,6 +1282,9 @@ class UnicodeTest(
         self.assertRaises(IndexError, u"{:}".format)
         self.assertRaises(IndexError, u"{:s}".format)
         self.assertRaises(IndexError, u"{}".format)
+        big = "23098475029384702983476098230754973209482573"
+        self.assertRaises(ValueError, ("{" + big + "}").format)
+        self.assertRaises(ValueError, ("{[" + big + "]}").format, [0])
 
         # issue 6089
         self.assertRaises(ValueError, u"{0[0]x}".format, [None])
index dfdda16784133faf2cb59bb103eea5a8d5ca4c12..442422812bbb73863f1d430e04b118c00e02d146 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python release candidate 2?
 Core and Builtins
 -----------------
 
+- In the unicode/str.format(), raise a ValueError when either indexes to
+  arguments are too large.
+
 Library
 -------
 
index 0aafb14286682748f0a3b5929b7272692f29d645..e7bf724b39517a7b23726df511177a23a561fa7c 100644 (file)
@@ -373,6 +373,8 @@ FieldNameIterator_next(FieldNameIterator *self, int *is_attribute,
         if (_FieldNameIterator_item(self, name) == 0)
             return 0;
         *name_idx = get_integer(name);
+        if (*name_idx == -1 && PyErr_Occurred())
+            return 0;
         break;
     default:
         /* Invalid character follows ']' */
@@ -429,6 +431,8 @@ field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first,
 
     /* see if "first" is an integer, in which case it's used as an index */
     *first_idx = get_integer(first);
+    if (*first_idx == -1 && PyErr_Occurred())
+        return 0;
 
     field_name_is_empty = first->ptr >= first->end;