raindrops RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <bofh@yhbt.net>
To: raindrops-public@yhbt.net
Subject: [PATCH] khashl: avoid multiplication overflow + memset
Date: Sat, 30 Mar 2024 18:03:54 +0000	[thread overview]
Message-ID: <20240330180354.16298-1-bofh@yhbt.net> (raw)

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 */ \

                 reply	other threads:[~2024-03-30 18:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/raindrops/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240330180354.16298-1-bofh@yhbt.net \
    --to=bofh@yhbt.net \
    --cc=raindrops-public@yhbt.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).