Author: Brian Kearns <[email protected]>
Branch: stdlib-2.7.6
Changeset: r69609:acad125f3839
Date: 2014-03-02 05:40 -0500
http://bitbucket.org/pypy/pypy/changeset/acad125f3839/

Log:    another string formatting overflow fix

diff --git a/pypy/objspace/std/formatting.py b/pypy/objspace/std/formatting.py
--- a/pypy/objspace/std/formatting.py
+++ b/pypy/objspace/std/formatting.py
@@ -218,7 +218,7 @@
 
             self.peel_flags()
 
-            self.width = self.peel_num('width', sys.maxint)
+            self.width = self.peel_num('width', self.space.int_w, sys.maxint)
             if self.width < 0:
                 # this can happen:  '%*s' % (-5, "hi")
                 self.f_ljust = True
@@ -226,7 +226,7 @@
 
             if self.peekchr() == '.':
                 self.forward()
-                self.prec = self.peel_num('prec', INT_MAX)
+                self.prec = self.peel_num('prec', self.space.c_int_w, INT_MAX)
                 if self.prec < 0:
                     self.prec = 0    # this can happen:  '%.*f' % (-5, 3)
             else:
@@ -264,13 +264,13 @@
 
         # Same as getmappingkey
         @jit.unroll_safe
-        def peel_num(self, name, maxval):
+        def peel_num(self, name, conv_w, maxval):
             space = self.space
             c = self.peekchr()
             if c == '*':
                 self.forward()
                 w_value = self.nextinputvalue()
-                return space.int_w(maybe_int(space, w_value))
+                return conv_w(w_value)
             result = 0
             while True:
                 digit = ord(c) - ord('0')
diff --git a/pypy/objspace/std/test/test_stringformat.py 
b/pypy/objspace/std/test/test_stringformat.py
--- a/pypy/objspace/std/test/test_stringformat.py
+++ b/pypy/objspace/std/test/test_stringformat.py
@@ -205,9 +205,11 @@
         assert "%x" % IntFails() == '0'
 
     def test_formatting_huge_precision(self):
-        format_string = "%.{}f".format(2**31)
+        prec = 2**31
+        format_string = "%.{}f".format(prec)
         exc = raises(ValueError, "format_string % 2.34")
         assert exc.value[0] == 'prec too big'
+        raises(OverflowError, lambda: u'%.*f' % (prec, 1. / 7))
 
     def test_formatting_huge_width(self):
         import sys
@@ -336,9 +338,11 @@
         raises(ValueError, 'u"%\u1234" % (f,)')
 
     def test_formatting_huge_precision(self):
-        format_string = u"%.{}f".format(2**31)
+        prec = 2**31
+        format_string = u"%.{}f".format(prec)
         exc = raises(ValueError, "format_string % 2.34")
         assert exc.value[0] == 'prec too big'
+        raises(OverflowError, lambda: u'%.*f' % (prec, 1. / 7))
 
     def test_formatting_huge_width(self):
         import sys
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to