Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/behaviortree_cpp/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,9 @@ template <typename T = AnyTypeAllowed>
auto sname = static_cast<std::string>(name);
if(!IsAllowedPortName(sname))
{
throw RuntimeError("The name of a port must not be `name` or `ID` "
"and must start with an alphabetic character. "
throw RuntimeError("The name of a port must not be `name` or `ID`, "
"must start with an alphabetic character, "
"and must not contain whitespace. "
"Underscore is reserved.");
}

Expand Down
6 changes: 6 additions & 0 deletions src/basic_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "behaviortree_cpp/tree_node.h"
#include "behaviortree_cpp/json_export.h"

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <clocale>
Expand Down Expand Up @@ -443,6 +445,10 @@ bool IsAllowedPortName(StringView str)
{
return false;
}
if(std::any_of(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c); }))
{
return false;
}
return !IsReservedAttribute(str);
}

Expand Down
8 changes: 8 additions & 0 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ void BT::XMLParser::PImpl::loadSubtreeModel(const XMLElement* xml_root)
{
throw RuntimeError("Missing attribute [name] in port (SubTree model)");
}
if(!IsAllowedPortName(name))
{
throw RuntimeError(StrCat("The port name [", name, "] in SubTree [",
subtree_id,
"] is not valid. Port names must start "
"with an alphabetic character and must "
"not contain whitespace."));
}
if(auto default_value = port_node->Attribute("default"))
{
port.setDefaultValue(default_value);
Expand Down
32 changes: 32 additions & 0 deletions tests/gtest_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,3 +861,35 @@ TEST(PortTest, VectorAny)
ASSERT_NO_THROW(status = tree.tickOnce());
ASSERT_EQ(status, NodeStatus::FAILURE);
}

TEST(PortTest, IsAllowedPortNameRejectsWhitespace)
{
EXPECT_FALSE(IsAllowedPortName("my port"));
EXPECT_FALSE(IsAllowedPortName("port\tname"));
EXPECT_FALSE(IsAllowedPortName(" leading"));
EXPECT_FALSE(IsAllowedPortName("trailing "));
EXPECT_FALSE(IsAllowedPortName("has\nnewline"));

// Sanity check: valid names still work.
EXPECT_TRUE(IsAllowedPortName("valid_port"));
EXPECT_TRUE(IsAllowedPortName("anotherPort123"));
}

TEST(PortTest, WhitespacePortNameInSubtreeModelThrows)
{
BT::BehaviorTreeFactory factory;

const std::string xml_txt = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<SubTree ID="MySub" my_port="hello" />
</BehaviorTree>
<TreeNodesModel>
<SubTree ID="MySub">
<input_port name="my port" />
</SubTree>
</TreeNodesModel>
</root>)";

EXPECT_THROW(factory.createTreeFromText(xml_txt), BT::RuntimeError);
}
Loading