Content-Length: 272176 | pFad | http://github.com/python/cpython/commit/078ce6891c2d663babaf81b1e89f1fef82c007bc

D2 GH-98897: fix memory leak if `math.dist` raises exception (GH-98898) · python/cpython@078ce68 · GitHub
Skip to content

Commit

Permalink
GH-98897: fix memory leak if math.dist raises exception (GH-98898)
Browse files Browse the repository at this point in the history
(cherry picked from commit ab57505)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
  • Loading branch information
miss-islington and kumaraditya303 authored Nov 1, 2022
1 parent d3d1738 commit 078ce68
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,11 @@ class T(tuple):
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 = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix memory leak in :func:`math.dist` when both points don't have the same dimension. Patch by Kumar Aditya.
6 changes: 3 additions & 3 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down

0 comments on commit 078ce68

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/commit/078ce6891c2d663babaf81b1e89f1fef82c007bc

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy