Skip to content

Commit af187d1

Browse files
committed
don't segfault when listing module's features
This is especially relevant when working with the "printed context" from sysrepo, because that context has all the parsed info removed. As a result, it is not possible to query a module's list of features. Let's stop dereferencing a null pointer in that case. Bug: sysrepo/sysrepo#3695 Change-Id: I6258f580b4e5aa02cae2d60260b84593aefcf587
1 parent 38d5c10 commit af187d1

5 files changed

Lines changed: 22 additions & 0 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ if(BUILD_TESTING)
118118
endfunction()
119119

120120
libyang_cpp_test(context)
121+
target_link_libraries(test_context PkgConfig::LIBYANG)
121122
libyang_cpp_test(data_node)
122123
target_link_libraries(test_data_node PkgConfig::LIBYANG)
123124
libyang_cpp_test(schema_node)

‎include/libyang-cpp/Utils.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class LIBYANG_CPP_EXPORT Error : public std::runtime_error {
3535
class LIBYANG_CPP_EXPORT ParsedInfoUnavailable : public Error {
3636
public:
3737
explicit ParsedInfoUnavailable();
38+
explicit ParsedInfoUnavailable(const std::string& what);
3839
};
3940

4041
/**

‎src/Module.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ void Module::setImplemented(const AllFeatures)
155155
*/
156156
std::vector<Feature> Module::features() const
157157
{
158+
if (!m_module->parsed) {
159+
throw ParsedInfoUnavailable{"Module::features: lys_module::parsed is not available"};
160+
}
158161
std::vector<Feature> res;
159162
for (const auto& feature : std::span(m_module->parsed->features, LY_ARRAY_COUNT(m_module->parsed->features))) {
160163
res.emplace_back(Feature{&feature, m_ctx});

‎src/utils/exception.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ ParsedInfoUnavailable::ParsedInfoUnavailable()
2222
{
2323
}
2424

25+
ParsedInfoUnavailable::ParsedInfoUnavailable(const std::string& what)
26+
: Error(what)
27+
{
28+
}
29+
2530
ErrorCode ErrorWithCode::code() const
2631
{
2732
return m_errCode;

‎tests/context.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <fstream>
1111
#include <libyang-cpp/Context.hpp>
1212
#include <libyang-cpp/Utils.hpp>
13+
#include <libyang/context.h>
1314
#include "example_schema.hpp"
1415
#include "pretty_printers.hpp"
1516
#include "test_vars.hpp"
@@ -333,6 +334,17 @@ TEST_CASE("context")
333334
REQUIRE(enabledFeatures == expectedEnabledFeatures);
334335
}
335336

337+
DOCTEST_SUBCASE("printed context")
338+
{
339+
ctx->setSearchDir(TESTS_DIR / "yang");
340+
auto mod = ctx->loadModule("mod1", std::nullopt, {"*"});
341+
REQUIRE(mod.features().size() == 3);
342+
ly_ctx_free_parsed(retrieveContext(*ctx));
343+
REQUIRE_THROWS_WITH_AS(mod.features(),
344+
"Module::features: lys_module::parsed is not available",
345+
libyang::ParsedInfoUnavailable);
346+
}
347+
336348
DOCTEST_SUBCASE("Module::setImplemented")
337349
{
338350
ctx->setSearchDir(TESTS_DIR / "yang");

0 commit comments

Comments
 (0)