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.133.124]) by sourceware.org (Postfix) with ESMTPS id 63E893858D38 for ; Fri, 30 Jun 2023 14:42:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63E893858D38 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=1688136145; 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=logHlHgC/OJezLIL70Jp7vyv1nVF6j/SHpbIjbfqlD4=; b=JIxQQ2mxVQep2Y6CmryXS2c5uwqbDOpxIEJ7K/861C5vNYrtMgnmIvnsMPozc1lBO5/1t7 tClGlRvH6ZPrur7i474dpVMsTGoomXP75PzzJ+O0N1QklEzlD/W4C+hsVt5Lv6nk5kfwUn d4iDJsIClovL+Y9EfQ2SGzMWhxcrLyE= 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-272-pex2yBSMNEi9Fu3GHCFSiQ-1; Fri, 30 Jun 2023 10:42:23 -0400 X-MC-Unique: pex2yBSMNEi9Fu3GHCFSiQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 535463813F30; Fri, 30 Jun 2023 14:42:23 +0000 (UTC) Received: from oak (unknown [10.22.8.210]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 386631121314; Fri, 30 Jun 2023 14:42:23 +0000 (UTC) Date: Fri, 30 Jun 2023 10:42:21 -0400 From: Joe Simmons-Talbott To: Adhemerval Zanella Netto Cc: libc-alpha@sourceware.org Subject: Re: [PATCH] getaddrinfo: Get rid of alloca Message-ID: <20230630144221.GS6392@oak> References: <20230620214031.1241057-1-josimmon@redhat.com> MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,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 Fri, Jun 30, 2023 at 10:15:48AM -0300, Adhemerval Zanella Netto wrote: > > > On 20/06/23 18:40, Joe Simmons-Talbott via Libc-alpha wrote: > > Use a scratch_buffer rather than alloca to avoid potential stack > > overflow. > > --- > > sysdeps/posix/getaddrinfo.c | 22 ++++++++-------------- > > 1 file changed, 8 insertions(+), 14 deletions(-) > > > > diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c > > index 0356b622be..442475d621 100644 > > --- a/sysdeps/posix/getaddrinfo.c > > +++ b/sysdeps/posix/getaddrinfo.c > > @@ -2404,22 +2404,17 @@ getaddrinfo (const char *name, const char *service, > > struct addrinfo *q; > > struct addrinfo *last = NULL; > > char *canonname = NULL; > > - bool malloc_results; > > size_t alloc_size = nresults * (sizeof (*results) + sizeof (size_t)); > > + struct scratch_buffer buf; > > + scratch_buffer_init (&buf); > > > > - malloc_results > > - = !__libc_use_alloca (alloc_size); > > - if (malloc_results) > > + if (!scratch_buffer_set_array_size (&buf, 1, alloc_size)) > > I think it would be better to use: > > if (!scratch_buffer_set_array_size (&buf, nresults, > sizeof (*results) + sizeof (size_t))) > [...] > > To use the overflow checks done by scratch_buffer_set_array_size ( > sizeof (*results) + sizeof (size_t) is always safe so I think there is no > need to add extra checks). You are correct. Thanks for catching that and for the review. I've pushed your suggestion as v2. Thanks, Joe