Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 078ce68

Browse files
GH-98897: fix memory leak if math.dist raises exception (GH-98898)
(cherry picked from commit ab57505) Co-authored-by: Kumar Aditya <[email protected]>
1 parent d3d1738 commit 078ce68

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

Lib/test/test_math.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,11 @@ class T(tuple):
979979
self.assertEqual(math.dist(p, q), 5*scale)
980980
self.assertEqual(math.dist(q, p), 5*scale)
981981

982+
def test_math_dist_leak(self):
983+
# gh-98897: Check for error handling does not leak memory
984+
with self.assertRaises(ValueError):
985+
math.dist([1, 2], [3, 4, 5])
986+
982987
def testIsqrt(self):
983988
# Test a variety of inputs, large and small.
984989
test_values = (
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix memory leak in :func:`math.dist` when both points don't have the same dimension. Patch by Kumar Aditya.

Modules/mathmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2658,13 +2658,13 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
26582658
if (m != n) {
26592659
PyErr_SetString(PyExc_ValueError,
26602660
"both points must have the same number of dimensions");
2661-
return NULL;
2662-
2661+
goto error_exit;
26632662
}
26642663
if (n > NUM_STACK_ELEMS) {
26652664
diffs = (double *) PyObject_Malloc(n * sizeof(double));
26662665
if (diffs == NULL) {
2667-
return PyErr_NoMemory();
2666+
PyErr_NoMemory();
2667+
goto error_exit;
26682668
}
26692669
}
26702670
for (i=0 ; i<n ; i++) {

0 commit comments

Comments
 (0)