]> granicus.if.org Git - python/commitdiff
Patch #1673759: add a missing overflow check when formatting floats
authorGeorg Brandl <georg@python.org>
Thu, 12 Jul 2007 08:38:00 +0000 (08:38 +0000)
committerGeorg Brandl <georg@python.org>
Thu, 12 Jul 2007 08:38:00 +0000 (08:38 +0000)
with %G.

Lib/test/test_format.py
Misc/NEWS
Objects/stringobject.c
Objects/unicodeobject.c

index 61e44f9129da0944f9328cb935827c356fe16ab5..c4cfa11935478dcd70b765e605375bfe89d1c784 100644 (file)
@@ -9,6 +9,7 @@ maxsize = MAX_Py_ssize_t
 # test on unicode strings as well
 
 overflowok = 1
+overflowrequired = 0
 
 def testformat(formatstr, args, output=None):
     if verbose:
@@ -25,11 +26,16 @@ def testformat(formatstr, args, output=None):
         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 @@ testboth("%#.*g", (110, -1.e+100/3.))
 # 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")
index dfec57b2a6e6ffe4dd5bcf621fad4697ddf9e205..9bf357b6a5ef59416315d90bbb79d9329d336568 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
 Core and builtins
 -----------------
 
+- Patch #1673759: add a missing overflow check when formatting floats
+  with %G.
+
 - Patch #1733960: Allow T_LONGLONG to accept ints.
 
 - T_PYSSIZET can now be used in PyMemberDef lists for Py_ssize_t members.
index 387034385c78faed1edd111b8b7b179d590d8464..504b119d6e9f144e7535d89791f0703b056f3402 100644 (file)
@@ -4198,7 +4198,8 @@ formatfloat(char *buf, size_t buflen, int flags,
           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?)");
index ad33b8e110f149801f2955fa3b7a0374c18fea60..85804f1e36608fae8ed109c44b06258ea359eb62 100644 (file)
@@ -7294,7 +7294,8 @@ formatfloat(Py_UNICODE *buf,
        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?)");