Skip to content

Commit 5f743f4

Browse files
authored
fix: prevent crash due to invalid headers (#132)
* fix: prevent crash due to invalid headers When the tracer encounters an invalid propagation header value, logging the list of headers and their values can lead to a crash.
1 parent bb5358e commit 5f743f4

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

src/datadog/extraction_util.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <unordered_map>
88

99
#include "extracted_data.h"
10-
#include "json.hpp"
1110
#include "logger.h"
1211
#include "parse_util.h"
1312
#include "string_util.h"
@@ -223,15 +222,15 @@ std::string extraction_error_prefix(
223222
std::ostringstream stream;
224223
stream << "While extracting trace context";
225224
if (style) {
226-
stream << " in the " << to_json(*style) << " propagation style";
225+
stream << " in the " << to_string_view(*style) << " propagation style";
227226
}
228-
auto it = headers_examined.begin();
229-
if (it != headers_examined.end()) {
230-
stream << " from the following headers: [";
231-
stream << nlohmann::json(it->first + ": " + it->second);
227+
228+
if (!headers_examined.empty()) {
229+
auto it = headers_examined.begin();
230+
stream << " from the following headers: [" << it->first << ": "
231+
<< it->second;
232232
for (++it; it != headers_examined.end(); ++it) {
233-
stream << ", ";
234-
stream << nlohmann::json(it->first + ": " + it->second);
233+
stream << ", " << it->first << ": " << it->second;
235234
}
236235
stream << "]";
237236
}

src/datadog/propagation_style.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace datadog {
99
namespace tracing {
1010

11-
nlohmann::json to_json(PropagationStyle style) {
11+
StringView to_string_view(PropagationStyle style) {
1212
// Note: Make sure that these strings are consistent (modulo case) with
1313
// `parse_propagation_styles` in `tracer_config.cpp`.
1414
switch (style) {
@@ -24,6 +24,8 @@ nlohmann::json to_json(PropagationStyle style) {
2424
}
2525
}
2626

27+
nlohmann::json to_json(PropagationStyle style) { return to_string_view(style); }
28+
2729
nlohmann::json to_json(const std::vector<PropagationStyle>& styles) {
2830
std::vector<nlohmann::json> styles_json;
2931
for (const auto style : styles) {

src/datadog/propagation_style.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ enum class PropagationStyle {
2727
NONE,
2828
};
2929

30+
StringView to_string_view(PropagationStyle style);
3031
nlohmann::json to_json(PropagationStyle style);
3132
nlohmann::json to_json(const std::vector<PropagationStyle>& styles);
3233

test/test_tracer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ TEST_CASE("span extraction") {
383383
{PropagationStyle::B3},
384384
{{"x-b3-traceid", "0"}, {"x-b3-spanid", "123"}, {"x-b3-sampled", "0"}},
385385
Error::ZERO_TRACE_ID},
386+
{__LINE__,
387+
"character encoding",
388+
{PropagationStyle::DATADOG},
389+
{{"x-datadog-trace-id", "\xFD\xD0\x6C\x6C\x6F\x2C\x20\xC3\xB1\x21"},
390+
{"x-datadog-parent-id", "1234"},
391+
{"x-datadog-sampling-priority", "0"}},
392+
Error::INVALID_INTEGER},
386393
}));
387394

388395
CAPTURE(test_case.line);

0 commit comments

Comments
 (0)