Skip to content

Commit c7fba65

Browse files
committed
Avoid dangling memory by passing double pointers as blobs
1 parent bb07f99 commit c7fba65

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/libYARP_robotinterface/tests/RobotinterfaceTest.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,15 @@ bool yarp::dev::RobotInterfaceTestMockDriver::open(yarp::os::Searchable& config)
8686
globalState.mockDriverWasOpened = true;
8787
globalState.mockDriverParamStringValue = config.check("theparam", yarp::os::Value("theparam_unset"), "The string param").asString();
8888
globalState.mockDriverParamListValue = *config.check("thelistparam", emptyList.get(0), "The list param").asList();
89-
globalState.mockDriverParamBlobValue = config.find("theblobparam").asBlob(); // don't use the `check` signature as it creates a temporary
89+
90+
// don't use the `check` signature as it creates a temporary
91+
if (const auto* ptr = config.find("theblobparam").asBlob(); ptr != nullptr)
92+
{
93+
// the caller test suite outlives `config`, thus we passed a pointer to the data instead of the data itself,
94+
// and we need to dereference it here
95+
globalState.mockDriverParamBlobValue = *reinterpret_cast<char* const*>(ptr);
96+
}
97+
9098
return true;
9199
}
92100

@@ -415,7 +423,7 @@ TEST_CASE("robotinterface::XMLReaderTest", "[yarp::robotinterface]")
415423
" </devices>\n"
416424
"</robot>\n";
417425

418-
int blobValue = 42;
426+
auto* blobValue = &XMLString;
419427

420428
yarp::robotinterface::XMLReader reader;
421429
yarp::os::Property config;
@@ -440,7 +448,7 @@ TEST_CASE("robotinterface::XMLReaderTest", "[yarp::robotinterface]")
440448
// Check that the device was opened and attach called
441449
CHECK(globalState.mockDriverWasOpened);
442450
CHECK(!globalState.mockDriverWasClosed);
443-
CHECK(*reinterpret_cast<const int *>(globalState.mockDriverParamBlobValue) == blobValue);
451+
CHECK(globalState.mockDriverParamBlobValue == blobValue);
444452

445453
// Stop the robot
446454
ok = result.robot.enterPhase(yarp::robotinterface::ActionPhaseInterrupt1);
@@ -451,7 +459,7 @@ TEST_CASE("robotinterface::XMLReaderTest", "[yarp::robotinterface]")
451459
// Check that the device was closed and detach called
452460
CHECK(globalState.mockDriverWasOpened);
453461
CHECK(globalState.mockDriverWasClosed);
454-
CHECK(*reinterpret_cast<const int *>(globalState.mockDriverParamBlobValue) == blobValue);
462+
CHECK(globalState.mockDriverParamBlobValue == blobValue);
455463
}
456464

457465
SECTION("Check valid robot file with portprefix passed via xml")

0 commit comments

Comments
 (0)