From c7fba65ca7ea255f69c0926e575b3270afd7f238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20=C5=81ukawski?= Date: Mon, 8 Jan 2024 17:29:54 +0100 Subject: [PATCH] Avoid dangling memory by passing double pointers as blobs --- .../tests/RobotinterfaceTest.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/libYARP_robotinterface/tests/RobotinterfaceTest.cpp b/src/libYARP_robotinterface/tests/RobotinterfaceTest.cpp index 7dfb479337..f33a26af20 100644 --- a/src/libYARP_robotinterface/tests/RobotinterfaceTest.cpp +++ b/src/libYARP_robotinterface/tests/RobotinterfaceTest.cpp @@ -86,7 +86,15 @@ bool yarp::dev::RobotInterfaceTestMockDriver::open(yarp::os::Searchable& config) globalState.mockDriverWasOpened = true; globalState.mockDriverParamStringValue = config.check("theparam", yarp::os::Value("theparam_unset"), "The string param").asString(); globalState.mockDriverParamListValue = *config.check("thelistparam", emptyList.get(0), "The list param").asList(); - globalState.mockDriverParamBlobValue = config.find("theblobparam").asBlob(); // don't use the `check` signature as it creates a temporary + + // don't use the `check` signature as it creates a temporary + if (const auto* ptr = config.find("theblobparam").asBlob(); ptr != nullptr) + { + // the caller test suite outlives `config`, thus we passed a pointer to the data instead of the data itself, + // and we need to dereference it here + globalState.mockDriverParamBlobValue = *reinterpret_cast(ptr); + } + return true; } @@ -415,7 +423,7 @@ TEST_CASE("robotinterface::XMLReaderTest", "[yarp::robotinterface]") " \n" "\n"; - int blobValue = 42; + auto* blobValue = &XMLString; yarp::robotinterface::XMLReader reader; yarp::os::Property config; @@ -440,7 +448,7 @@ TEST_CASE("robotinterface::XMLReaderTest", "[yarp::robotinterface]") // Check that the device was opened and attach called CHECK(globalState.mockDriverWasOpened); CHECK(!globalState.mockDriverWasClosed); - CHECK(*reinterpret_cast(globalState.mockDriverParamBlobValue) == blobValue); + CHECK(globalState.mockDriverParamBlobValue == blobValue); // Stop the robot ok = result.robot.enterPhase(yarp::robotinterface::ActionPhaseInterrupt1); @@ -451,7 +459,7 @@ TEST_CASE("robotinterface::XMLReaderTest", "[yarp::robotinterface]") // Check that the device was closed and detach called CHECK(globalState.mockDriverWasOpened); CHECK(globalState.mockDriverWasClosed); - CHECK(*reinterpret_cast(globalState.mockDriverParamBlobValue) == blobValue); + CHECK(globalState.mockDriverParamBlobValue == blobValue); } SECTION("Check valid robot file with portprefix passed via xml")