Skip to content

Commit 195a539

Browse files
committed
Protect against compilers without VLAs.
1 parent e9c3d7c commit 195a539

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Python/traceback.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525
# define CAN_C_BACKTRACE
2626
#endif
2727

28+
#if defined(__STDC_NO_VLA__) && (__STDC_NO_VLA__ == 1)
29+
/* Use alloca() for VLAs. */
30+
# define VLA(type, name, size) type *name = alloca(size)
31+
#elif !defined(__STDC_NO_VLA__) || (__STDC_NO_VLA__ == 0)
32+
/* Use actual C VLAs.*/
33+
# define VLA(type, name, size) type name[size]
34+
#elif defined(CAN_C_BACKTRACE)
35+
/* VLAs are not possible. Disable C stack trace functions. */
36+
# undef CAN_C_BACKTRACE
37+
#endif
38+
2839
#define OFF(x) offsetof(PyTracebackObject, x)
2940
#define PUTS(fd, str) (void)_Py_write_noraise(fd, str, strlen(str))
3041

@@ -1176,13 +1187,12 @@ _Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
11761187
void
11771188
_Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
11781189
{
1179-
Dl_info info[size] = {};
1180-
int status[size] = {};
1190+
VLA(Dl_info, info, size);
1191+
VLA(int, status, size);
11811192
/* Fill in the information we can get from dladdr() */
11821193
for (Py_ssize_t i = 0; i < size; ++i)
11831194
{
11841195
struct link_map *map;
1185-
assert(array[i] != NULL);
11861196
status[i] = dladdr1(array[i], &info[i], (void **)&map, RTLD_DL_LINKMAP);
11871197
if (status[i] != 0
11881198
&& info[i].dli_fname != NULL
@@ -1240,7 +1250,7 @@ _Py_DumpStack(int fd)
12401250
{
12411251
#define BACKTRACE_SIZE 32
12421252
PUTS(fd, "Current thread's C stack trace (most recent call first):\n");
1243-
void *callstack[BACKTRACE_SIZE] = {};
1253+
VLA(void *, callstack, BACKTRACE_SIZE);
12441254
int frames = backtrace(callstack, BACKTRACE_SIZE);
12451255
if (frames == 0) {
12461256
// Some systems won't return anything for the stack trace

0 commit comments

Comments
 (0)