* [PATCH] khashl: avoid multiplication overflow + memset
@ 2024-03-30 18:03 Eric Wong
0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2024-03-30 18:03 UTC (permalink / raw)
To: raindrops-public
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<<h->bits : 0U; \
if (n_buckets < new_n_buckets) { /* expand */ \
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2024-03-30 18:03 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-30 18:03 [PATCH] khashl: avoid multiplication overflow + memset Eric Wong
Code repositories for project(s) associated with this public inbox
https://yhbt.net/raindrops.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).