Skip to content

fix dangling reference in pool_type_impl<T&> init constructor (#504)#674

Merged
kris-jusiak merged 1 commit into
boost-ext:masterfrom
PavelGuzenfeld:fix/issue-504-pool-type-ref-init-ctor
May 23, 2026
Merged

fix dangling reference in pool_type_impl<T&> init constructor (#504)#674
kris-jusiak merged 1 commit into
boost-ext:masterfrom
PavelGuzenfeld:fix/issue-504-pool-type-ref-init-ctor

Conversation

@PavelGuzenfeld
Copy link
Copy Markdown
Contributor

Problem

The pool_type_impl<T&> specialisation (active under BOOST_SML_CREATE_DEFAULT_CONSTRUCTIBLE_DEPS) contains a copy-paste bug in its (init, object) constructor:

constexpr pool_type_impl(const init &i, const TObject &object)
  : value(i, object) {}  // BUG

value is a T& (reference) member. In a member-initialiser list, value(i, object) for a reference type does not call a two-argument constructor — it applies the C++ comma operator: evaluates i (discards result), then evaluates object, and binds value to the result.

object is a function parameter (const TObject&) that goes out of scope once the constructor returns, leaving value as a dangling reference. Any subsequent access through value is undefined behaviour.

Fix

Initialise the backing-store member value_ by extracting T from the source pool via try_get, then bind the reference member value to value_:

constexpr pool_type_impl(const init &, const TObject &object)
  : value_{try_get<T>(&object)}, value{value_} {}

This stores a local copy in value_ and keeps the reference valid for the lifetime of the pool_type_impl object — matching the pattern already used in the other constructors of this specialisation.

Test

Regression test ref_dep_copy_from_pool_not_dangling added in test/ft/dependencies.cpp: an SM with a reference dep (dep504&) and a sub-state machine exercises the pool(const pool<TArgs...>&) constructor path that instantiates the fixed constructor.

Fixes #504.

@kris-jusiak
Copy link
Copy Markdown
Collaborator

LGTM, thanks, there is merge conflict though, coule you rebase

…oost-ext#504)

The pool_type_impl<T&> specialisation (active under
BOOST_SML_CREATE_DEFAULT_CONSTRUCTIBLE_DEPS) had a copy-paste bug in the
constructor that accepts an (init, object) pair:

  constexpr pool_type_impl(const init &i, const TObject &object)
    : value(i, object) {}

'value' is a T& reference member.  In a member-initialiser list the
expression 'value(i, object)' for a reference type does NOT call a
two-argument constructor; it applies the C++ comma operator — evaluates i
(discarding the result), evaluates object, and binds 'value' to the result.
'object' is a function parameter (const TObject&) which goes out of scope
once the constructor returns, leaving 'value' as a dangling reference.

Fix: initialise the backing-store member value_ by extracting the T from
the source pool via try_get, then bind the reference member value to value_:

  constexpr pool_type_impl(const init &, const TObject &object)
    : value_{try_get<T>(&object)}, value{value_} {}

This stores a local copy in value_ and keeps the reference valid for the
lifetime of the pool_type_impl object.

Regression test added in test/ft/dependencies.cpp:
  ref_dep_copy_from_pool_not_dangling — SM with reference dep + sub-SM
  exercises the pool(const pool<TArgs...>&) path that instantiates the
  fixed constructor.

Fixes boost-ext#504.
@PavelGuzenfeld PavelGuzenfeld force-pushed the fix/issue-504-pool-type-ref-init-ctor branch from 5c36ddb to 2f24cc4 Compare May 23, 2026 21:48
@PavelGuzenfeld
Copy link
Copy Markdown
Contributor Author

LGTM, thanks, there is merge conflict though, coule you rebase

On it.

@kris-jusiak kris-jusiak merged commit e4fdeb2 into boost-ext:master May 23, 2026
1 of 5 checks passed
@PavelGuzenfeld
Copy link
Copy Markdown
Contributor Author

LGTM, thanks, there is merge conflict though, coule you rebase

On it.

I'm also making sure the CI runs green. If its possible please wait till all is GO next time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copy paste error in the sml.hpp source code

2 participants