Skip to content
Merged
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
62 changes: 62 additions & 0 deletions .github/workflows/cmake_macos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: cmake macOS

on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
BUILD_TYPE: Release

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
# macos-latest is Apple Silicon (arm64) and builds with AppleClang +
# libc++, i.e. the toolchain that exercises Apple-specific standard
# library behavior (e.g. the missing floating-point std::from_chars).
os: [macos-latest]

steps:
- uses: actions/checkout@v7

# Install via Homebrew rather than turtlebrowser/get-conan: on macOS runners
# that action's `pip3 install` fails with PEP 668 (externally-managed-environment).
- name: Install Conan
run: brew install conan

- name: Create default profile
run: conan profile detect

- name: Install conan dependencies
run: conan install conanfile.py -s build_type=${{env.BUILD_TYPE}} --build=missing --settings:host compiler.cppstd=17

- name: Normalize build type
shell: bash
run: echo "BUILD_TYPE_LOWERCASE=$(echo "${BUILD_TYPE}" | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV

- name: Configure CMake
shell: bash
run: cmake --preset conan-${{ env.BUILD_TYPE_LOWERCASE }}

- name: Build
shell: bash
run: cmake --build --preset conan-${{ env.BUILD_TYPE_LOWERCASE }}

# The tests below assert tight wall-clock timing that the shared macOS
# runners cannot meet reliably (they are much slower/noisier under load).
# They are not macOS-specific logic and keep running on Linux and Windows,
# so exclude them here to keep this job deterministic without touching the
# shared tests. --repeat until-pass absorbs residual flakiness in the rest.
- name: run test (macOS)
run: |
EXCLUDE='SimpleParallelTest.ConditionsTrue|ComplexParallelTest.ConditionRightFalseAction1Done|Parallel.PauseWithRetry|Parallel.Issue819_SequenceVsReactiveSequence|SequenceTripleActionTest.TripleAction|SimpleSequenceWithMemoryTest.ConditionTrue|SwitchTest.CaseSwitchToDefault|CoroTest.do_action_timeout|CoroTest.sequence_child|DeadlineTest.DeadlineTriggeredTest|Decorator.DelayWithXML'
ctest --test-dir build/${{env.BUILD_TYPE}} --output-on-failure --repeat until-pass:3 -E "$EXCLUDE"
17 changes: 17 additions & 0 deletions include/behaviortree_cpp/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ template <>
template <>
[[nodiscard]] double convertFromString<double>(StringView str);

/**
* @brief Parse a double from a string using the semantics of
* std::from_chars(std::chars_format::general): locale-independent (only '.' as
* the decimal separator), rejecting leading whitespace, a leading '+', and hex
* floats. Includes a thread-safe fallback for standard libraries that lack the
* floating-point std::from_chars overload (e.g. Apple libc++).
*
* @param str the input string.
* @param out set to the parsed value on success (left untouched on failure).
* @param require_full_consumption when true, the whole string must be a valid
* double (trailing characters cause failure); when false, parsing stops
* at the first non-numeric character.
* @return true on success.
*/
[[nodiscard]] bool parseDouble(StringView str, double& out,
bool require_full_consumption);

// Integer numbers separated by the character ";"
template <>
[[nodiscard]] std::vector<int> convertFromString<std::vector<int>>(StringView str);
Expand Down
9 changes: 8 additions & 1 deletion include/behaviortree_cpp/decorators/delay_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ class DelayNode : public DecoratorNode

~DelayNode() override
{
halt();
// Only stop the timer thread here; do NOT call halt(). During tree
// destruction the child node may already be gone: nodes are owned by a
// flat std::vector<TreeNode::Ptr> whose element-destruction order is
// unspecified (libstdc++ tears down front-to-back, libc++ back-to-front),
// so the child can outlive or predecease this decorator. halt() ->
// DecoratorNode::halt() -> resetChild() dereferences that child and
// segfaults on libc++ (macOS). This mirrors TimeoutNode's destructor.
timer_.cancelAll();
}

DelayNode(const DelayNode&) = delete;
Expand Down
106 changes: 84 additions & 22 deletions src/basic_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
#include <algorithm>
#include <array>
#include <charconv>
#if __cpp_lib_to_chars < 201611L
#include <clocale>
// Apple's libc++ lacks the floating-point std::from_chars overload; parseDouble
// falls back to strtod_l with a private "C" locale, which needs these headers.
#if !defined(__cpp_lib_to_chars) || (__cpp_lib_to_chars < 201611L)
#include <cctype>
#include <cerrno>

#include <locale.h>
#if defined(__APPLE__)
#include <xlocale.h>
#endif
#endif
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -196,34 +204,88 @@ uint32_t convertFromString<uint32_t>(StringView str)
return ConvertWithBoundCheck<uint32_t>(str);
}

bool parseDouble(StringView str, double& out, bool require_full_consumption)
{
#if defined(__cpp_lib_to_chars) && (__cpp_lib_to_chars >= 201611L)
// std::from_chars is locale-independent and thread-safe.
const char* begin = str.data();
const char* end = begin + str.size();
const auto [ptr, ec] = std::from_chars(begin, end, out);
if(ec != std::errc())
{
return false;
}
return !require_full_consumption || ptr == end;
#else
// Apple's libc++ lacks the floating-point std::from_chars overload. Reproduce
// its std::chars_format::general semantics with strtod_l under a "C" locale
// created once (thread-safe, no global setlocale mutation), plus a guard that
// rejects the leading whitespace, leading '+', and hex floats that strtod
// would otherwise accept.
if(str.empty())
{
return false;
}
const char first = str.front();
if(first == '+' || std::isspace(static_cast<unsigned char>(first)) != 0)
{
return false;
}
// std::from_chars(general) does not recognise a "0x"/"0X" hex-float prefix; it
// parses only the leading "0" and stops at the 'x'. strtod would consume the
// whole hex float, so emulate from_chars here: the value is 0, and everything
// from the 'x' onward is unparsed (a failure only in the full-consumption case).
const std::size_t mantissa = (first == '-') ? 1u : 0u;
if(str.size() > mantissa + 1 && str[mantissa] == '0' &&
(str[mantissa + 1] == 'x' || str[mantissa + 1] == 'X'))
{
if(require_full_consumption)
{
return false;
}
out = (first == '-') ? -0.0 : 0.0;
return true;
}
static ::locale_t c_locale =
::newlocale(LC_NUMERIC_MASK, "C", static_cast<::locale_t>(0));
if(c_locale == static_cast<::locale_t>(0))
{
return false;
}
// strtod_l needs a null-terminated buffer.
const std::string buffer(str.data(), str.size());
errno = 0;
char* parse_end = nullptr;
const double value = ::strtod_l(buffer.c_str(), &parse_end, c_locale);
if(parse_end == buffer.c_str() || errno == ERANGE)
{
return false;
}
if(require_full_consumption && parse_end != buffer.c_str() + buffer.size())
{
return false;
}
out = value;
return true;
#endif
}

template <>
double convertFromString<double>(StringView str)
{
#if __cpp_lib_to_chars >= 201611L
// from_chars is locale-independent and thread-safe
double result = 0;
const auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
if(ec != std::errc())
if(!parseDouble(str, result, /*require_full_consumption=*/false))
{
throw RuntimeError(StrCat("Can't convert string [", str, "] to double"));
}
return result;
#else
// Fallback: stod is locale-dependent, so force "C" locale.
// See issue #120. Note: setlocale is not thread-safe.
const std::string old_locale = setlocale(LC_NUMERIC, nullptr);
std::ignore = setlocale(LC_NUMERIC, "C");
const std::string str_copy(str.data(), str.size());
const double val = std::stod(str_copy);
std::ignore = setlocale(LC_NUMERIC, old_locale.c_str());
return val;
#endif
}

template <>
float convertFromString<float>(StringView str)
{
#if __cpp_lib_to_chars >= 201611L
// Parse directly as float to preserve std::from_chars<float> range semantics.
float result = 0;
const auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
if(ec != std::errc())
Expand All @@ -232,12 +294,12 @@ float convertFromString<float>(StringView str)
}
return result;
#else
const std::string old_locale = setlocale(LC_NUMERIC, nullptr);
std::ignore = setlocale(LC_NUMERIC, "C");
const std::string str_copy(str.data(), str.size());
const double val = std::stod(str_copy);
std::ignore = setlocale(LC_NUMERIC, old_locale.c_str());
return static_cast<float>(val);
double result = 0;
if(!parseDouble(str, result, /*require_full_consumption=*/false))
{
throw RuntimeError(StrCat("Can't convert string [", str, "] to float"));
}
return static_cast<float>(result);
#endif
}

Expand Down
3 changes: 1 addition & 2 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1200,8 +1200,7 @@ void BT::XMLParser::PImpl::recursivelyCreateSubtree(
if(!stored)
{
double dbl_val = 0;
auto [ptr, ec] = std::from_chars(begin, end, dbl_val);
if(ec == std::errc() && ptr == end)
if(parseDouble(str_value, dbl_val, /*require_full_consumption=*/true))
{
new_bb->set(attr_name, dbl_val);
stored = true;
Expand Down
Loading