diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 06b95a6248..ed6a87eed0 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -39,8 +39,8 @@ class Context template Context SetValues(T &values) noexcept { - Context context = Context(values); - nostd::shared_ptr &last = context.head_; + Context context = Context(values); + nostd::shared_ptr last = context.head_; while (last->next_ != nullptr) { last = last->next_; @@ -110,7 +110,7 @@ class Context // Builds a data list off of a key and value iterable and returns the head template - DataList(const T &keys_and_vals) : key_{nullptr} + DataList(const T &keys_and_vals) : key_{nullptr}, next_(nostd::shared_ptr{nullptr}) { bool first = true; auto *node = this; diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index f9a6dfbfd4..66b7f34c45 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -78,14 +78,19 @@ TEST(ContextTest, ContextInheritance) { using M = std::map; - M m1 = {{"test_key", (int64_t)123}, {"foo_key", (int64_t)456}}; - M m2 = {{"other_key", (int64_t)789}}; + M m1 = {{"test_key", (int64_t)123}, {"foo_key", (int64_t)321}}; + M m2 = {{"other_key", (int64_t)789}, {"another_key", (int64_t)987}}; context::Context test_context = context::Context(m1); context::Context foo_context = test_context.SetValues(m2); EXPECT_EQ(nostd::get(foo_context.GetValue("test_key")), 123); - EXPECT_EQ(nostd::get(foo_context.GetValue("foo_key")), 456); + EXPECT_EQ(nostd::get(foo_context.GetValue("foo_key")), 321); + EXPECT_EQ(nostd::get(foo_context.GetValue("other_key")), 789); + EXPECT_EQ(nostd::get(foo_context.GetValue("another_key")), 987); + + EXPECT_EQ(nostd::get(test_context.GetValue("other_key")), 0); + EXPECT_EQ(nostd::get(test_context.GetValue("another_key")), 0); } // Tests that copying a context copies the key value pairs as expected.