Skip to content

Commit 4829c22

Browse files
committed
[C++] Fix ToStringWithoutContextLines to check for :\d+ pattern before removing lines
1 parent 727106f commit 4829c22

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

cpp/src/arrow/status.cc

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "arrow/status.h"
1414

1515
#include <cassert>
16+
#include <cctype>
1617
#include <cstdlib>
1718
#include <iostream>
1819
#ifdef ARROW_EXTRA_ERROR_CONTEXT
@@ -131,8 +132,25 @@ std::string Status::ToStringWithoutContextLines() const {
131132
if (last_new_line_position == std::string::npos) {
132133
break;
133134
}
134-
// TODO: We may want to check /:\d+ /
135-
if (message.find(":", last_new_line_position) == std::string::npos) {
135+
// Check for the pattern ":\d+ " (colon followed by one or more digits and a space)
136+
// to identify context lines in the format "filename:line expr"
137+
auto colon_position = message.find(":", last_new_line_position);
138+
if (colon_position == std::string::npos) {
139+
break;
140+
}
141+
// Verify that the colon is followed by one or more digits and then a space
142+
size_t pos = colon_position + 1;
143+
if (pos >= message.size() ||
144+
!std::isdigit(static_cast<unsigned char>(message[pos]))) {
145+
break;
146+
}
147+
// Skip all digits
148+
while (pos < message.size() &&
149+
std::isdigit(static_cast<unsigned char>(message[pos]))) {
150+
pos++;
151+
}
152+
// Check if followed by a space
153+
if (pos >= message.size() || message[pos] != ' ') {
136154
break;
137155
}
138156
message = message.substr(0, last_new_line_position);

cpp/src/arrow/status_test.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,21 @@ TEST(StatusTest, ReturnIfNotOk) {
342342
ASSERT_EQ(StripContext(st.message()), "StatusLike: 43");
343343
}
344344

345+
#ifdef ARROW_EXTRA_ERROR_CONTEXT
346+
TEST(StatusTest, ToStringWithoutContextLines) {
347+
Status status = Status::IOError("base error");
348+
status.AddContextLine("file1.cc", 42, "expr");
349+
status.AddContextLine("file2.cc", 100, "expr");
350+
351+
ASSERT_EQ(status.ToStringWithoutContextLines(), "IOError: base error");
352+
353+
Status status2(StatusCode::Invalid,
354+
"Error message\nThis line has: a colon but no digits");
355+
status2.AddContextLine("file.cc", 20, "expr");
356+
357+
ASSERT_EQ(status2.ToStringWithoutContextLines(),
358+
"Invalid: Error message\nThis line has: a colon but no digits");
359+
}
360+
#endif
361+
345362
} // namespace arrow

0 commit comments

Comments
 (0)