From 35e6ec0186275ce644dfe6a0cd2d5eb86d50f5d0 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Tue, 5 Nov 2024 21:50:27 +0000 Subject: [PATCH] Prevent repeated conversions of hex versions to binary --- .../trace/propagation/http_trace_context.h | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/api/include/opentelemetry/trace/propagation/http_trace_context.h b/api/include/opentelemetry/trace/propagation/http_trace_context.h index 608f93e9dc..e6a436d110 100644 --- a/api/include/opentelemetry/trace/propagation/http_trace_context.h +++ b/api/include/opentelemetry/trace/propagation/http_trace_context.h @@ -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, @@ -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(); }