Skip to content

Commit

Permalink
Prevent repeated conversions of hex versions to binary
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Nov 5, 2024
1 parent ebaaaf3 commit 35e6ec0
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions api/include/opentelemetry/trace/propagation/http_trace_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,14 @@ class HttpTraceContext : public context::propagation::TextMapPropagator
static constexpr uint8_t kInvalidVersion = 0xFF;
static constexpr uint8_t kDefaultAssumedVersion = 0x00;

static bool IsValidVersion(nostd::string_view version_hex)
static bool IsValidVersion(uint8_t version_binary)
{
uint8_t version;
detail::HexToBinary(version_hex, &version, sizeof(version));
return version != kInvalidVersion;
return version_binary != kInvalidVersion;
}

static bool IsHigherVersion(nostd::string_view version_hex)
static bool IsHigherVersion(uint8_t version_binary)
{
uint8_t version;
detail::HexToBinary(version_hex, &version, sizeof(version));
return version > kDefaultAssumedVersion;
return version_binary > kDefaultAssumedVersion;
}

static void InjectImpl(context::propagation::TextMapCarrier &carrier,
Expand Down Expand Up @@ -153,17 +149,20 @@ class HttpTraceContext : public context::propagation::TextMapPropagator
return SpanContext::GetInvalid();
}

if (!IsValidVersion(version_hex))
// hex is valid, convert it to binary
uint8_t version_binary;
detail::HexToBinary(version_hex, &version_binary, sizeof(version_binary));
if (!IsValidVersion(version_binary))
{
return SpanContext::GetInvalid();
}

if (IsHigherVersion(version_hex) && trace_parent.size() < kTraceParentSize)
if (IsHigherVersion(version_binary) && trace_parent.size() < kTraceParentSize)
{
return SpanContext::GetInvalid();
}

if (!IsHigherVersion(version_hex) && trace_parent.size() != kTraceParentSize)
if (!IsHigherVersion(version_binary) && trace_parent.size() != kTraceParentSize)
{
return SpanContext::GetInvalid();
}
Expand Down

0 comments on commit 35e6ec0

Please sign in to comment.