Skip to content

bpo-33817: Fix _PyBytes_Resize() for empty bytes object. #11516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,12 @@ def ptr_formatter(ptr):
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
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2680,6 +2680,12 @@ def check_format(expected, format, *args):
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed :c:func:`_PyBytes_Resize` for empty bytes objects.
13 changes: 13 additions & 0 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down