From b15c2190defef3bdb18cb7fd9b77b5b484b7b45e Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 4 Mar 2025 06:28:43 -0500 Subject: [PATCH 1/4] gh-130824: Add tests for NULL in PyLong_*AndOverflow functions --- Lib/test/test_capi/test_long.py | 3 ++- Modules/_testlimitedcapi/long.c | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_capi/test_long.py b/Lib/test/test_capi/test_long.py index d45ac75c822ea9..ee70730f5402da 100644 --- a/Lib/test/test_capi/test_long.py +++ b/Lib/test/test_capi/test_long.py @@ -211,9 +211,10 @@ def check_long_asintandoverflow(self, func, min_val, max_val): self.assertEqual(func(min_val - 1), (-1, -1)) self.assertEqual(func(max_val + 1), (-1, +1)) + with self.assertRaises(SystemError): + func(None) # CRASHES func(1.0) - # CRASHES func(NULL) def test_long_asint(self): # Test PyLong_AsInt() diff --git a/Modules/_testlimitedcapi/long.c b/Modules/_testlimitedcapi/long.c index b9c35803b423c2..d30b586c007f3a 100644 --- a/Modules/_testlimitedcapi/long.c +++ b/Modules/_testlimitedcapi/long.c @@ -625,7 +625,8 @@ pylong_aslongandoverflow(PyObject *module, PyObject *arg) int overflow = UNINITIALIZED_INT; long value = PyLong_AsLongAndOverflow(arg, &overflow); if (value == -1 && PyErr_Occurred()) { - assert(overflow == -1); + // overflow can be 0 if a seperate exception occurred + assert(overflow == -1 || overflow == 0); return NULL; } return Py_BuildValue("li", value, overflow); @@ -671,7 +672,7 @@ pylong_aslonglongandoverflow(PyObject *module, PyObject *arg) int overflow = UNINITIALIZED_INT; long long value = PyLong_AsLongLongAndOverflow(arg, &overflow); if (value == -1 && PyErr_Occurred()) { - assert(overflow == -1); + assert(overflow == -1 || overflow == 0); return NULL; } return Py_BuildValue("Li", value, overflow); From 0fc0c2137ea7447b7265e0cc71a0bb6318652eea Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 4 Mar 2025 09:17:11 -0500 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Sergey B Kirpichev --- Lib/test/test_capi/test_long.py | 3 +-- Modules/_testlimitedcapi/long.c | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_capi/test_long.py b/Lib/test/test_capi/test_long.py index ee70730f5402da..b1e16431a95524 100644 --- a/Lib/test/test_capi/test_long.py +++ b/Lib/test/test_capi/test_long.py @@ -211,8 +211,7 @@ def check_long_asintandoverflow(self, func, min_val, max_val): self.assertEqual(func(min_val - 1), (-1, -1)) self.assertEqual(func(max_val + 1), (-1, +1)) - with self.assertRaises(SystemError): - func(None) + self.assertRaises(SystemError, func, None) # CRASHES func(1.0) diff --git a/Modules/_testlimitedcapi/long.c b/Modules/_testlimitedcapi/long.c index d30b586c007f3a..6aac73298b8a70 100644 --- a/Modules/_testlimitedcapi/long.c +++ b/Modules/_testlimitedcapi/long.c @@ -672,6 +672,7 @@ pylong_aslonglongandoverflow(PyObject *module, PyObject *arg) int overflow = UNINITIALIZED_INT; long long value = PyLong_AsLongLongAndOverflow(arg, &overflow); if (value == -1 && PyErr_Occurred()) { + // overflow can be 0 if a seperate exception occurred assert(overflow == -1 || overflow == 0); return NULL; } From 46cd27f916f56169e59e9415e29846eb408c7d1a Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 4 Mar 2025 20:06:57 -0500 Subject: [PATCH 3/4] Add check for TypeError --- Lib/test/test_capi/test_long.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_capi/test_long.py b/Lib/test/test_capi/test_long.py index b1e16431a95524..a371f634432b6d 100644 --- a/Lib/test/test_capi/test_long.py +++ b/Lib/test/test_capi/test_long.py @@ -212,8 +212,7 @@ def check_long_asintandoverflow(self, func, min_val, max_val): self.assertEqual(func(min_val - 1), (-1, -1)) self.assertEqual(func(max_val + 1), (-1, +1)) self.assertRaises(SystemError, func, None) - - # CRASHES func(1.0) + self.assertRaises(TypeError, func, 1.0) def test_long_asint(self): # Test PyLong_AsInt() From 8bb2502e5040eb60c6d0535e601405cd2d50ee2a Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 4 Mar 2025 20:56:25 -0500 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Sergey B Kirpichev --- Modules/_testlimitedcapi/long.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_testlimitedcapi/long.c b/Modules/_testlimitedcapi/long.c index 6aac73298b8a70..d896435c99a169 100644 --- a/Modules/_testlimitedcapi/long.c +++ b/Modules/_testlimitedcapi/long.c @@ -625,7 +625,7 @@ pylong_aslongandoverflow(PyObject *module, PyObject *arg) int overflow = UNINITIALIZED_INT; long value = PyLong_AsLongAndOverflow(arg, &overflow); if (value == -1 && PyErr_Occurred()) { - // overflow can be 0 if a seperate exception occurred + // overflow can be 0 if a separate exception occurred assert(overflow == -1 || overflow == 0); return NULL; } @@ -672,7 +672,7 @@ pylong_aslonglongandoverflow(PyObject *module, PyObject *arg) int overflow = UNINITIALIZED_INT; long long value = PyLong_AsLongLongAndOverflow(arg, &overflow); if (value == -1 && PyErr_Occurred()) { - // overflow can be 0 if a seperate exception occurred + // overflow can be 0 if a separate exception occurred assert(overflow == -1 || overflow == 0); return NULL; }