From 1d45242307f321acea891172acea290c71096e48 Mon Sep 17 00:00:00 2001 From: Kaijie Chen Date: Mon, 23 Jun 2025 17:50:53 +0800 Subject: [PATCH] [fix](load) resolve UBSan error when printing unique IDs (#52042) ### What problem does this PR solve? This commit fixes an Undefined Behavior Sanitizer error triggered when printing `PUniqueId` directly to an ostream. Among the three types of unique IDs, `UniqueId`, `PUniqueId` (protobuf), and `TUniqueId` (thrift), only `UniqueId` has an overloaded `operator<<()`. Attempting to stream the other two types leads to undefined behavior, making the current approach error-prone. Introduces a unified `print_id()` function for safely printing all unique ID types (`UniqueId`, `PUniqueId`, `TUniqueId`). It is recommended to use `print_id()` instead of streaming directly. --- be/src/runtime/load_stream.cpp | 2 +- be/src/util/uid_util.cpp | 4 ++++ be/src/util/uid_util.h | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/be/src/runtime/load_stream.cpp b/be/src/runtime/load_stream.cpp index 0bd045d46c16d3..eb28daaa86fd20 100644 --- a/be/src/runtime/load_stream.cpp +++ b/be/src/runtime/load_stream.cpp @@ -72,7 +72,7 @@ TabletStream::TabletStream(PUniqueId load_id, int64_t id, int64_t txn_id, } inline std::ostream& operator<<(std::ostream& ostr, const TabletStream& tablet_stream) { - ostr << "load_id=" << tablet_stream._load_id << ", txn_id=" << tablet_stream._txn_id + ostr << "load_id=" << print_id(tablet_stream._load_id) << ", txn_id=" << tablet_stream._txn_id << ", tablet_id=" << tablet_stream._id << ", status=" << tablet_stream._status.status(); return ostr; } diff --git a/be/src/util/uid_util.cpp b/be/src/util/uid_util.cpp index 0f93f437ab6cc4..fb767f09bd7e5b 100644 --- a/be/src/util/uid_util.cpp +++ b/be/src/util/uid_util.cpp @@ -44,6 +44,10 @@ std::ostream& operator<<(std::ostream& os, const UniqueId& uid) { return os; } +std::string print_id(const UniqueId& id) { + return id.to_string(); +} + std::string print_id(const TUniqueId& id) { return fmt::format(FMT_COMPILE("{:x}-{:x}"), static_cast(id.hi), static_cast(id.lo)); diff --git a/be/src/util/uid_util.h b/be/src/util/uid_util.h index 5c0b5fb72fb3cd..7f300c561b04ef 100644 --- a/be/src/util/uid_util.h +++ b/be/src/util/uid_util.h @@ -168,6 +168,7 @@ inline TUniqueId generate_uuid() { std::ostream& operator<<(std::ostream& os, const UniqueId& uid); +std::string print_id(const UniqueId& id); std::string print_id(const TUniqueId& id); std::string print_id(const PUniqueId& id);