Skip to content

Commit 7df8419

Browse files
committed
fix integer overflow in gc threshold calculation
Signed-off-by: zhenweijin <[email protected]>
1 parent 6b51c61 commit 7df8419

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

core/shared/mem-alloc/ems/ems_gc_internal.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,13 @@ typedef struct gc_heap_struct {
338338
static inline void
339339
gc_update_threshold(gc_heap_t *heap)
340340
{
341-
heap->gc_threshold =
342-
heap->total_free_size * heap->gc_threshold_factor / 1000;
341+
uint64_t result = (uint64_t)heap->total_free_size
342+
* (uint64_t)heap->gc_threshold_factor / 1000;
343+
/* heap->total_free_size * heap->gc_threshold_factor won't exceed
344+
* 6^32(GC_HEAP_SIZE_MAX * GC_DEFAULT_THRESHOLD_FACTOR), so casting result
345+
* to uint32_t is safe
346+
*/
347+
heap->gc_threshold = (uint32_t)result;
343348
}
344349

345350
#define gct_vm_mutex_init os_mutex_init

0 commit comments

Comments
 (0)