Skip to content

Commit fe49c5b

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

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,18 @@ 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+
if (result > UINT32_MAX) {
344+
/* Threshold factor can be greater than 1000 (100%), which means
345+
* GC will never be triggered. So heap->gc_threshold >
346+
* APP_HEAP_SIZE_MAX is allowed
347+
*/
348+
heap->gc_threshold = UINT32_MAX;
349+
}
350+
else {
351+
heap->gc_threshold = (uint32_t)result;
352+
}
343353
}
344354

345355
#define gct_vm_mutex_init os_mutex_init

0 commit comments

Comments
 (0)