Skip to content

Commit 622585c

Browse files
authored
fix: regex usage in w3c extraction (#121)
Usage of std::regex_match was problematic for traceparent with trailing spaces. The overload use is `std::regex_match(str, str + std::char_traits<CharT>::length(str), m, e, flags)`, which resolves as the whole input instead of the trimmed input. Changes: - Fix std::regex_match usage. - Add unit test.
1 parent fad4e8d commit 622585c

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

src/datadog/w3c_propagation.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ namespace datadog {
1717
namespace tracing {
1818
namespace {
1919

20+
// Note that match group 0 is the entire match.
21+
constexpr StringView k_traceparent_pattern =
22+
"([0-9a-f]{2})" // hex version number (match group 1)
23+
"-"
24+
"([0-9a-f]{32})" // hex trace ID (match group 2)
25+
"-"
26+
"([0-9a-f]{16})" // hex parent span ID (match group 3)
27+
"-"
28+
"([0-9a-f]{2})" // hex "trace-flags" (match group 4)
29+
"($|-.*)"; // either the end, or a hyphen preceding further fields (match
30+
// group 5)
31+
2032
// Return a predicate that returns whether its `char` argument is any of the
2133
// following:
2234
//
@@ -45,32 +57,23 @@ Optional<std::string> extract_traceparent(ExtractedData& result,
4557

4658
const auto traceparent = trim(*maybe_traceparent);
4759

48-
// Note that leading and trailing whitespace was already removed above.
49-
// Note that match group 0 is the entire match.
50-
static const auto& pattern =
51-
"([0-9a-f]{2})" // hex version number (match group 1)
52-
"-"
53-
"([0-9a-f]{32})" // hex trace ID (match group 2)
54-
"-"
55-
"([0-9a-f]{16})" // hex parent span ID (match group 3)
56-
"-"
57-
"([0-9a-f]{2})" // hex "trace-flags" (match group 4)
58-
"($|-.*)"; // either the end, or a hyphen preceding further fields (match
59-
// group 5)
60-
static const std::regex regex{pattern};
60+
static const std::regex regex{k_traceparent_pattern.data()};
6161

6262
std::cmatch match;
63-
if (!std::regex_match(traceparent.data(), match, regex)) {
63+
if (!std::regex_match(traceparent.data(),
64+
traceparent.data() + traceparent.size(), match,
65+
regex)) {
6466
return "malformed_traceparent";
6567
}
6668

6769
assert(match.ready());
68-
assert(match.size() == 5 + 1);
70+
assert(match.size() == 6);
6971

70-
const auto to_string_view = [traceparent](const std::cmatch& match,
71-
const std::size_t index) {
72+
const auto to_string_view = [traceparent_beg = traceparent.data()](
73+
const std::cmatch& match,
74+
const std::size_t index) {
7275
assert(index < match.size());
73-
return StringView(traceparent.data() + match.position(index),
76+
return StringView(traceparent_beg + match.position(index),
7477
std::size_t(match.length(index)));
7578
};
7679

test/test_tracer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,13 @@ TEST_CASE("span extraction") {
622622
67667974448284343ULL, // expected_parent_id
623623
0}, // expected_sampling_priority
624624

625+
{__LINE__, "valid: leading and trailing spaces",
626+
" 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01 \t", // traceparent
627+
nullopt,
628+
*TraceID::parse_hex("4bf92f3577b34da6a3ce929d0e0e4736"), // expected_trace_id
629+
67667974448284343ULL, // expected_parent_id
630+
1}, // expected_sampling_priority
631+
625632
{__LINE__, "no traceparent",
626633
nullopt}, // traceparent
627634

0 commit comments

Comments
 (0)