From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.1 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 70DF01F44D for ; Sun, 24 Mar 2024 21:10:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=yhbt.net; s=selector1; t=1711314643; bh=1i0rnuN2SGu1NxejjY/agc6aAzuxG7ihXoUoEJryB7c=; h=Date:From:To:Subject:References:In-Reply-To:From; b=M6lfqrK/PimClhlxR/8s5Kim7XIUHyBNsfb0D1uMZHr5puR9Qtr/Qf2TYjM7xzrJM u0bl6tuL6s+DkmicmPbe0k9WxuUcTZUs6DNhhGXdPNANoFhqQyJB87vQaAxKaRjQul BwuApTp37I5enOOdAcOn9ivPlin98LwP1bJLwdxM= Date: Sun, 24 Mar 2024 21:10:43 +0000 From: Eric Wong To: raindrops-public@yhbt.net Subject: [PATCH 3/2] khashl: use ruby_xrealloc2 to avoid overflow Message-ID: <20240324211043.M354387@dcvr> References: <20240324001918.1644612-1-bofh@yhbt.net> <20240324001918.1644612-3-bofh@yhbt.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20240324001918.1644612-3-bofh@yhbt.net> List-Id: 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<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"