Skip to content

Commit 33672c0

Browse files
bpo-41175: Guard against a NULL pointer dereference within bytearrayobject (GH-21240)
The issue is triggered by the bytearray() + bytearray() operation. Detected by GCC 10 static analysis tool. (cherry picked from commit 61fc23c) Co-authored-by: stratakis <cstratak@redhat.com>
1 parent 3d1c06e commit 33672c0

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Guard against a NULL pointer dereference within bytearrayobject triggered by
2+
the ``bytearray() + bytearray()`` operation.

Objects/bytearrayobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ PyByteArray_Concat(PyObject *a, PyObject *b)
276276

277277
result = (PyByteArrayObject *) \
278278
PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
279-
if (result != NULL) {
279+
// result->ob_bytes is NULL if result is an empty string:
280+
// if va.len + vb.len equals zero.
281+
if (result != NULL && result->ob_bytes != NULL) {
280282
memcpy(result->ob_bytes, va.buf, va.len);
281283
memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
282284
}

0 commit comments

Comments
 (0)