From 9e45671c8acfcf0c7da9807adeef689be7fe70bf Mon Sep 17 00:00:00 2001 From: Hyukjin Kwon Date: Mon, 29 Dec 2025 18:38:17 +0900 Subject: [PATCH] [C++] Fix ToStringWithoutContextLines to check for :\d+ pattern before removing lines --- cpp/src/arrow/status.cc | 12 ++++++++++-- cpp/src/arrow/status_test.cc | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/status.cc b/cpp/src/arrow/status.cc index 55ce3fb78d2..b97a9e3de08 100644 --- a/cpp/src/arrow/status.cc +++ b/cpp/src/arrow/status.cc @@ -13,6 +13,7 @@ #include "arrow/status.h" #include +#include #include #include #ifdef ARROW_EXTRA_ERROR_CONTEXT @@ -131,8 +132,15 @@ std::string Status::ToStringWithoutContextLines() const { if (last_new_line_position == std::string::npos) { break; } - // TODO: We may want to check /:\d+ / - if (message.find(":", last_new_line_position) == std::string::npos) { + // Check for the pattern ":\d+" (colon followed by digits) to identify + // context lines in the format "filename:line expr" + auto colon_position = message.find(":", last_new_line_position); + if (colon_position == std::string::npos) { + break; + } + // Verify that the colon is followed by at least one digit + if (colon_position + 1 >= message.size() || + !std::isdigit(static_cast(message[colon_position + 1]))) { break; } message = message.substr(0, last_new_line_position); diff --git a/cpp/src/arrow/status_test.cc b/cpp/src/arrow/status_test.cc index 39a52bd2bad..b0af683a55c 100644 --- a/cpp/src/arrow/status_test.cc +++ b/cpp/src/arrow/status_test.cc @@ -342,4 +342,23 @@ TEST(StatusTest, ReturnIfNotOk) { ASSERT_EQ(StripContext(st.message()), "StatusLike: 43"); } +#ifdef ARROW_EXTRA_ERROR_CONTEXT +TEST(StatusTest, ToStringWithoutContextLines) { + Status status = Status::IOError("base error"); + status.AddContextLine("file1.cc", 42, "expr"); + status.AddContextLine("file2.cc", 100, "expr"); + + ASSERT_EQ(status.ToStringWithoutContextLines(), "IOError: base error"); + + Status status2(StatusCode::Invalid, + "Error message\nThis line has: a colon but no digits"); + status2.AddContextLine("file.cc", 20, "expr"); + + std::string result = status2.ToStringWithoutContextLines(); + ASSERT_EQ(result.find("file.cc:20"), std::string::npos); + ASSERT_NE(result.find("This line has: a colon but no digits"), std::string::npos); + ASSERT_NE(result.find("Error message"), std::string::npos); +} +#endif + } // namespace arrow