Skip to content

Commit 7304c3d

Browse files
authored
Prevent malloc_size underflow
I was going nuts debugging an issue that turned out to be mismatched allocators, which triggered a malloc_size underflow and the whole program was running GC all the time unable to do anything else. abort() so the user can see things went wrong.
1 parent 8fe6ea4 commit 7304c3d

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

quickjs.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,8 +1634,13 @@ void js_free_rt(JSRuntime *rt, void *ptr)
16341634
return;
16351635

16361636
s = &rt->malloc_state;
1637+
size_t free_size = rt->mf.js_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
1638+
if (unlikely(free_size > s->malloc_size)) {
1639+
printf("js_free_rt: malloc_size underflow: freeing %zu but only %zu tracked\n", free_size, s->malloc_size);
1640+
abort();
1641+
}
16371642
s->malloc_count--;
1638-
s->malloc_size -= rt->mf.js_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
1643+
s->malloc_size -= free_size;
16391644
rt->mf.js_free(s->opaque, ptr);
16401645
}
16411646

0 commit comments

Comments
 (0)