Skip to content

Commit dc39d9a

Browse files
committed
Don't rely on dprintf() for faulthandler C stacks
1 parent 3b4b56f commit dc39d9a

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

Include/internal/pycore_fileutils.h

+2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ PyAPI_FUNC(Py_ssize_t) _Py_write_noraise(
132132
const void *buf,
133133
size_t count);
134134

135+
extern int _Py_fdprintf(int fd, const char *fmt, ...);
136+
135137
#ifdef HAVE_READLINK
136138
extern int _Py_wreadlink(
137139
const wchar_t *path,

Python/fileutils.c

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# include <windows.h>
1515
# include <winioctl.h> // FILE_DEVICE_* constants
1616
# include "pycore_fileutils_windows.h" // FILE_STAT_BASIC_INFORMATION
17+
# define fdopen _fdopen
1718
# if defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP)
1819
# define PATHCCH_ALLOW_LONG_PATHS 0x01
1920
# else
@@ -2059,6 +2060,21 @@ _Py_write_noraise(int fd, const void *buf, size_t count)
20592060
return _Py_write_impl(fd, buf, count, 0);
20602061
}
20612062

2063+
int
2064+
_Py_fdprintf(int fd, const char *fmt, ...)
2065+
{
2066+
FILE *handle = fdopen(fd, "a");
2067+
va_list vargs;
2068+
va_start(vargs, fmt);
2069+
int res = vfprintf(handle, fmt, vargs);
2070+
va_end(vargs);
2071+
if (res != 0) {
2072+
return -1;
2073+
}
2074+
2075+
return 0;
2076+
}
2077+
20622078
#ifdef HAVE_READLINK
20632079

20642080
/* Read value of symbolic link. Encode the path to the locale encoding, decode

Python/traceback.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
12101210
|| info[i].dli_fname == NULL
12111211
|| info[i].dli_fname[0] == '\0'
12121212
) {
1213-
dprintf(fd, " Binary file '<unknown>' [%p]\n", array[i]);
1213+
_Py_fdprintf(fd, " Binary file '<unknown>' [%p]\n", array[i]);
12141214
continue;
12151215
}
12161216

@@ -1222,9 +1222,9 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
12221222

12231223
if (info[i].dli_sname == NULL
12241224
&& info[i].dli_saddr == 0) {
1225-
dprintf(fd, " Binary file \"%s\" [%p]\n",
1226-
info[i].dli_fname,
1227-
array[i]);
1225+
_Py_fdprintf(fd, " Binary file \"%s\" [%p]\n",
1226+
info[i].dli_fname,
1227+
array[i]);
12281228
}
12291229
else {
12301230
char sign;
@@ -1238,10 +1238,10 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
12381238
offset = info[i].dli_saddr - array[i];
12391239
}
12401240
const char *symbol_name = info[i].dli_sname != NULL ? info[i].dli_sname : "";
1241-
dprintf(fd, " Binary file \"%s\", at %s%c%#tx [%p]\n",
1242-
info[i].dli_fname,
1243-
symbol_name,
1244-
sign, offset, array[i]);
1241+
_Py_fdprintf(fd, " Binary file \"%s\", at %s%c%#tx [%p]\n",
1242+
info[i].dli_fname,
1243+
symbol_name,
1244+
sign, offset, array[i]);
12451245
}
12461246
}
12471247
}

0 commit comments

Comments
 (0)