summaryrefslogtreecommitdiffstats
path: root/Objects/bytesobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-01-12 07:22:29 (GMT)
committerGitHub <noreply@github.com>2019-01-12 07:22:29 (GMT)
commit44cc4822bb3799858201e61294c5863f93ec12e2 (patch)
treef7d25e8a7403e81d86893c8a2f7f5402297bd903 /Objects/bytesobject.c
parentd0d3e99120b19a4b800f0f381b2807c93aeecf0e (diff)
downloadcpython-44cc4822bb3799858201e61294c5863f93ec12e2.zip
cpython-44cc4822bb3799858201e61294c5863f93ec12e2.tar.gz
cpython-44cc4822bb3799858201e61294c5863f93ec12e2.tar.bz2
bpo-33817: Fix _PyBytes_Resize() for empty bytes object. (GH-11516)
Add also tests for PyUnicode_FromFormat() and PyBytes_FromFormat() with empty result.
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r--Objects/bytesobject.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index adf0cff..40ef471 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2991,9 +2991,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);