Skip to content

Commit 8f67299

Browse files
use the locked entry in script assignment string conversion (#1265)
1 parent d475020 commit 8f67299

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎include/behaviortree_cpp/scripting/operators.hpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,9 @@ struct ExprAssignment : ExprBase
622622
// special case: string to other type.
623623
// Check if we can use the StringConverter
624624
auto const str = value.cast<std::string>();
625-
const auto* entry_info = env.vars->entryInfo(key);
626-
627-
if(auto converter = entry_info->converter())
625+
// Use the entry locked above. Looking the key up again could return
626+
// nullptr, or a different entry, if it was unset in the meantime.
627+
if(auto converter = entry->info.converter())
628628
{
629629
*dst_ptr = converter(str);
630630
}

‎tests/script_parser_test.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,25 @@ TEST(ParserTest, BitwiseComplementRejectsNonIntegers)
721721
static_cast<double>(~std::numeric_limits<int64_t>::min()));
722722
}
723723

724+
TEST(ParserTest, AssignStringWhileEntryIsUnset)
725+
{
726+
auto bb = BT::Blackboard::create();
727+
bb->set<int>("x", 1);
728+
bb->set<std::string>("y", "");
729+
BT::Ast::Environment env = { bb, {} };
730+
731+
// Writing y unsets x, after the assignment to x has already resolved its
732+
// entry. The string to int conversion then used to look x up again and
733+
// dereference the nullptr returned for the missing entry. Another thread
734+
// calling unset() at the wrong time has the same effect.
735+
auto subscriber = bb->subscribeToEntry("y", [&bb]() { bb->unset("x"); });
736+
737+
auto result = BT::ParseScriptAndExecute(env, "x = (y = '42')");
738+
ASSERT_TRUE(result.has_value()) << result.error();
739+
EXPECT_EQ(result.value().cast<int>(), 42);
740+
EXPECT_EQ(bb->getEntry("x"), nullptr);
741+
}
742+
724743
TEST(ParserTest, TernaryExpressions)
725744
{
726745
BT::Ast::Environment env = { BT::Blackboard::create(), {} };

0 commit comments

Comments
 (0)