Skip to content

Commit 9080eff

Browse files
committed
Reject port names containing whitespace
1 parent 4ede94d commit 9080eff

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

include/behaviortree_cpp/basic_types.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,9 @@ template <typename T = AnyTypeAllowed>
443443
auto sname = static_cast<std::string>(name);
444444
if(!IsAllowedPortName(sname))
445445
{
446-
throw RuntimeError("The name of a port must not be `name` or `ID` "
447-
"and must start with an alphabetic character. "
446+
throw RuntimeError("The name of a port must not be `name` or `ID`, "
447+
"must start with an alphabetic character, "
448+
"and must not contain whitespace. "
448449
"Underscore is reserved.");
449450
}
450451

src/basic_types.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "behaviortree_cpp/tree_node.h"
33
#include "behaviortree_cpp/json_export.h"
44

5+
#include <algorithm>
6+
#include <cctype>
57
#include <cstdlib>
68
#include <cstring>
79
#include <clocale>
@@ -443,6 +445,10 @@ bool IsAllowedPortName(StringView str)
443445
{
444446
return false;
445447
}
448+
if(std::any_of(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c); }))
449+
{
450+
return false;
451+
}
446452
return !IsReservedAttribute(str);
447453
}
448454

tests/gtest_ports.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,3 +861,17 @@ TEST(PortTest, VectorAny)
861861
ASSERT_NO_THROW(status = tree.tickOnce());
862862
ASSERT_EQ(status, NodeStatus::FAILURE);
863863
}
864+
865+
TEST(PortTest, IsAllowedPortNameRejectsWhitespace)
866+
{
867+
EXPECT_FALSE(IsAllowedPortName("my port"));
868+
EXPECT_FALSE(IsAllowedPortName("port\tname"));
869+
EXPECT_FALSE(IsAllowedPortName(" leading"));
870+
EXPECT_FALSE(IsAllowedPortName("trailing "));
871+
EXPECT_FALSE(IsAllowedPortName("has\nnewline"));
872+
873+
// Sanity check: valid names still work.
874+
EXPECT_TRUE(IsAllowedPortName("valid_port"));
875+
EXPECT_TRUE(IsAllowedPortName("anotherPort123"));
876+
}
877+

0 commit comments

Comments
 (0)