Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51b36ed

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 <[email protected]>
1 parent fd27fb7 commit 51b36ed

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
@@ -273,7 +273,9 @@ PyByteArray_Concat(PyObject *a, PyObject *b)
273273

274274
result = (PyByteArrayObject *) \
275275
PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
276-
if (result != NULL) {
276+
// result->ob_bytes is NULL if result is an empty string:
277+
// if va.len + vb.len equals zero.
278+
if (result != NULL && result->ob_bytes != NULL) {
277279
memcpy(result->ob_bytes, va.buf, va.len);
278280
memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
279281
}

0 commit comments

Comments
 (0)