Skip to content

Commit d8cd725

Browse files
committed
fix segfault and throw instead when manifest is nullptr
1 parent 980db2d commit d8cd725

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

include/behaviortree_cpp/tree_node.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,13 @@ inline Expected<Timestamp> TreeNode::getInputStamped(const std::string& key,
433433
{
434434
port_value_str = input_port_it->second;
435435
}
436+
else if(!config().manifest)
437+
{
438+
return nonstd::make_unexpected(StrCat("getInput() of node '", fullPath(),
439+
"' failed because the manifest is "
440+
"nullptr (WTF?) and the key: [",
441+
key, "] is missing"));
442+
}
436443
else
437444
{
438445
// maybe it is declared with a default value in the manifest

tests/gtest_ports.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ class NodeWithPorts : public SyncActionNode
1818
{
1919
int val_A = 0;
2020
int val_B = 0;
21-
if(getInput("in_port_A", val_A) && getInput("in_port_B", val_B) && val_A == 42 &&
22-
val_B == 66)
21+
if(!getInput("in_port_A", val_A))
22+
{
23+
throw RuntimeError("missing input [in_port_A]");
24+
}
25+
if(!getInput("in_port_B", val_B))
26+
{
27+
throw RuntimeError("missing input [in_port_B]");
28+
}
29+
30+
if(val_A == 42 && val_B == 66)
2331
{
2432
return NodeStatus::SUCCESS;
2533
}
@@ -33,6 +41,16 @@ class NodeWithPorts : public SyncActionNode
3341
}
3442
};
3543

44+
TEST(PortTest, WrongNodeConfig)
45+
{
46+
NodeConfig config;
47+
config.input_ports["in_port_A"] = "42";
48+
// intentionally missing:
49+
// config.input_ports["in_port_B"] = "69";
50+
NodeWithPorts node("will_fail", config);
51+
ASSERT_ANY_THROW(node.tick());
52+
}
53+
3654
TEST(PortTest, DefaultPorts)
3755
{
3856
std::string xml_txt = R"(
@@ -61,8 +79,7 @@ TEST(PortTest, MissingPort)
6179
BehaviorTreeFactory factory;
6280
factory.registerNodeType<NodeWithPorts>("NodeWithPorts");
6381
auto tree = factory.createTreeFromText(xml_txt);
64-
NodeStatus status = tree.tickWhileRunning();
65-
ASSERT_EQ(status, NodeStatus::FAILURE);
82+
ASSERT_ANY_THROW(tree.tickWhileRunning());
6683
}
6784

6885
TEST(PortTest, WrongPort)

0 commit comments

Comments
 (0)