From bca8b2beff7f9613817d4a7d1d98814e872517fe Mon Sep 17 00:00:00 2001 From: Marco Paolini Date: Sun, 3 Nov 2019 23:29:23 +0000 Subject: [PATCH 1/8] bpo-38677: Fix arraymodule error handling in module initialization --- Modules/arraymodule.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 5ba261819db2ce..be7fa2d6f65e45 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -3040,9 +3040,11 @@ array_modexec(PyObject *m) Py_TYPE(&PyArrayIter_Type) = &PyType_Type; Py_INCREF((PyObject *)&Arraytype); - PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype); + if (PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype) < 0) + return -1; Py_INCREF((PyObject *)&Arraytype); - PyModule_AddObject(m, "array", (PyObject *)&Arraytype); + if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) + return -1; for (descr=descriptors; descr->typecode != '\0'; descr++) { size++; @@ -3053,13 +3055,11 @@ array_modexec(PyObject *m) *p++ = (char)descr->typecode; } typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); + assert(typecodes != NULL); - PyModule_AddObject(m, "typecodes", typecodes); + if (PyModule_AddObject(m, "typecodes", typecodes) < 0) + return -1; - if (PyErr_Occurred()) { - Py_DECREF(m); - m = NULL; - } return 0; } From e9a1cf03953b7382b96bee14c876d98f62ee06af Mon Sep 17 00:00:00 2001 From: Marco Paolini Date: Mon, 4 Nov 2019 17:06:39 +0000 Subject: [PATCH 2/8] Update Modules/arraymodule.c Co-Authored-By: Brandt Bucher --- Modules/arraymodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index be7fa2d6f65e45..8c7a7998bc3960 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -3041,7 +3041,8 @@ array_modexec(PyObject *m) Py_INCREF((PyObject *)&Arraytype); if (PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype) < 0) - return -1; + return -1; + } Py_INCREF((PyObject *)&Arraytype); if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) return -1; From 71cbac8c6fdf1d407891ba6cff01a3c48114f735 Mon Sep 17 00:00:00 2001 From: Marco Paolini Date: Mon, 4 Nov 2019 17:06:50 +0000 Subject: [PATCH 3/8] Update Modules/arraymodule.c Co-Authored-By: Brandt Bucher --- Modules/arraymodule.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 8c7a7998bc3960..58ba76d08c623e 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -3044,7 +3044,9 @@ array_modexec(PyObject *m) return -1; } Py_INCREF((PyObject *)&Arraytype); - if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) + if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) { + Py_DECREF((PyObject *)&Arraytype); + Py_CLEAR(m); return -1; for (descr=descriptors; descr->typecode != '\0'; descr++) { From 854ccdb72c80fec2c59ac81c6527b7e3337b2dfd Mon Sep 17 00:00:00 2001 From: Marco Paolini Date: Mon, 4 Nov 2019 17:07:13 +0000 Subject: [PATCH 4/8] Update Modules/arraymodule.c Co-Authored-By: Brandt Bucher --- Modules/arraymodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 58ba76d08c623e..bad1094da6c231 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -3061,7 +3061,8 @@ array_modexec(PyObject *m) assert(typecodes != NULL); if (PyModule_AddObject(m, "typecodes", typecodes) < 0) - return -1; + return -1; + } return 0; } From 02e901f7d8103ad0b7de9defa338c860355abd60 Mon Sep 17 00:00:00 2001 From: Marco Paolini Date: Mon, 4 Nov 2019 17:07:34 +0000 Subject: [PATCH 5/8] Update Modules/arraymodule.c Co-Authored-By: Brandt Bucher --- Modules/arraymodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index bad1094da6c231..bd99e523073b1e 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -3047,7 +3047,8 @@ array_modexec(PyObject *m) if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) { Py_DECREF((PyObject *)&Arraytype); Py_CLEAR(m); - return -1; + return -1; + } for (descr=descriptors; descr->typecode != '\0'; descr++) { size++; From 91a821be310615f179e8e2e98081bdd4f6e0c492 Mon Sep 17 00:00:00 2001 From: Marco Paolini Date: Mon, 4 Nov 2019 18:27:07 +0000 Subject: [PATCH 6/8] Fix some syntax errors reverting some previous commits --- Modules/arraymodule.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index bd99e523073b1e..454d7df0c3ef41 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -3040,13 +3040,13 @@ array_modexec(PyObject *m) Py_TYPE(&PyArrayIter_Type) = &PyType_Type; Py_INCREF((PyObject *)&Arraytype); - if (PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype) < 0) + if (PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype) < 0) { + Py_DECREF((PyObject *)&Arraytype); return -1; } Py_INCREF((PyObject *)&Arraytype); if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) { Py_DECREF((PyObject *)&Arraytype); - Py_CLEAR(m); return -1; } @@ -3061,7 +3061,8 @@ array_modexec(PyObject *m) typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); assert(typecodes != NULL); - if (PyModule_AddObject(m, "typecodes", typecodes) < 0) + if (PyModule_AddObject(m, "typecodes", typecodes) < 0) { + Py_DECREF(typecodes); return -1; } From 13e3bb6e47f7c5cde84c656a0f318544464029fb Mon Sep 17 00:00:00 2001 From: Marco Paolini Date: Mon, 4 Nov 2019 19:00:25 +0000 Subject: [PATCH 7/8] Correctly handle DecodeAscii errors in module exec --- Modules/arraymodule.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 454d7df0c3ef41..5b5865870916e2 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -3059,8 +3059,9 @@ array_modexec(PyObject *m) *p++ = (char)descr->typecode; } typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); - assert(typecodes != NULL); - + if (typecodes == NULL) { + return -1; + } if (PyModule_AddObject(m, "typecodes", typecodes) < 0) { Py_DECREF(typecodes); return -1; From 777fc61cd2a06c17055ed6d2c7e59069f75c1223 Mon Sep 17 00:00:00 2001 From: Marco Paolini Date: Thu, 14 Nov 2019 18:38:05 +0000 Subject: [PATCH 8/8] Avoid useless typecodes error check --- Modules/arraymodule.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 5b5865870916e2..1ceba9e63cbdf4 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -3059,11 +3059,8 @@ array_modexec(PyObject *m) *p++ = (char)descr->typecode; } typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); - if (typecodes == NULL) { - return -1; - } if (PyModule_AddObject(m, "typecodes", typecodes) < 0) { - Py_DECREF(typecodes); + Py_XDECREF(typecodes); return -1; }