From c9734484ca774afad49e361f72cad60d937acf1b Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 13 Apr 2013 17:44:44 +0100 Subject: Issue #17715: Add missing NULL Check to PyNumber_Long. --- Lib/test/test_int.py | 6 ++++++ Misc/NEWS | 3 +++ Objects/abstract.c | 2 ++ 3 files changed, 11 insertions(+) diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index c35a42f..703c233 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -313,6 +313,12 @@ class IntTestCases(unittest.TestCase): return 42 self.assertEqual(int(JustTrunc()), 42) + class ExceptionalTrunc(base): + def __trunc__(self): + 1 / 0 + with self.assertRaises(ZeroDivisionError): + int(ExceptionalTrunc()) + for trunc_result_base in (object, Classic): class Integral(trunc_result_base): def __int__(self): diff --git a/Misc/NEWS b/Misc/NEWS index e04324f..4fd149a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,9 @@ What's New in Python 3.3.2? Core and Builtins ----------------- +- Issue #17715: Fix segmentation fault from raising an exception in a __trunc__ + method. + - Issue #16447: Fixed potential segmentation fault when setting __name__ on a class. diff --git a/Objects/abstract.c b/Objects/abstract.c index a2737dd..7c24724 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1293,6 +1293,8 @@ PyNumber_Long(PyObject *o) PyObject *truncated = PyEval_CallObject(trunc_func, NULL); PyObject *int_instance; Py_DECREF(trunc_func); + if (truncated == NULL) + return NULL; /* __trunc__ is specified to return an Integral type, but int() needs to return a int. */ int_instance = convert_integral_to_int(truncated, -- cgit v0.12