From: Eric Wong <bofh@yhbt.net>
To: raindrops-public@yhbt.net
Subject: [PATCH 3/2] khashl: use ruby_xrealloc2 to avoid overflow
Date: Sun, 24 Mar 2024 21:10:43 +0000 [thread overview]
Message-ID: <20240324211043.M354387@dcvr> (raw)
In-Reply-To: <20240324001918.1644612-3-bofh@yhbt.net>
While no user is likely to have enough listeners to trigger an
overflow, just use ruby_xrealloc2 to be safe since it's already
provided by Ruby (and AFAIK reallocarray(3) isn't standardized).
---
ext/raindrops/khashl.h | 8 ++++----
ext/raindrops/linux_inet_diag.c | 4 ++--
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/ext/raindrops/khashl.h b/ext/raindrops/khashl.h
index df97c7f..425d95e 100644
--- a/ext/raindrops/khashl.h
+++ b/ext/raindrops/khashl.h
@@ -164,9 +164,8 @@ static kh_inline khint_t __kh_h2b(khint_t hash, khint_t bits) { return hash * 26
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 */ \
- khkey_t *new_keys = (khkey_t*)krealloc((void*)h->keys, new_n_buckets * sizeof(khkey_t)); \
- if (!new_keys) { kfree(new_used); return -1; } \
- h->keys = new_keys; \
+ h->keys = ruby_xrealloc2(h->keys, new_n_buckets, \
+ sizeof(khkey_t)); \
} /* otherwise shrink */ \
new_mask = new_n_buckets - 1; \
for (j = 0; j != n_buckets; ++j) { \
@@ -189,7 +188,8 @@ static kh_inline khint_t __kh_h2b(khint_t hash, khint_t bits) { return hash * 26
} \
} \
if (n_buckets > new_n_buckets) /* shrink the hash table */ \
- h->keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
+ h->keys = ruby_xrealloc2(h->keys, new_n_buckets, \
+ sizeof(khkey_t)); \
kfree(h->used); /* free the working space */ \
h->used = new_used, h->bits = new_bits; \
return 0; \
diff --git a/ext/raindrops/linux_inet_diag.c b/ext/raindrops/linux_inet_diag.c
index 058936b..79f24bb 100644
--- a/ext/raindrops/linux_inet_diag.c
+++ b/ext/raindrops/linux_inet_diag.c
@@ -53,10 +53,10 @@ struct listen_stats {
uint32_t listener_p;
};
-/* override khashl.h defaults */
+/* override khashl.h defaults, these run w/o GVL */
#define kcalloc(N,Z) xcalloc(N,Z)
#define kmalloc(Z) xmalloc(Z)
-#define krealloc(P,Z) xrealloc(P,Z)
+#define krealloc(P,Z) abort() /* never called, we use ruby_xrealloc2 */
#define kfree(P) xfree(P)
#include "khashl.h"
prev parent reply other threads:[~2024-03-24 21:10 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-24 0:19 [PATCH 0/2] drop ruby/st.h usage in favor of khashl Eric Wong
2024-03-24 0:19 ` [PATCH 1/2] linux_inet_diag: remove needless OBJ_FREEZE calls Eric Wong
2024-03-24 0:19 ` [PATCH 2/2] use switch to khashl for hash table outside of GVL Eric Wong
2024-03-24 21:10 ` Eric Wong [this message]
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=20240324211043.M354387@dcvr \
--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).