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 19CDF3858D3C for ; Fri, 9 Jun 2023 15:00:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 19CDF3858D3C 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=1686322842; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=aKBSeyySW/VA2UhSRW1+bSazF2eCNyfSwSjc3Aejtk0=; b=UE6awji6eBDbL3Ez/OjmWAuMQ9/kzUv1haozl2BxRZkFvZHbTXAxNNq4PyYbH9PqC+mdwd XkfOgWsrhmEEgnm1YzQotuKHlBMsrxxkPduDxVMMum1HInB1cIAQs6A6w2ghuXnqu9DL3g M01aouS9Nlh7RVmync3fPVXsfJ+va3A= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-507-cmGecA9PPbuu8MPNJA_v5A-1; Fri, 09 Jun 2023 11:00:40 -0400 X-MC-Unique: cmGecA9PPbuu8MPNJA_v5A-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9F2F53C0F19F for ; Fri, 9 Jun 2023 15:00:39 +0000 (UTC) Received: from oak (unknown [10.22.10.124]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8A1C84087C62 for ; Fri, 9 Jun 2023 15:00:39 +0000 (UTC) Date: Fri, 9 Jun 2023 11:00:38 -0400 From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Subject: Re: [PATCH v2] check_native: Get rid of alloca Message-ID: <20230609150038.GY176347@oak> References: <20230601145500.2039369-1-josimmon@redhat.com> MIME-Version: 1.0 In-Reply-To: <20230601145500.2039369-1-josimmon@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=-12.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,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 Thu, Jun 01, 2023 at 10:55:00AM -0400, Joe Simmons-Talbott wrote: > Use malloc rather than alloca to avoid potential stack overflow. Ping. Thanks, Joe > --- > Changes to v1: > * Do not use a cleanup handler. > > sysdeps/unix/sysv/linux/check_native.c | 35 ++++++++------------------ > 1 file changed, 11 insertions(+), 24 deletions(-) > > diff --git a/sysdeps/unix/sysv/linux/check_native.c b/sysdeps/unix/sysv/linux/check_native.c > index 34876ca624..601f14e5b0 100644 > --- a/sysdeps/unix/sysv/linux/check_native.c > +++ b/sysdeps/unix/sysv/linux/check_native.c > @@ -48,11 +48,20 @@ __check_native (uint32_t a1_index, int *a1_native, > nladdr.nl_family = AF_NETLINK; > > socklen_t addr_len = sizeof (nladdr); > - bool use_malloc = false; > > if (fd < 0) > return; > > + /* Netlink requires that user buffer needs to be either 8kb or page size > + (whichever is bigger), however this has been changed over time and now > + 8Kb is sufficient (check NLMSG_DEFAULT_SIZE on Linux > + linux/include/linux/netlink.h). */ > + const size_t buf_size = 8192; > + char *buf = malloc (buf_size); > + > + if (buf == NULL) > + goto out; > + > if (__bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) != 0 > || __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) != 0) > goto out; > @@ -81,26 +90,6 @@ __check_native (uint32_t a1_index, int *a1_native, > memset (&nladdr, '\0', sizeof (nladdr)); > nladdr.nl_family = AF_NETLINK; > > -#ifdef PAGE_SIZE > - /* Help the compiler optimize out the malloc call if PAGE_SIZE > - is constant and smaller or equal to PTHREAD_STACK_MIN/4. */ > - const size_t buf_size = PAGE_SIZE; > -#else > - const size_t buf_size = __getpagesize (); > -#endif > - char *buf; > - > - if (__libc_use_alloca (buf_size)) > - buf = alloca (buf_size); > - else > - { > - buf = malloc (buf_size); > - if (buf != NULL) > - use_malloc = true; > - else > - goto out; > - } > - > struct iovec iov = { buf, buf_size }; > > if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0, > @@ -170,7 +159,5 @@ __check_native (uint32_t a1_index, int *a1_native, > > out: > __close_nocancel_nostatus (fd); > - > - if (use_malloc) > - free (buf); > + free(buf); > } > -- > 2.39.2 >