]> granicus.if.org Git - python/commitdiff
Issue #23466: Raised OverflowError if %c argument is out of range.
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 3 Apr 2015 17:53:46 +0000 (20:53 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 3 Apr 2015 17:53:46 +0000 (20:53 +0300)
Lib/test/test_format.py
Objects/bytesobject.c

index 5a2a357e081c6af1efd5a26df7e96676d949859f..2dad95844d519d5614fbef2fdfb1b60894303f20 100644 (file)
@@ -358,12 +358,12 @@ class FormatTest(unittest.TestCase):
                  "not all arguments converted during bytes formatting")
         test_exc(b'no format', bytearray(b'1'), TypeError,
                  "not all arguments converted during bytes formatting")
-        test_exc(b"%c", -1, TypeError,
-                "%c requires an integer in range(256) or a single byte")
-        test_exc(b"%c", 256, TypeError,
-                "%c requires an integer in range(256) or a single byte")
-        test_exc(b"%c", 2**128, TypeError,
-                "%c requires an integer in range(256) or a single byte")
+        test_exc(b"%c", -1, OverflowError,
+                "%c arg not in range(256)")
+        test_exc(b"%c", 256, OverflowError,
+                "%c arg not in range(256)")
+        test_exc(b"%c", 2**128, OverflowError,
+                "%c arg not in range(256)")
         test_exc(b"%c", b"Za", TypeError,
                 "%c requires an integer in range(256) or a single byte")
         test_exc(b"%c", "Y", TypeError,
index 5a2d41c5a8f5ea2e6b0f26169610f0a2e993c371..27d6472292f685d812fb0837ab2d63127cb09d36 100644 (file)
@@ -496,10 +496,15 @@ byte_converter(PyObject *arg, char *p)
             ival = PyLong_AsLongAndOverflow(iobj, &overflow);
             Py_DECREF(iobj);
         }
-        if (!overflow && 0 <= ival && ival <= 255) {
-            *p = (char)ival;
-            return 1;
+        if (!overflow && ival == -1 && PyErr_Occurred())
+            goto onError;
+        if (overflow || !(0 <= ival && ival <= 255)) {
+            PyErr_SetString(PyExc_OverflowError,
+                            "%c arg not in range(256)");
+            return 0;
         }
+        *p = (char)ival;
+        return 1;
     }
   onError:
     PyErr_SetString(PyExc_TypeError,