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
8 changes: 8 additions & 0 deletions include/behaviortree_cpp/basic_types.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <algorithm>
#include <cctype>
#include <chrono>
#include <iostream>
#include <functional>
Expand Down Expand Up @@ -447,6 +449,12 @@ template <typename T = AnyTypeAllowed>
"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<std::string, PortInfo> out;

Expand Down
11 changes: 11 additions & 0 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <sstream>
#include <string>
#include <string_view>
#include <typeindex>

#include "behaviortree_cpp/basic_types.h"
#include "behaviortree_cpp/utils/strcat.hpp"

Expand Down Expand Up @@ -214,6 +218,13 @@ void BT::XMLParser::PImpl::loadSubtreeModel(const XMLElement* xml_root)
{
throw RuntimeError("Missing attribute [name] in port (SubTree model)");
}
std::string_view sname(name);
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, "'"));
}
if(auto default_value = port_node->Attribute("default"))
{
port.setDefaultValue(default_value);
Expand Down
10 changes: 10 additions & 0 deletions tests/gtest_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>("port name"));
ASSERT_ANY_THROW(BT::InputPort<std::string>("port\tname"));
ASSERT_ANY_THROW(BT::InputPort<std::string>("port\nname"));
ASSERT_ANY_THROW(BT::InputPort<std::string>(" leading"));
ASSERT_ANY_THROW(BT::InputPort<std::string>("trailing "));
ASSERT_NO_THROW(BT::InputPort<std::string>("valid_port_name"));
}
42 changes: 42 additions & 0 deletions tests/gtest_subtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,48 @@ TEST(SubTree, SubtreeModels)
tree.tickWhileRunning();
}

TEST(SubTree, WhitespaceInSubtreeModel)
{
// clang-format off

static const char* xml_text = R"(
<root main_tree_to_execute = "MainTree" BTCPP_format="4">
<TreeNodesModel>
<SubTree ID="MySub">
<input_port name="port with space" default="42"/>
</SubTree>
</TreeNodesModel>

<BehaviorTree ID="MainTree">
<Sequence>
<SubTree ID="MySub" />
</Sequence>
</BehaviorTree>

<BehaviorTree ID="MySub">
<Sequence>
<AlwaysSuccess />
</Sequence>
</BehaviorTree>
</root>
)";

// clang-format on

BehaviorTreeFactory factory;
try
{
auto _ = factory.createTreeFromText(xml_text);
}
catch(RuntimeError e)
{
EXPECT_NE(std::string_view(e.what()).find("not contain whitespace"),
std::string_view::npos);
return;
}
FAIL() << "Exception was not thrown.";
}

class PrintToConsole : public BT::SyncActionNode
{
public:
Expand Down
Loading