From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 184E13858C83 for ; Tue, 16 May 2023 13:51:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 184E13858C83 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684245068; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=vtpzkIFHqxXjvNVxVmGFXuwj+piRJBDmKlqOvlqyqoU=; b=c7Dk58v1y6dqlx/9lBLXPRXvDy1VoKLq2IhMRIw3YgJ+ATN6yzQR8+ITbbY7TNsHqutVwV Mhyh6x69CI75TC7VFl/KvWywuGPj+Ts2WU4Nywv13PhSSOiYFBh3yllyM3S5TFdg5/ajkc Zd461Bn/Dax0cBkS05aZ5zZSVWIpUv4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-507-qnPEddVSNbycNuhj6X1eSA-1; Tue, 16 May 2023 09:51:06 -0400 X-MC-Unique: qnPEddVSNbycNuhj6X1eSA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EE94C857E6B; Tue, 16 May 2023 13:51:05 +0000 (UTC) Received: from oak (unknown [10.22.32.82]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D2340400F5A; Tue, 16 May 2023 13:51:05 +0000 (UTC) Date: Tue, 16 May 2023 09:51:04 -0400 From: Joe Simmons-Talbott To: Adhemerval Zanella Netto Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v2] setsourcefilter: Use malloc() rather than alloca(). Message-ID: <20230516135104.GE176347@oak> References: <20230515190806.2770627-1-josimmon@redhat.com> <55a4e9ee-7996-bfc1-752a-b7842691d141@linaro.org> MIME-Version: 1.0 In-Reply-To: <55a4e9ee-7996-bfc1-752a-b7842691d141@linaro.org> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Tue, May 16, 2023 at 08:09:13AM -0300, Adhemerval Zanella Netto wrote: > > > On 15/05/23 16:08, Joe Simmons-Talbott via Libc-alpha wrote: > > To prevent possible stack overflow use malloc() rather than alloca(). > > --- > > Changes to v1: > > - don't save and restore errno around free() > > > > sysdeps/unix/sysv/linux/setsourcefilter.c | 20 ++++---------------- > > 1 file changed, 4 insertions(+), 16 deletions(-) > > > > diff --git a/sysdeps/unix/sysv/linux/setsourcefilter.c b/sysdeps/unix/sysv/linux/setsourcefilter.c > > index 538f4de696..2823168d34 100644 > > --- a/sysdeps/unix/sysv/linux/setsourcefilter.c > > +++ b/sysdeps/unix/sysv/linux/setsourcefilter.c > > @@ -16,7 +16,6 @@ > > License along with the GNU C Library; if not, see > > . */ > > > > -#include > > #include > > #include > > #include > > @@ -34,17 +33,11 @@ setsourcefilter (int s, uint32_t interface, const struct sockaddr *group, > > /* We have to create an struct ip_msfilter object which we can pass > > to the kernel. */ > > size_t needed = GROUP_FILTER_SIZE (numsrc); > > - int use_alloca = __libc_use_alloca (needed); > > > > struct group_filter *gf; > > - if (use_alloca) > > - gf = (struct group_filter *) alloca (needed); > > - else > > - { > > - gf = (struct group_filter *) malloc (needed); > > - if (gf == NULL) > > - return -1; > > - } > > + gf = (struct group_filter *) malloc (needed); > > + if (gf == NULL) > > + return -1; > > > > gf->gf_interface = interface; > > memcpy (&gf->gf_group, group, grouplen); > > @@ -63,12 +56,7 @@ setsourcefilter (int s, uint32_t interface, const struct sockaddr *group, > > else > > result = __setsockopt (s, sol, MCAST_MSFILTER, gf, needed); > > > > - if (! use_alloca) > > - { > > - int save_errno = errno; > > - free (gf); > > - __set_errno (save_errno); > > - } > > + free (gf); > > > > return result; > > } > > Maybe we can use a scratch_buffer to avoid always use malloc: > > diff --git a/sysdeps/unix/sysv/linux/setsourcefilter.c b/sysdeps/unix/sysv/linux/setsourcefilter.c > index 538f4de696..22b307ca80 100644 > --- a/sysdeps/unix/sysv/linux/setsourcefilter.c > +++ b/sysdeps/unix/sysv/linux/setsourcefilter.c > @@ -16,13 +16,10 @@ > License along with the GNU C Library; if not, see > . */ > > -#include > #include > -#include > #include > -#include > #include > -#include > +#include > #include "getsourcefilter.h" > > > @@ -34,17 +31,11 @@ setsourcefilter (int s, uint32_t interface, const struct sockaddr *group, > /* We have to create an struct ip_msfilter object which we can pass > to the kernel. */ > size_t needed = GROUP_FILTER_SIZE (numsrc); > - int use_alloca = __libc_use_alloca (needed); > - > - struct group_filter *gf; > - if (use_alloca) > - gf = (struct group_filter *) alloca (needed); > - else > - { > - gf = (struct group_filter *) malloc (needed); > - if (gf == NULL) > - return -1; > - } > + struct scratch_buffer buf; > + scratch_buffer_init (&buf); > + if (!scratch_buffer_set_array_size (&buf, 1, needed)) > + return -1; > + struct group_filter *gf = buf.data; > > gf->gf_interface = interface; > memcpy (&gf->gf_group, group, grouplen); > @@ -63,12 +54,7 @@ setsourcefilter (int s, uint32_t interface, const struct sockaddr *group, > else > result = __setsockopt (s, sol, MCAST_MSFILTER, gf, needed); > > - if (! use_alloca) > - { > - int save_errno = errno; > - free (gf); > - __set_errno (save_errno); > - } > + scratch_buffer_free (&buf); > > return result; > } > Thanks for the suggestion. This patch is now superseded by a new patch [1] Thanks, Joe [1] https://sourceware.org/pipermail/libc-alpha/2023-May/148146.html