From 1056445ba4e412f93174ca440028a9fe9dba4b2b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 30 Mar 2024 18:03:54 +0000 Subject: khashl: avoid multiplication overflow + memset Instead of a malloc where arguments can silently overflow, use calloc directly to handle the overflow and zeroing for us. --- ext/raindrops/khashl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/raindrops/khashl.h b/ext/raindrops/khashl.h index 425d95e..9a6e4fe 100644 --- a/ext/raindrops/khashl.h +++ b/ext/raindrops/khashl.h @@ -159,8 +159,8 @@ static kh_inline khint_t __kh_h2b(khint_t hash, khint_t bits) { return hash * 26 new_bits = j > 2? j : 2; \ new_n_buckets = (khint_t)1U << new_bits; \ if (h->count > (new_n_buckets>>1) + (new_n_buckets>>2)) return 0; /* requested size is too small */ \ - new_used = (khint32_t*)kmalloc(__kh_fsize(new_n_buckets) * sizeof(khint32_t)); \ - memset(new_used, 0, __kh_fsize(new_n_buckets) * sizeof(khint32_t)); \ + new_used = (khint32_t*)kcalloc(__kh_fsize(new_n_buckets), \ + sizeof(khint32_t)); \ if (!new_used) return -1; /* not enough memory */ \ n_buckets = h->keys? (khint_t)1U<bits : 0U; \ if (n_buckets < new_n_buckets) { /* expand */ \ -- cgit v1.2.3-24-ge0c7