[Python-checkins] r56299 - in python/branches/release25-maint: Lib/test/test_format.py Misc/NEWS Objects/stringobject.c Objects/unicodeobject.c

georg.brandl python-checkins at python.org
Thu Jul 12 10:38:05 CEST 2007


Author: georg.brandl
Date: Thu Jul 12 10:38:04 2007
New Revision: 56299

Modified:
   python/branches/release25-maint/Lib/test/test_format.py
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/Objects/stringobject.c
   python/branches/release25-maint/Objects/unicodeobject.c
Log:
Patch #1673759: add a missing overflow check when formatting floats
with %G.
 (backport from rev. 56298)

Modified: python/branches/release25-maint/Lib/test/test_format.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_format.py	(original)
+++ python/branches/release25-maint/Lib/test/test_format.py	Thu Jul 12 10:38:04 2007
@@ -9,6 +9,7 @@
 # test on unicode strings as well
 
 overflowok = 1
+overflowrequired = 0
 
 def testformat(formatstr, args, output=None):
     if verbose:
@@ -25,11 +26,16 @@
         if verbose:
             print 'overflow (this is fine)'
     else:
-        if output and result != output:
+        if overflowrequired:
             if verbose:
                 print 'no'
-            print "%s %% %s == %s != %s" %\
-                (repr(formatstr), repr(args), repr(result), repr(output))
+            print "overflow expected on %s %% %s" % \
+                  (repr(formatstr), repr(args))
+        elif output and result != output:
+            if verbose:
+                print 'no'
+            print "%s %% %s == %s != %s" % \
+                  (repr(formatstr), repr(args), repr(result), repr(output))
         else:
             if verbose:
                 print 'yes'
@@ -57,6 +63,14 @@
 # test some ridiculously large precision, expect overflow
 testboth('%12.*f', (123456, 1.0))
 
+# check for internal overflow validation on length of precision
+overflowrequired = 1
+testboth("%#.*g", (110, -1.e+100/3.))
+testboth("%#.*G", (110, -1.e+100/3.))
+testboth("%#.*f", (110, -1.e+100/3.))
+testboth("%#.*F", (110, -1.e+100/3.))
+overflowrequired = 0
+
 # Formatting of long integers. Overflow is not ok
 overflowok = 0
 testboth("%x", 10L, "a")

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Thu Jul 12 10:38:04 2007
@@ -12,6 +12,9 @@
 Core and builtins
 -----------------
 
+- Patch #1673759: add a missing overflow check when formatting floats
+  with %G.
+
 - Patch #1733960: Allow T_LONGLONG to accept ints.
 
 - Prevent expandtabs() on string and unicode objects from causing a segfault

Modified: python/branches/release25-maint/Objects/stringobject.c
==============================================================================
--- python/branches/release25-maint/Objects/stringobject.c	(original)
+++ python/branches/release25-maint/Objects/stringobject.c	Thu Jul 12 10:38:04 2007
@@ -4188,7 +4188,8 @@
 	   always given), therefore increase the length by one.
 
 	*/
-	if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) ||
+	if (((type == 'g' || type == 'G') &&
+              buflen <= (size_t)10 + (size_t)prec) ||
 	    (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) {
 		PyErr_SetString(PyExc_OverflowError,
 			"formatted float is too long (precision too large?)");

Modified: python/branches/release25-maint/Objects/unicodeobject.c
==============================================================================
--- python/branches/release25-maint/Objects/unicodeobject.c	(original)
+++ python/branches/release25-maint/Objects/unicodeobject.c	Thu Jul 12 10:38:04 2007
@@ -7290,7 +7290,8 @@
        always given), therefore increase the length by one.
 
     */
-    if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) ||
+    if (((type == 'g' || type == 'G') && 
+          buflen <= (size_t)10 + (size_t)prec) ||
 	(type == 'f' && buflen <= (size_t)53 + (size_t)prec)) {
 	PyErr_SetString(PyExc_OverflowError,
 			"formatted float is too long (precision too large?)");


More information about the Python-checkins mailing list