Skip to content
Closed
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
6 changes: 3 additions & 3 deletions include/chaiscript/language/chaiscript_optimizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,11 @@ namespace chaiscript {
assert(children.size() == 1);
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);

int i = start_int;
t_ss.add_object(id, var(&i));
auto i = std::make_shared<int>(start_int);
t_ss.add_object(id, var(i));

try {
for (; i < end_int; ++i) {
for (; *i < end_int; ++(*i)) {
try {
// Body of Loop
children[0]->eval(t_ss);
Expand Down
21 changes: 21 additions & 0 deletions unittests/future_assign.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Regression test for #635: Segfault when using := in async function
// with optimized for loop. The := operator copies raw pointers from the
// loop variable, which is stack-allocated in the optimized for loop.
// When the function returns, the stack is unwound and pointers dangle.

var func = fun(){
var ret = 0;
for (var i = 0; i < 50000; ++i) {
ret := i;
}
return ret;
}

var fut1 = async(func);
var fut2 = async(func);

// This triggers Boxed_Number::get_as<int> on the future results
// which would crash if the pointers are dangling
// := creates an alias, so ret ends up as 50000 (loop exit value of i)
assert_equal(50000, fut1.get())
assert_equal(50000, fut2.get())