fix integer overflow in gc threshold calculation (#4546)

Signed-off-by: zhenweijin <zhenwei.jin@intel.com>
This commit is contained in:
Zhenwei Jin 2025-08-19 08:53:28 +08:00 committed by GitHub
parent c661592edd
commit d0c636bd80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -338,8 +338,13 @@ typedef struct gc_heap_struct {
static inline void
gc_update_threshold(gc_heap_t *heap)
{
heap->gc_threshold =
heap->total_free_size * heap->gc_threshold_factor / 1000;
uint64_t result = (uint64_t)heap->total_free_size
* (uint64_t)heap->gc_threshold_factor / 1000;
/* heap->total_free_size * heap->gc_threshold_factor won't exceed
* 6^32(GC_HEAP_SIZE_MAX * GC_DEFAULT_THRESHOLD_FACTOR), so casting result
* to uint32_t is safe
*/
heap->gc_threshold = (uint32_t)result;
}
#define gct_vm_mutex_init os_mutex_init