]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #25270: Prevent codecs.escape_encode() from raising SystemError when an empty...
authorBerker Peksag <berker.peksag@gmail.com>
Fri, 16 Sep 2016 14:31:06 +0000 (17:31 +0300)
committerBerker Peksag <berker.peksag@gmail.com>
Fri, 16 Sep 2016 14:31:06 +0000 (17:31 +0300)
Doc/c-api/bytes.rst
Lib/test/test_codecs.py
Misc/NEWS
Objects/bytesobject.c

index dcd70886298a51f608fea1519b2a11a130f4daf7..ee42f854ed821d5fe6746618119a93ad88acb736 100644 (file)
@@ -198,5 +198,5 @@ called with a non-bytes parameter.
    desired.  On success, *\*bytes* holds the resized bytes object and ``0`` is
    returned; the address in *\*bytes* may differ from its input value.  If the
    reallocation fails, the original bytes object at *\*bytes* is deallocated,
-   *\*bytes* is set to *NULL*, a memory exception is set, and ``-1`` is
+   *\*bytes* is set to *NULL*, :exc:`MemoryError` is set, and ``-1`` is
    returned.
index 04795427454b05edb22c9197956bc5d650366d03..013ec64b2097d2dbdeb55ecbecd0cc44f279470e 100644 (file)
@@ -2501,6 +2501,26 @@ class RawUnicodeEscapeTest(unittest.TestCase):
         self.assertEqual(decode(br"\U00110000", "replace"), ("\ufffd", 10))
 
 
+class EscapeEncodeTest(unittest.TestCase):
+
+    def test_escape_encode(self):
+        tests = [
+            (b'', (b'', 0)),
+            (b'foobar', (b'foobar', 6)),
+            (b'spam\0eggs', (b'spam\\x00eggs', 9)),
+            (b'a\'b', (b"a\\'b", 3)),
+            (b'b\\c', (b'b\\\\c', 3)),
+            (b'c\nd', (b'c\\nd', 3)),
+            (b'd\re', (b'd\\re', 3)),
+            (b'f\x7fg', (b'f\\x7fg', 3)),
+        ]
+        for data, output in tests:
+            with self.subTest(data=data):
+                self.assertEqual(codecs.escape_encode(data), output)
+        self.assertRaises(TypeError, codecs.escape_encode, 'spam')
+        self.assertRaises(TypeError, codecs.escape_encode, bytearray(b'spam'))
+
+
 class SurrogateEscapeTest(unittest.TestCase):
 
     def test_utf8(self):
index 218513e70a47125fa34cd7c4e6ef6e036e84eaf1..f7fe181d295d509cbc0bd45bda574852ac12f8fb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -71,6 +71,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #25270: Prevent codecs.escape_encode() from raising SystemError when
+  an empty bytestring is passed.
+
 - Issue #28181: Get antigravity over HTTPS. Patch by Kaartic Sivaraam.
 
 - Issue #25895: Enable WebSocket URL schemes in urllib.parse.urljoin.
index 5934336f892e360976d6661a9e606458568c2da0..393432870b1e4053668d5e3bd4fad7bac6c53710 100644 (file)
@@ -3550,11 +3550,15 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
     PyObject *v;
     PyBytesObject *sv;
     v = *pv;
-    if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) {
-        *pv = 0;
-        Py_DECREF(v);
-        PyErr_BadInternalCall();
-        return -1;
+    if (!PyBytes_Check(v) || newsize < 0) {
+        goto error;
+    }
+    if (Py_SIZE(v) == newsize) {
+        /* return early if newsize equals to v->ob_size */
+        return 0;
+    }
+    if (Py_REFCNT(v) != 1) {
+        goto error;
     }
     /* XXX UNREF/NEWREF interface should be more symmetrical */
     _Py_DEC_REFTOTAL;
@@ -3572,6 +3576,11 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
     sv->ob_sval[newsize] = '\0';
     sv->ob_shash = -1;          /* invalidate cached hash value */
     return 0;
+error:
+    *pv = 0;
+    Py_DECREF(v);
+    PyErr_BadInternalCall();
+    return -1;
 }
 
 void