From 95f863e3a1b3839369a77d377ddcd01597289013 Mon Sep 17 00:00:00 2001 From: Alejandro Hernandez Cordero Date: Fri, 13 Nov 2026 04:20:45 +0100 Subject: [PATCH] Fix infinite loop and library name lookup in plugin XML parsing Signed-off-by: Alejandro Hernandez Cordero --- .../include/pluginlib/class_loader_imp.hpp | 19 ++--- pluginlib/test/utest.cpp | 69 +++++++++++++++++++ 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/pluginlib/include/pluginlib/class_loader_imp.hpp b/pluginlib/include/pluginlib/class_loader_imp.hpp index 51c1209..74cb6bf 100644 --- a/pluginlib/include/pluginlib/class_loader_imp.hpp +++ b/pluginlib/include/pluginlib/class_loader_imp.hpp @@ -660,8 +660,11 @@ void ClassLoader::processSingleXMLPluginFile( config = config->FirstChildElement("library"); } - tinyxml2::XMLElement * library = config; - while (library != nullptr) { + // Advance to the next in the loop statement so that the error + // paths below, which `continue`, cannot loop forever on the same element. + for (tinyxml2::XMLElement * library = config; library != nullptr; + library = library->NextSiblingElement("library")) + { const char * path = library->Attribute("path"); if (nullptr == path) { RCUTILS_LOG_ERROR_NAMED("pluginlib.ClassLoader", @@ -737,7 +740,6 @@ void ClassLoader::processSingleXMLPluginFile( // step to next class_element class_element = class_element->NextSiblingElement("class"); } - library = library->NextSiblingElement("library"); } } @@ -774,12 +776,11 @@ template std::string ClassLoader::stripAllButFileFromPath(const std::string & path) /***************************************************************************/ { - size_t c = path.find_last_of(getPathSeparator()); - if (std::string::npos == c) { - return path; - } else { - return path.substr(c, path.size()); - } + // Return only the file name, without a leading separator. Keeping the + // separator produces names such as "lib/foo.so" (or "/foo.dll" on Windows) + // once the platform prefix and suffix are added, which never match a file in + // the search directories. + return std::filesystem::path(path).filename().string(); } template diff --git a/pluginlib/test/utest.cpp b/pluginlib/test/utest.cpp index 772a858..f106055 100644 --- a/pluginlib/test/utest.cpp +++ b/pluginlib/test/utest.cpp @@ -28,7 +28,10 @@ #include +#include +#include #include +#include #include // NOLINT #include "rcutils/logging_macros.h" // NOLINT @@ -191,6 +194,72 @@ TEST(PluginlibTest, Splitting) { EXPECT_EQ(pl.getName(""), ""); } +// Writes a plugin description XML next to a package.xml for 'test_pluginlib' +// in a fresh temporary directory, so that libraries are looked up in the +// mock install prefix of 'test_pluginlib'. Returns the path to the plugin XML. +static std::string writePluginXml(const std::string & test_name, const std::string & content) +{ + const std::filesystem::path dir = + std::filesystem::temp_directory_path() / ("pluginlib_utest_" + test_name); + std::filesystem::remove_all(dir); + std::filesystem::create_directories(dir); + std::ofstream(dir / "package.xml") << + "test_pluginlib\n"; + const std::filesystem::path xml_path = dir / "plugins.xml"; + std::ofstream(xml_path) << content; + return xml_path.string(); +} + +TEST(PluginlibTest, libraryWithoutPathIsSkipped) { + // A without a usable 'path' attribute must be skipped, and the + // remaining libraries must still be parsed (this used to loop forever). + const std::string xml_path = writePluginXml( + "libraryWithoutPathIsSkipped", + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "\n"); + + pluginlib::ClassLoader loader( + "test_pluginlib", "test_base::Fubar", "plugin", {xml_path}); + + EXPECT_FALSE(loader.isClassAvailable("test_pluginlib/no_path")); + EXPECT_FALSE(loader.isClassAvailable("test_pluginlib/empty_path")); + ASSERT_TRUE(loader.isClassAvailable("test_pluginlib/foo")); + + std::shared_ptr foo = loader.createSharedInstance("test_pluginlib/foo"); + foo->initialize(10.0); + EXPECT_EQ(100.0, foo->result()); +} + +TEST(PluginlibTest, libraryPathWithDirectories) { + // The library is found by its file name when the 'path' attribute also + // contains directories that do not exist in the search paths. + const std::string xml_path = writePluginXml( + "libraryPathWithDirectories", + "\n" + " \n" + "\n"); + + pluginlib::ClassLoader loader( + "test_pluginlib", "test_base::Fubar", "plugin", {xml_path}); + + std::shared_ptr foo = loader.createSharedInstance("test_pluginlib/foo"); + foo->initialize(10.0); + EXPECT_EQ(100.0, foo->result()); +} + // Run all the tests that were declared with TEST() int main(int argc, char ** argv) {