From f8aca1c316b116c2de11d42010fdda0ddb418750 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 4 Feb 2025 13:45:45 -0500 Subject: diag: Try to make diagnostics signal-safer Link: https://sourceware.org/bugzilla/show_bug.cgi?id=16060 --- src/diag.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/diag.c b/src/diag.c index 4909cf5..4f1c84c 100644 --- a/src/diag.c +++ b/src/diag.c @@ -14,13 +14,26 @@ #include #include #include +#include + +/** + * Print an error using dprintf() if possible, because it's more likely to be + * async-signal-safe in practice. + */ +#if BFS_HAS_DPRINTF +# define eprintf(...) dprintf(STDERR_FILENO, __VA_ARGS__) +# define veprintf(...) vdprintf(STDERR_FILENO, __VA_ARGS__) +#else +# define eprintf(...) fprintf(stderr, __VA_ARGS__) +# define veprintf(...) vfprintf(stderr, __VA_ARGS__) +#endif /** bfs_diagf() implementation. */ _printf(2, 0) static void bfs_vdiagf(const struct bfs_loc *loc, const char *format, va_list args) { - fprintf(stderr, "%s: %s@%s:%d: ", xgetprogname(), loc->func, loc->file, loc->line); - vfprintf(stderr, format, args); - fprintf(stderr, "\n"); + eprintf("%s: %s@%s:%d: ", xgetprogname(), loc->func, loc->file, loc->line); + veprintf(format, args); + eprintf("\n"); } void bfs_diagf(const struct bfs_loc *loc, const char *format, ...) { -- cgit v1.2.3