From 2550a0021af4bbb5b22a4e1af89ea49eeb535ba0 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 9 Jun 2023 21:47:41 +0200 Subject: [PATCH] gh-105375: Improve array.array exception handling Fix a bug where 'tp_richcompare' could end up overwriting an exception. --- .../2023-06-09-21-46-52.gh-issue-105375.yrJelV.rst | 2 ++ Modules/arraymodule.c | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-06-09-21-46-52.gh-issue-105375.yrJelV.rst diff --git a/Misc/NEWS.d/next/Library/2023-06-09-21-46-52.gh-issue-105375.yrJelV.rst b/Misc/NEWS.d/next/Library/2023-06-09-21-46-52.gh-issue-105375.yrJelV.rst new file mode 100644 index 00000000000000..21aea1b0b4082c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-09-21-46-52.gh-issue-105375.yrJelV.rst @@ -0,0 +1,2 @@ +Fix a bug in :class:`array.array` where an exception could end up being +overwritten. diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 16e3739eb26fc3..4c1ba2068d2d8c 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -767,10 +767,12 @@ array_richcompare(PyObject *v, PyObject *w, int op) k = 1; for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) { vi = getarrayitem(v, i); + if (vi == NULL) { + return NULL; + } wi = getarrayitem(w, i); - if (vi == NULL || wi == NULL) { - Py_XDECREF(vi); - Py_XDECREF(wi); + if (wi == NULL) { + Py_DECREF(vi); return NULL; } k = PyObject_RichCompareBool(vi, wi, Py_EQ);