From 69144942e3b76f71ef35f7ff3a5c7a297c268410 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 23 May 2023 16:31:24 -0700 Subject: [PATCH] Fix fortify Close #7009, fix #7003 --- native/src/base/compat/fortify.hpp | 2 +- native/src/base/logging.cpp | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/native/src/base/compat/fortify.hpp b/native/src/base/compat/fortify.hpp index d657fb393..7ff879bd0 100644 --- a/native/src/base/compat/fortify.hpp +++ b/native/src/base/compat/fortify.hpp @@ -140,5 +140,5 @@ int __openat_2(int fd, const char* pathname, int flags) { int __vsnprintf_chk(char* dst, size_t supplied_size, int /*flags*/, size_t dst_len_from_compiler, const char* format, va_list va) { __check_buffer_access("vsnprintf", "write into", supplied_size, dst_len_from_compiler); - return vsnprintf(dst, supplied_size, format, va); + return __call_bypassing_fortify(vsnprintf)(dst, supplied_size, format, va); } diff --git a/native/src/base/logging.cpp b/native/src/base/logging.cpp index aab721a5f..a1a191dac 100644 --- a/native/src/base/logging.cpp +++ b/native/src/base/logging.cpp @@ -8,11 +8,15 @@ using namespace std; +#undef vsnprintf static int fmt_and_log_with_rs(LogLevel level, const char *fmt, va_list ap) { - char buf[4096]; - int ret = vssprintf(buf, sizeof(buf), fmt, ap); - log_with_rs(level, rust::Slice(reinterpret_cast(buf), ret)); - return ret; + constexpr int sz = 4096; + char buf[sz]; + buf[0] = '\0'; + // Fortify logs when a fatal error occurs. Do not run through fortify again + int len = std::min(__call_bypassing_fortify(vsnprintf)(buf, sz, fmt, ap), sz - 1); + log_with_rs(level, rust::Slice(reinterpret_cast(buf), len)); + return len; } // Used to override external C library logging