This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.0 by this push:
     new 19fa9318145 branch-4.0: [Fix](Exception) Fix potential use-after-free 
because `Exception::to_string` is not thread safe #59558 (#59598)
19fa9318145 is described below

commit 19fa9318145205c99163ceed68587029ae4cfad9
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Jan 7 18:07:14 2026 +0800

    branch-4.0: [Fix](Exception) Fix potential use-after-free because 
`Exception::to_string` is not thread safe #59558 (#59598)
    
    Cherry-picked from #59558
    
    Co-authored-by: bobhan1 <[email protected]>
---
 be/src/common/exception.cpp |  8 ++++++++
 be/src/common/exception.h   | 20 +++-----------------
 2 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/be/src/common/exception.cpp b/be/src/common/exception.cpp
index 371a369b7be..297b07222de 100644
--- a/be/src/common/exception.cpp
+++ b/be/src/common/exception.cpp
@@ -48,6 +48,14 @@ Exception::Exception(int code, const std::string_view& msg, 
bool from_status) {
     // std::cout << "Exception: " << code << ", " << msg << ", " << 
get_stack_trace(0, "DISABLED")
     //           << std::endl;
 #endif
+
+    fmt::memory_buffer buf;
+    fmt::format_to(buf, "[E{}] {}", _code, _err_msg->_msg);
+    if (!_err_msg->_stack.empty()) {
+        fmt::format_to(buf, "\n{}", _err_msg->_stack);
+    }
+    _cache_string = fmt::to_string(buf);
+
     if (config::exit_on_exception) {
         LOG(FATAL) << "[ExitOnException] error code: " << code << ", message: 
" << msg;
     }
diff --git a/be/src/common/exception.h b/be/src/common/exception.h
index c24839eb541..bcf4aea42ee 100644
--- a/be/src/common/exception.h
+++ b/be/src/common/exception.h
@@ -46,9 +46,9 @@ public:
     int code() const { return _code; }
     std::string message() const { return _err_msg ? _err_msg->_msg : ""; }
 
-    const std::string& to_string() const;
+    const std::string& to_string() const { return _cache_string; }
 
-    const char* what() const noexcept override { return to_string().c_str(); }
+    const char* what() const noexcept override { return _cache_string.c_str(); 
}
 
     Status to_status() const { return {code(), _err_msg->_msg, 
_err_msg->_stack}; }
 
@@ -61,22 +61,8 @@ private:
         std::string _stack;
     };
     std::unique_ptr<ErrMsg> _err_msg;
-    mutable std::string _cache_string;
+    std::string _cache_string {};
 };
-
-inline const std::string& Exception::to_string() const {
-    if (!_cache_string.empty()) {
-        return _cache_string;
-    }
-    fmt::memory_buffer buf;
-    fmt::format_to(buf, "[E{}] {}", _code, _err_msg ? _err_msg->_msg : "");
-    if (_err_msg && !_err_msg->_stack.empty()) {
-        fmt::format_to(buf, "\n{}", _err_msg->_stack);
-    }
-    _cache_string = fmt::to_string(buf);
-    return _cache_string;
-}
-
 } // namespace doris
 
 #define RETURN_IF_CATCH_EXCEPTION(stmt)                                        
                  \


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to