diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-11-01 02:47:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-01 02:47:29 (GMT) |
commit | 078ce6891c2d663babaf81b1e89f1fef82c007bc (patch) | |
tree | 26d9ca2a728aa922e4292fdb3fe98cc56b631214 | |
parent | d3d1738acd4f62869d7f1e119c257e2ee46fd16f (diff) | |
download | cpython-078ce6891c2d663babaf81b1e89f1fef82c007bc.zip cpython-078ce6891c2d663babaf81b1e89f1fef82c007bc.tar.gz cpython-078ce6891c2d663babaf81b1e89f1fef82c007bc.tar.bz2 |
GH-98897: fix memory leak if `math.dist` raises exception (GH-98898)
(cherry picked from commit ab575050709e2b313ca9a9585f09b6f4b0560318)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
-rw-r--r-- | Lib/test/test_math.py | 5 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-10-31-12-34-03.gh-issue-98897.rgNn4x.rst | 1 | ||||
-rw-r--r-- | Modules/mathmodule.c | 6 |
3 files changed, 9 insertions, 3 deletions
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index e5f4e2b..ada196a 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -979,6 +979,11 @@ class MathTests(unittest.TestCase): self.assertEqual(math.dist(p, q), 5*scale) self.assertEqual(math.dist(q, p), 5*scale) + def test_math_dist_leak(self): + # gh-98897: Check for error handling does not leak memory + with self.assertRaises(ValueError): + math.dist([1, 2], [3, 4, 5]) + def testIsqrt(self): # Test a variety of inputs, large and small. test_values = ( diff --git a/Misc/NEWS.d/next/Library/2022-10-31-12-34-03.gh-issue-98897.rgNn4x.rst b/Misc/NEWS.d/next/Library/2022-10-31-12-34-03.gh-issue-98897.rgNn4x.rst new file mode 100644 index 0000000..f61af25 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-10-31-12-34-03.gh-issue-98897.rgNn4x.rst @@ -0,0 +1 @@ +Fix memory leak in :func:`math.dist` when both points don't have the same dimension. Patch by Kumar Aditya. diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 4534176..f31ca83 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2658,13 +2658,13 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q) if (m != n) { PyErr_SetString(PyExc_ValueError, "both points must have the same number of dimensions"); - return NULL; - + goto error_exit; } if (n > NUM_STACK_ELEMS) { diffs = (double *) PyObject_Malloc(n * sizeof(double)); if (diffs == NULL) { - return PyErr_NoMemory(); + PyErr_NoMemory(); + goto error_exit; } } for (i=0 ; i<n ; i++) { |