Skip to content

Commit

Permalink
fix segfault and throw instead when manifest is nullptr
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed May 22, 2024
1 parent 980db2d commit d8cd725
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 7 additions & 0 deletions include/behaviortree_cpp/tree_node.h
Original file line number Diff line number Diff line change
@@ -433,6 +433,13 @@ inline Expected<Timestamp> TreeNode::getInputStamped(const std::string& key,
{
port_value_str = input_port_it->second;
}
else if(!config().manifest)
{
return nonstd::make_unexpected(StrCat("getInput() of node '", fullPath(),
"' failed because the manifest is "
"nullptr (WTF?) and the key: [",
key, "] is missing"));
}
else
{
// maybe it is declared with a default value in the manifest
25 changes: 21 additions & 4 deletions tests/gtest_ports.cpp
Original file line number Diff line number Diff line change
@@ -18,8 +18,16 @@ class NodeWithPorts : public SyncActionNode
{
int val_A = 0;
int val_B = 0;
if(getInput("in_port_A", val_A) && getInput("in_port_B", val_B) && val_A == 42 &&
val_B == 66)
if(!getInput("in_port_A", val_A))
{
throw RuntimeError("missing input [in_port_A]");
}
if(!getInput("in_port_B", val_B))
{
throw RuntimeError("missing input [in_port_B]");
}

if(val_A == 42 && val_B == 66)
{
return NodeStatus::SUCCESS;
}
@@ -33,6 +41,16 @@ class NodeWithPorts : public SyncActionNode
}
};

TEST(PortTest, WrongNodeConfig)
{
NodeConfig config;
config.input_ports["in_port_A"] = "42";
// intentionally missing:
// config.input_ports["in_port_B"] = "69";
NodeWithPorts node("will_fail", config);
ASSERT_ANY_THROW(node.tick());
}

TEST(PortTest, DefaultPorts)
{
std::string xml_txt = R"(
@@ -61,8 +79,7 @@ TEST(PortTest, MissingPort)
BehaviorTreeFactory factory;
factory.registerNodeType<NodeWithPorts>("NodeWithPorts");
auto tree = factory.createTreeFromText(xml_txt);
NodeStatus status = tree.tickWhileRunning();
ASSERT_EQ(status, NodeStatus::FAILURE);
ASSERT_ANY_THROW(tree.tickWhileRunning());
}

TEST(PortTest, WrongPort)

0 comments on commit d8cd725

Please sign in to comment.