Skip to content

Commit 85bad39

Browse files
committed
memory: abort on temp_stack malloc failures
This change doesn't require any API change, still it is not optimal since it makes use of abort(). Signed-off-by: Davide Bettio <[email protected]>
1 parent 15fbf8b commit 85bad39

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/libAtomVM/memory.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,15 @@ unsigned long memory_estimate_usage(term t)
224224
unsigned long acc = 0;
225225

226226
struct TempStack temp_stack;
227-
temp_stack_init(&temp_stack);
227+
if (UNLIKELY(temp_stack_init(&temp_stack) != TempStackOk)) {
228+
// TODO: handle failed malloc
229+
AVM_ABORT();
230+
}
228231

229-
temp_stack_push(&temp_stack, t);
232+
if (UNLIKELY(temp_stack_push(&temp_stack, t) != TempStackOk)) {
233+
// TODO: handle failed malloc
234+
AVM_ABORT();
235+
}
230236

231237
while (!temp_stack_is_empty(&temp_stack)) {
232238
if (term_is_atom(t)) {
@@ -243,7 +249,10 @@ unsigned long memory_estimate_usage(term t)
243249

244250
} else if (term_is_nonempty_list(t)) {
245251
acc += 2;
246-
temp_stack_push(&temp_stack, term_get_list_tail(t));
252+
if (UNLIKELY(temp_stack_push(&temp_stack, term_get_list_tail(t)) != TempStackOk)) {
253+
// TODO: handle failed malloc
254+
AVM_ABORT();
255+
}
247256
t = term_get_list_head(t);
248257

249258
} else if (term_is_tuple(t)) {
@@ -252,7 +261,10 @@ unsigned long memory_estimate_usage(term t)
252261

253262
if (tuple_size > 0) {
254263
for (int i = 1; i < tuple_size; i++) {
255-
temp_stack_push(&temp_stack, term_get_tuple_element(t, i));
264+
if (UNLIKELY(temp_stack_push(&temp_stack, term_get_tuple_element(t, i)) != TempStackOk)) {
265+
// TODO: handle failed malloc
266+
AVM_ABORT();
267+
}
256268
}
257269
t = term_get_tuple_element(t, 0);
258270

@@ -265,10 +277,19 @@ unsigned long memory_estimate_usage(term t)
265277
acc += term_map_size_in_terms(map_size);
266278
if (map_size > 0) {
267279
for (int i = 1; i < map_size; i++) {
268-
temp_stack_push(&temp_stack, term_get_map_key(t, i));
269-
temp_stack_push(&temp_stack, term_get_map_value(t, i));
280+
if (UNLIKELY(temp_stack_push(&temp_stack, term_get_map_key(t, i)) != TempStackOk)) {
281+
// TODO: handle failed malloc
282+
AVM_ABORT();
283+
}
284+
if (UNLIKELY(temp_stack_push(&temp_stack, term_get_map_value(t, i)) != TempStackOk)) {
285+
// TODO: handle failed malloc
286+
AVM_ABORT();
287+
}
288+
}
289+
if (UNLIKELY(temp_stack_push(&temp_stack, term_get_map_value(t, 0)) != TempStackOk)) {
290+
// TODO: handle failed malloc
291+
AVM_ABORT();
270292
}
271-
temp_stack_push(&temp_stack, term_get_map_value(t, 0));
272293
t = term_get_map_key(t, 0);
273294

274295
} else {

0 commit comments

Comments
 (0)