Skip to content

Commit 564a0e7

Browse files
committed
Fix bs_get_binary2 for a potential use after free
Likewise f9b70c6 Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent abd7122 commit 564a0e7

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

src/libAtomVM/opcodesswitch.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5103,9 +5103,6 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
51035103
uint32_t fail;
51045104
DECODE_LABEL(fail, pc)
51055105
term src;
5106-
#ifdef IMPL_EXECUTE_LOOP
5107-
const uint8_t *src_pc = pc;
5108-
#endif
51095106
DECODE_COMPACT_TERM(src, pc);
51105107
uint32_t live;
51115108
DECODE_LITERAL(live, pc);
@@ -5157,8 +5154,10 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
51575154
term_set_match_state_offset(src, bs_offset + size_val * unit);
51585155

51595156
TRIM_LIVE_REGS(live);
5157+
// there is always room for a MAX_REG + 1 register, used as working register
5158+
x_regs[live] = bs_bin;
51605159
size_t heap_size = term_sub_binary_heap_size(bs_bin, size_val);
5161-
if (UNLIKELY(memory_ensure_free_with_roots(ctx, heap_size, live, x_regs, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
5160+
if (UNLIKELY(memory_ensure_free_with_roots(ctx, heap_size, live + 1, x_regs, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
51625161
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
51635162
}
51645163
#endif
@@ -5167,9 +5166,7 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
51675166
DECODE_DEST_REGISTER(dreg, pc);
51685167

51695168
#ifdef IMPL_EXECUTE_LOOP
5170-
// re-compute src
5171-
DECODE_COMPACT_TERM(src, src_pc);
5172-
bs_bin = term_get_match_state_binary(src);
5169+
bs_bin = x_regs[live];
51735170

51745171
term t = term_maybe_create_sub_binary(bs_bin, bs_offset / unit, size_val, &ctx->heap, ctx->global);
51755172
WRITE_REGISTER(dreg, t);

0 commit comments

Comments
 (0)