diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 2fbc2a041269f4..c3893f204540fe 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -625,6 +625,18 @@ def test_signatures(self): str(inspect.signature(traceback.format_exception_only)), '(exc, /, value=, *, show_group=False, **kwargs)') + def test_traceback_deep_recursion_alloca(self): + + def recurse(n): + if n == 0: + raise RuntimeError("boom") + return recurse(n - 1) + try: + recurse(50) + except RuntimeError as exc: + tb = traceback.format_exception(exc) + assert any("RuntimeError" in line for line in tb) + class PurePythonExceptionFormattingMixin: def get_exception(self, callable, slice_start=0, slice_end=-1): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-11-19-09-47.gh-issue-145792.X5KUhc.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-11-19-09-47.gh-issue-145792.X5KUhc.rst new file mode 100644 index 00000000000000..202c4d2df4431e --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-11-19-09-47.gh-issue-145792.X5KUhc.rst @@ -0,0 +1,2 @@ +Fix incorrect memory allocation in the VLA fallback macro in traceback.c +when using alloca(), preventing potential out-of-bounds access. diff --git a/Python/traceback.c b/Python/traceback.c index 74360a1c73c271..9e5578670a214f 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -41,7 +41,7 @@ #if defined(__STDC_NO_VLA__) && (__STDC_NO_VLA__ == 1) /* Use alloca() for VLAs. */ -# define VLA(type, name, size) type *name = alloca(size) +# define VLA(type, name, size) type *name = (type *)alloca(sizeof(type) * (size)) #elif !defined(__STDC_NO_VLA__) || (__STDC_NO_VLA__ == 0) /* Use actual C VLAs.*/ # define VLA(type, name, size) type name[size]