]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-33817: Fix _PyBytes_Resize() for empty bytes object. (GH-11516)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 12 Jan 2019 07:40:09 +0000 (23:40 -0800)
committerGitHub <noreply@github.com>
Sat, 12 Jan 2019 07:40:09 +0000 (23:40 -0800)
Add also tests for PyUnicode_FromFormat() and PyBytes_FromFormat()
with empty result.
(cherry picked from commit 44cc4822bb3799858201e61294c5863f93ec12e2)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_bytes.py
Lib/test/test_unicode.py
Misc/NEWS.d/next/C API/2019-01-11-11-16-16.bpo-33817.nJ4yIj.rst [new file with mode: 0644]
Objects/bytesobject.c

index 145411efbb9d0cadcc6afe815eac5ee232dfa8cd..274616bf998dbc41b82b3cf37281e2a0f7447c60 100644 (file)
@@ -999,6 +999,12 @@ class BytesTest(BaseBytesTest, unittest.TestCase):
         self.assertRaises(OverflowError,
                           PyBytes_FromFormat, b'%c', c_int(256))
 
+        # Issue #33817: empty strings
+        self.assertEqual(PyBytes_FromFormat(b''),
+                         b'')
+        self.assertEqual(PyBytes_FromFormat(b'%s', b''),
+                         b'')
+
     def test_bytes_blocking(self):
         class IterationBlocked(list):
             __bytes__ = None
index 3cc018c0cc2caa8c3125233fde2a4d314dc4e0d9..1aad9334074c5cc22af16414826d8db67292e0f5 100644 (file)
@@ -2676,6 +2676,12 @@ class CAPITest(unittest.TestCase):
         check_format('%.%s',
                      b'%.%s', b'abc')
 
+        # Issue #33817: empty strings
+        check_format('',
+                     b'')
+        check_format('',
+                     b'%s', b'')
+
     # Test PyUnicode_AsWideChar()
     @support.cpython_only
     def test_aswidechar(self):
diff --git a/Misc/NEWS.d/next/C API/2019-01-11-11-16-16.bpo-33817.nJ4yIj.rst b/Misc/NEWS.d/next/C API/2019-01-11-11-16-16.bpo-33817.nJ4yIj.rst
new file mode 100644 (file)
index 0000000..ca4ccb2
--- /dev/null
@@ -0,0 +1 @@
+Fixed :c:func:`_PyBytes_Resize` for empty bytes objects.
index 711faba645488fd8b94c5ae1f8796b6d24ae0d5a..5f9e1eccf2e4a0479f96821c369a400d0d1a01dc 100644 (file)
@@ -2990,9 +2990,22 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
         /* return early if newsize equals to v->ob_size */
         return 0;
     }
+    if (Py_SIZE(v) == 0) {
+        if (newsize == 0) {
+            return 0;
+        }
+        *pv = _PyBytes_FromSize(newsize, 0);
+        Py_DECREF(v);
+        return (*pv == NULL) ? -1 : 0;
+    }
     if (Py_REFCNT(v) != 1) {
         goto error;
     }
+    if (newsize == 0) {
+        *pv = _PyBytes_FromSize(0, 0);
+        Py_DECREF(v);
+        return (*pv == NULL) ? -1 : 0;
+    }
     /* XXX UNREF/NEWREF interface should be more symmetrical */
     _Py_DEC_REFTOTAL;
     _Py_ForgetReference(v);