leftibot 6dc28ca1ba Fix #635: Segfault in async result via dangling pointer from optimized for loop
Two bugs combined to cause the crash:

1. The optimized for loop (chaiscript_optimizer.hpp) stored the loop
   counter as a stack-local `int` and exposed it to ChaiScript via
   `var(&i)`, creating a reference-type Boxed_Value pointing to the
   stack frame.

2. The `:=` operator's `Data::operator=` performed a shallow clone of
   the Any (copying the reference_wrapper, not the value), so
   `ret := i` made `ret` alias the stack-local `int`. When the
   optimized loop's frame unwound, `ret` held a dangling pointer.

Fix 1: The optimizer now heap-allocates the loop counter via
`Boxed_Value(start_int)` and obtains a reference to the
heap-allocated int for the native C++ loop. The Boxed_Value's
shared_ptr ensures the storage outlives any copies.

Fix 2: The `:=` operator now deep-copies arithmetic values via
`Boxed_Number::clone` instead of the shallow `assign()`, ensuring
value semantics. This also fixes a pre-existing aliasing bug where
`a := b; ++b` would incorrectly mutate `a`.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 23:12:37 -06:00
..
chaiscript_algebraic.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
chaiscript_common.hpp Fix #607: Remove unnecessary forward declarations that cause C++20 build failures (#647) 2026-04-11 13:42:40 -06:00
chaiscript_engine.hpp Fix #571: How to redirect "cout" and "print" to a microsoft Windows 10 window? (#657) 2026-04-11 18:58:09 -06:00
chaiscript_eval.hpp Fix #635: Segfault in async result via dangling pointer from optimized for loop 2026-04-11 23:12:37 -06:00
chaiscript_optimizer.hpp Fix #635: Segfault in async result via dangling pointer from optimized for loop 2026-04-11 23:12:37 -06:00
chaiscript_parser.hpp Fix #473: Allow assignment expressions in return statements (#665) 2026-04-11 17:13:50 -06:00
chaiscript_posix.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
chaiscript_prelude_docs.hpp Fix #12: Document reflection and introspection capabilities 2026-04-09 21:43:56 -06:00
chaiscript_prelude.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
chaiscript_tracer.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
chaiscript_unknown.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
chaiscript_windows.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00