diff --git a/include/behaviortree_cpp/basic_types.h b/include/behaviortree_cpp/basic_types.h index b61d49c16..4bebc687c 100644 --- a/include/behaviortree_cpp/basic_types.h +++ b/include/behaviortree_cpp/basic_types.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include #include @@ -447,6 +449,12 @@ template "and must start with an alphabetic character. " "Underscore is reserved."); } + if(std::any_of(sname.begin(), sname.end(), + [](unsigned char c) { return std::isspace(c); })) + { + throw RuntimeError( + StrCat("The name of a port must not contain whitespace: '", sname, "'")); + } std::pair out; diff --git a/tests/gtest_ports.cpp b/tests/gtest_ports.cpp index eaaf9733a..abb2809fc 100644 --- a/tests/gtest_ports.cpp +++ b/tests/gtest_ports.cpp @@ -861,3 +861,13 @@ TEST(PortTest, VectorAny) ASSERT_NO_THROW(status = tree.tickOnce()); ASSERT_EQ(status, NodeStatus::FAILURE); } + +TEST(PortTest, WhitespaceInPortName) +{ + ASSERT_ANY_THROW(BT::InputPort("port name")); + ASSERT_ANY_THROW(BT::InputPort("port\tname")); + ASSERT_ANY_THROW(BT::InputPort("port\nname")); + ASSERT_ANY_THROW(BT::InputPort(" leading")); + ASSERT_ANY_THROW(BT::InputPort("trailing ")); + ASSERT_NO_THROW(BT::InputPort("valid_port_name")); +}