boost-ext / sml

C++14 State Machine library
https://boost-ext.github.io/sml
Boost Software License 1.0
1.13k stars 177 forks source link

Crash in guard when dependency referenced by const vs non-const #485

Open cppden opened 2 years ago

cppden commented 2 years ago

Expected Behavior

for non-const dependency both const and non-const access is valid.

Actual Behavior

only non-const is valid while const result in nullptr and crash.

Steps to Reproduce the Problem

crash which can be fixed by replacing Dep const* with Dep* in line 26.

ukreator commented 2 years ago

I might be wrong, but from what I understand, sml captures dependencies and matches their type with all modifiers. If not matched, it will pass garbage to your actions/guards/etc.

For example, this fixes your example too (constness now match):

  const Dep dep;
  sml::sm<events> sm(&dep);

And if you do this, it will happily compile but crash by passing garbage to your guard:

  int i = 0;
  sml::sm<events> sm(&i);
cppden commented 2 years ago

AFAIK, sml has the check to detect non-captured dependencies but it seems to work only for references:

In instantiation of 'boost::ext::sml::v1_1_5::aux::pool_type_impl<T&, typename boost::ext::sml::v1_1_5::aux::enable_if<(decltype (test_is_constructible<T>(0))::value && decltype (test_is_constructible<T, T>(0))::value), void>::type>::pool_type_impl(TObject) 
[with TObject = boost::ext::sml::v1_1_5::aux::missing_ctor_parameter<{anonymous}::Dep2>; T = {anonymous}::Dep2; 
typename boost::ext::sml::v1_1_5::aux::enable_if<(decltype (test_is_constructible<T>(0))::value && decltype (test_is_constructible<T, T>(0))::value), void>::type = void]':

I would assume 2 things:

  1. it should check pointers as well.
  2. it should follow native C++ conversion rules which implicitly converts from non-const to const.