gh-111174: Fix crash in getbuffer() called repeatedly for empty BytesIO (GH-111210)

This commit is contained in:
Serhiy Storchaka 2023-10-25 13:50:16 +03:00 committed by GitHub
parent f6a45a03d0
commit 9da98c0d9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 3 deletions

View file

@ -126,12 +126,13 @@ unshare_buffer(bytesio *self, size_t size)
static int
resize_buffer(bytesio *self, size_t size)
{
assert(self->buf != NULL);
assert(self->exports == 0);
/* Here, unsigned types are used to avoid dealing with signed integer
overflow, which is undefined in C. */
size_t alloc = PyBytes_GET_SIZE(self->buf);
assert(self->buf != NULL);
/* For simplicity, stay in the range of the signed type. Anyway, Python
doesn't allow strings to be longer than this. */
if (size > PY_SSIZE_T_MAX)
@ -1074,7 +1075,7 @@ bytesiobuf_getbuffer(bytesiobuf *obj, Py_buffer *view, int flags)
"bytesiobuf_getbuffer: view==NULL argument is obsolete");
return -1;
}
if (SHARED_BUF(b)) {
if (b->exports == 0 && SHARED_BUF(b)) {
if (unshare_buffer(b, b->string_size) < 0)
return -1;
}