-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(DataNode): segfault when unlink unmanaged
When an Unmanaged DataNode is unlinked (e.g. was temporary linked to print a JSON string), the method dereferences a nullptr which is UB. Added tests in unsafe.cpp. Fixes: #21 Change-Id: Ia6a621e0b9d5faad8c64368b3a2e344e52288e87
- Loading branch information
1 parent
d8bf2a8
commit c5ecde0
Showing
2 changed files
with
34 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,13 @@ | |
* Written by Václav Kubernát <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
*/ | ||
|
||
#include <doctest/doctest.h> | ||
#include <libyang-cpp/Context.hpp> | ||
#include <libyang-cpp/Utils.hpp> | ||
#include <libyang/libyang.h> | ||
#include <libyang/tree_data.h> | ||
#include "example_schema.hpp" | ||
#include "test_vars.hpp" | ||
#include "utils/filesystem_path.hpp" | ||
|
@@ -40,7 +41,6 @@ TEST_CASE("Unsafe methods") | |
ctx_deleter.release(); | ||
|
||
auto wrapped = libyang::createUnmanagedContext(ctx, ly_ctx_destroy); | ||
|
||
} | ||
|
||
DOCTEST_SUBCASE("No custom deleter") | ||
|
@@ -111,6 +111,20 @@ TEST_CASE("Unsafe methods") | |
// Both are still unmanaged, both are accessible. | ||
REQUIRE(wrapped.path() == "/example-schema:leafInt32"); | ||
REQUIRE(anotherNodeWrapped.path() == "/example-schema:leafInt8"); | ||
|
||
DOCTEST_SUBCASE("no explicit unlink") { } | ||
|
||
DOCTEST_SUBCASE("unlink an unmanaged node from an unmanaged node") | ||
{ | ||
REQUIRE(wrapped.findPath("/example-schema:leafInt8")); | ||
REQUIRE(anotherNodeWrapped.findPath("/example-schema:leafInt32")); | ||
|
||
// After unlink they are not reachable from each other | ||
anotherNodeWrapped.unlink(); | ||
REQUIRE(!wrapped.findPath("/example-schema:leafInt8")); | ||
REQUIRE(!anotherNodeWrapped.findPath("/example-schema:leafInt32")); | ||
lyd_free_all(anotherNode); | ||
} | ||
} | ||
|
||
// You have a C++ managed node and you want to insert that into an unmanaged node. | ||
|
@@ -123,6 +137,22 @@ TEST_CASE("Unsafe methods") | |
// BOTH are now unmanaged, both are accessible. | ||
REQUIRE(wrapped.path() == "/example-schema:leafInt32"); | ||
REQUIRE(anotherNodeWrapped.path() == "/example-schema:leafInt8"); | ||
|
||
DOCTEST_SUBCASE("no explicit unlink") { } | ||
|
||
DOCTEST_SUBCASE("unlink a managed node from an unmanaged node") | ||
{ | ||
REQUIRE(wrapped.findPath("/example-schema:leafInt8")); | ||
REQUIRE(anotherNodeWrapped.findPath("/example-schema:leafInt32")); | ||
|
||
// After unlink they are not reachable from each other | ||
anotherNodeWrapped.unlink(); | ||
REQUIRE(!wrapped.findPath("/example-schema:leafInt8")); | ||
REQUIRE(!anotherNodeWrapped.findPath("/example-schema:leafInt32")); | ||
|
||
// this is still unmanaged and we need to delete it | ||
lyd_free_all(anotherNode); | ||
} | ||
} | ||
|
||
// You have a C++ managed node and you want to insert an unmanaged node into it. | ||
|