Skip to content

Commit d3c8278

Browse files
pypingouclaude
authored andcommitted
Fix dangling reference warnings in JSON parser tests
GCC 14's -Werror=dangling-reference detects references to values inside temporary Result objects. The temporaries are destroyed at statement end, leaving dangling references. Solution: store intermediate Result objects to extend their lifetime before taking references to their contents. Applied to three test cases in parsers_test_suite.h: - CanParseObjectInObject - CanParseListInObject - CanParseObjectInObjectAndIterateOverKeys Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> Signed-off-by: Leonardo Rossetti <lrossett@redhat.com>
1 parent 6ae7601 commit d3c8278

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

score/json/internal/parser/parsers_test_suite.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ TYPED_TEST_P(ParserTest, CanParseObjectString)
114114
auto root = TypeParam::FromBuffer(buffer_simple_json);
115115

116116
// When reading a key of an object that is interpreted as std::string
117-
auto& value = GetValueOfObject<std::string>(root.value(), "color");
117+
auto value = GetValueOfObject<std::string>(root.value(), "color");
118118

119119
// Then the correct value is returned
120120
EXPECT_EQ(value, "gold");
@@ -133,7 +133,7 @@ TYPED_TEST_P(ParserTest, CanParseObjectNull)
133133
auto root = TypeParam::FromBuffer(buffer_simple_json);
134134

135135
// When reading a key of an object that is interpreted as Null
136-
auto& value = GetValueOfObject<Null>(root.value(), "null");
136+
auto value = GetValueOfObject<Null>(root.value(), "null");
137137

138138
// Then the correct value is returned
139139
EXPECT_EQ(value, Null{});
@@ -195,7 +195,11 @@ TYPED_TEST_P(ParserTest, CanParseObjectInObject)
195195
auto root = TypeParam::FromBuffer(buffer_simple_json);
196196

197197
// When reading a key of an object that is interpreted as number
198-
auto& value = GetValueOfObject<Object>(root.value(), "object");
198+
auto root_object_result = root.value().template As<Object>();
199+
auto& root_object = root_object_result.value().get();
200+
const auto& nested_any = root_object.at("object");
201+
auto nested_object_result = nested_any.template As<Object>();
202+
const auto& value = nested_object_result.value().get();
199203

200204
// Then the correct value is returned
201205
EXPECT_EQ(value.at("a").template As<std::string>().value().get(), "b");
@@ -214,7 +218,11 @@ TYPED_TEST_P(ParserTest, CanParseListInObject)
214218
auto root = TypeParam::FromBuffer(buffer_simple_json);
215219

216220
// When reading a key of an object that is interpreted as number
217-
auto& value = GetValueOfObject<List>(*root, "list");
221+
auto root_object_result = root->template As<Object>();
222+
auto& root_object = root_object_result.value().get();
223+
const auto& list_any = root_object.at("list");
224+
auto list_result = list_any.template As<List>();
225+
const auto& value = list_result.value().get();
218226

219227
// Then the correct value is returned
220228
EXPECT_EQ(value[0].template As<std::string>().value().get(), "first");
@@ -259,9 +267,13 @@ TYPED_TEST_P(ParserTest, CanParseObjectInObjectAndIterateOverKeys)
259267
auto root = TypeParam::FromBuffer(buffer);
260268

261269
// When iterating over the unknown keys
262-
const auto& storage_list = root.value().template As<Object>().value().get()["storage_list"];
270+
auto root_object_result = root.value().template As<Object>();
271+
auto& root_object = root_object_result.value().get();
272+
const auto& storage_list_any = root_object["storage_list"];
273+
auto storage_obj_result = storage_list_any.template As<Object>();
274+
auto& storage_obj = storage_obj_result.value().get();
263275
std::vector<std::string> collected_paths{};
264-
for (const auto& element : storage_list.template As<Object>().value().get())
276+
for (const auto& element : storage_obj)
265277
{
266278
collected_paths.push_back(
267279
element.second.template As<Object>().value().get().at("path").template As<std::string>().value().get());

0 commit comments

Comments
 (0)