summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-07-29 12:05:30 (GMT)
committerGitHub <noreply@github.com>2021-07-29 12:05:30 (GMT)
commit761c641f19838517bcf8df5b91d2eb46880efe68 (patch)
tree62bbebb4eca83d013bd43cf5ef734fc6db46f9ae /Objects
parent6b922dae6cae9f212d678a36e8b41f2287cf5441 (diff)
downloadcpython-761c641f19838517bcf8df5b91d2eb46880efe68.zip
cpython-761c641f19838517bcf8df5b91d2eb46880efe68.tar.gz
cpython-761c641f19838517bcf8df5b91d2eb46880efe68.tar.bz2
bpo-44707: Fix an undefined behavior of the null pointer arithmetic (GH-27292) (GH-27442)
(cherry picked from commit e5c8ddb1714fb51ab1defa24352c98e0f01205dc) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/listobject.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 6eb7dce..898cbc2 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -180,9 +180,10 @@ PyList_New(Py_ssize_t size)
static PyObject *
list_new_prealloc(Py_ssize_t size)
{
+ assert(size > 0);
PyListObject *op = (PyListObject *) PyList_New(0);
- if (size == 0 || op == NULL) {
- return (PyObject *) op;
+ if (op == NULL) {
+ return NULL;
}
assert(op->ob_item == NULL);
op->ob_item = PyMem_New(PyObject *, size);
@@ -459,6 +460,9 @@ list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
PyObject **src, **dest;
Py_ssize_t i, len;
len = ihigh - ilow;
+ if (len <= 0) {
+ return PyList_New(0);
+ }
np = (PyListObject *) list_new_prealloc(len);
if (np == NULL)
return NULL;
@@ -512,6 +516,9 @@ list_concat(PyListObject *a, PyObject *bb)
#define b ((PyListObject *)bb)
assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
size = Py_SIZE(a) + Py_SIZE(b);
+ if (size == 0) {
+ return PyList_New(0);
+ }
np = (PyListObject *) list_new_prealloc(size);
if (np == NULL) {
return NULL;