Fix YangSchema::enableFeature segfault on invalid modules
Now throws an exception on invalid modules.
Change-Id: I494813fa5bc3f8357d98e731f7b2748e2df3ee90
diff --git a/src/yang_schema.cpp b/src/yang_schema.cpp
index bbfce7c..2b69dc7 100644
--- a/src/yang_schema.cpp
+++ b/src/yang_schema.cpp
@@ -375,7 +375,12 @@
void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName)
{
- m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str());
+ auto module = getYangModule(moduleName);
+ if (!module) {
+ using namespace std::string_literals;
+ throw std::runtime_error("Module \""s + moduleName + "\" doesn't exist.");
+ }
+ module->feature_enable(featureName.c_str());
}
void YangSchema::registerModuleCallback(const std::function<std::string(const char*, const char*, const char*, const char*)>& clb)
@@ -406,7 +411,7 @@
std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)
{
- return m_context->get_module(name.c_str(), nullptr, 0);
+ return m_context->get_module(name.c_str());
}
namespace {
diff --git a/tests/yang.cpp b/tests/yang.cpp
index f50d76e..833ef70 100644
--- a/tests/yang.cpp
+++ b/tests/yang.cpp
@@ -1183,5 +1183,10 @@
REQUIRE_THROWS(ys.nodeType(path, node));
}
+
+ SECTION("enableFeature - non existing module")
+ {
+ REQUIRE_THROWS_AS(ys.enableFeature("non-existing", "just-no"), std::runtime_error);
+ }
}
}