From 37d2ac42af2910c87da0fd635eef8f5c8347ab9a Mon Sep 17 00:00:00 2001 From: Schrodinger ZHU Yifan Date: Wed, 15 Dec 2021 22:01:34 +0800 Subject: [PATCH] use ::write and ::fsync in error path (#443) * use ::write and ::fsync in error path Signed-off-by: SchrodingerZhu * mark return value unused Signed-off-by: SchrodingerZhu * adjust fsync positions Signed-off-by: SchrodingerZhu --- src/pal/pal_posix.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/pal/pal_posix.h b/src/pal/pal_posix.h index 4c8dff2..6f12b04 100644 --- a/src/pal/pal_posix.h +++ b/src/pal/pal_posix.h @@ -151,10 +151,9 @@ namespace snmalloc constexpr int SIZE = 1024; void* buffer[SIZE]; auto nptrs = backtrace(buffer, SIZE); - fflush(stdout); backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO); - puts(""); - fflush(stdout); + UNUSED(write(STDOUT_FILENO, "\n", 1)); + UNUSED(fsync(STDOUT_FILENO)); #endif } @@ -163,7 +162,14 @@ namespace snmalloc */ [[noreturn]] static void error(const char* const str) noexcept { - puts(str); + /// by this part, the allocator is failed; so we cannot assume + /// subsequent allocation will work. + /// @attention: since the program is failing, we do not guarantee that + /// previous bytes in stdout will be flushed + UNUSED(write(STDOUT_FILENO, "\n", 1)); + UNUSED(write(STDOUT_FILENO, str, strlen(str))); + UNUSED(write(STDOUT_FILENO, "\n", 1)); + UNUSED(fsync(STDOUT_FILENO)); print_stack_trace(); abort(); }