Skip to content

Commit 78b37db

Browse files
committed
variables: Catch out of memory errors cloning the stack
When we do global variable operations, we may need to clone objects that are on the stack. This may fail. We need to correctly propagate the error when that happens. Signed-off-by: Christophe de Dinechin <christophe@dinechin.org>
1 parent 540aef2 commit 78b37db

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/runtime.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ object_p runtime::clone(object_p source)
979979
}
980980

981981

982-
object_p runtime::clone_global(object_p global, size_t sz)
982+
bool runtime::clone_global(object_p global, size_t sz)
983983
// ----------------------------------------------------------------------------
984984
// Check if any entry in the stack points to a given global, if so clone it
985985
// ----------------------------------------------------------------------------
@@ -997,11 +997,15 @@ object_p runtime::clone_global(object_p global, size_t sz)
997997
if (*s >= global && *s < global + sz)
998998
{
999999
if (!cloned)
1000+
{
10001001
cloned = clone(global);
1002+
if (!cloned)
1003+
return false;
1004+
}
10011005
*s = cloned + (*s - global);
10021006
}
10031007
}
1004-
return cloned;
1008+
return true;
10051009
}
10061010

10071011

src/runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ struct runtime
209209
// Clone an object into the temporaries area
210210
// ------------------------------------------------------------------------
211211

212-
object_p clone_global(object_p source, size_t sz);
212+
bool clone_global(object_p source, size_t sz);
213213
// ------------------------------------------------------------------------
214214
// Clone values in the stack that point to a global we will change
215215
// ------------------------------------------------------------------------

src/variables.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,16 @@ object_p directory::store(object_g name, object_g value)
289289
delta = vs - es;
290290

291291
// Clone any value in the stack that points to the existing value
292-
rt.clone_global(evalue, es);
292+
if (!rt.clone_global(evalue, es))
293+
return nullptr; // Out of memory, bail out
293294

294295
// Clone input value if it is within object being replaced
295296
if (+value >= +evalue && +value < +evalue + es)
297+
{
296298
value = rt.clone(value);
299+
if (!value)
300+
return nullptr;
301+
}
297302

298303
// Move memory above storage if necessary
299304
if (vs != es)
@@ -643,7 +648,8 @@ size_t directory::purge(object_p name)
643648
object_p body = header;
644649
size_t old = leb128<size_t>(body); // Old size of directory
645650

646-
rt.clone_global(value, vs);
651+
if (!rt.clone_global(value, vs))
652+
return 0; // Out of memory, bail out
647653
rt.move_globals(name, name + purged);
648654

649655
if (old < purged)

0 commit comments

Comments
 (0)