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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,18 @@ def test_signatures(self):
str(inspect.signature(traceback.format_exception_only)),
'(exc, /, value=<implicit>, *, 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:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this test.

tb = traceback.format_exception(exc)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is unrelated to the fix, I suggest removing it. I tried to write a test using faulthandler.dump_c_stack() but I don't know how to create a long C stack. The Python recurse() reuses the same _PyEval_EvalFrameDefault frame for the 50 calls.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a reliable test we can write for this at the moment. The code path in question is only triggered on compilers that don't support VLAs, which is likely only MSVC on our buildbots. But, since Windows doesn't support backtrace(), C stack dumps are disabled on MSVC anyway.

assert any("RuntimeError" in line for line in tb)


class PurePythonExceptionFormattingMixin:
def get_exception(self, callable, slice_start=0, slice_end=-1):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too technical. We should say something like "Fix out-of-bounds access when invoking faulthandler on a CPython build compiled without support for VLAs."

2 changes: 1 addition & 1 deletion Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the (type *) change. In C, void * pointers can be implicitly casted.

#elif !defined(__STDC_NO_VLA__) || (__STDC_NO_VLA__ == 0)
/* Use actual C VLAs.*/
# define VLA(type, name, size) type name[size]
Expand Down
Loading