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 DB669385802F for ; Wed, 4 Oct 2023 14:00:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB669385802F 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=1696428004; 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=OxCBXg+J1Asoic7283tprweH9EC5lJxAq0640KlZAs0=; b=dAki+8/63pAk+sVpvoGnsy/ai9SzqW4g9ZLtKnmI8+WQVvcv6AmLL22yjN7UQH8B3cfQEa cairEW2Bme62sSt3cg73uEIVo491Yrsqy5Rp10XhRx2stJa8dCWCQSa6xtLg09LTqaX0FG wPu5DjaBrBhBHIE4UUtUMdD0pkyJwgE= 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-554-GtRIvZuvMcCyCBxiPr6TlA-1; Wed, 04 Oct 2023 10:00:03 -0400 X-MC-Unique: GtRIvZuvMcCyCBxiPr6TlA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 18048810BDD for ; Wed, 4 Oct 2023 14:00:03 +0000 (UTC) Received: from oak (unknown [10.22.16.149]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F0520400F0F; Wed, 4 Oct 2023 14:00:02 +0000 (UTC) Date: Wed, 4 Oct 2023 10:00:01 -0400 From: Joe Simmons-Talbott To: Arjun Shankar Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v2] nss: Get rid of alloca usage in makedb's write_output. Message-ID: <20231004140001.GH4098455@oak> References: <20230928141125.600816-1-josimmon@redhat.com> MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 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.9 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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP 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 Wed, Oct 04, 2023 at 03:09:14PM +0200, Arjun Shankar wrote: > Hi Joe, > > > Replace alloca usage with a scratch_buffer. > > --- > > Changes to v1: > > * move scratch_buffer_free after the call to error (). > > > > nss/makedb.c | 14 +++++++++++++- > > 1 file changed, 13 insertions(+), 1 deletion(-) > > > > diff --git a/nss/makedb.c b/nss/makedb.c > > index 48c8fe1333..67c3c4c893 100644 > > --- a/nss/makedb.c > > +++ b/nss/makedb.c > > @@ -25,6 +25,7 @@ > > #include > > #include > > #include > > +#include > > #include > > #include > > #include > > OK. > > > @@ -739,7 +740,16 @@ write_output (int fd) > > struct nss_db_header *header; > > uint64_t file_offset = (sizeof (struct nss_db_header) > > + (ndatabases * sizeof (header->dbs[0]))); > > - header = alloca (file_offset); > > + struct scratch_buffer sbuf; > > + scratch_buffer_init (&sbuf); > > + > > + > > + if (!scratch_buffer_set_array_size (&sbuf, 1, file_offset)) > > + { > > + error (0, errno, gettext ("failed to allocate memory")); > > + return EXIT_FAILURE; > > + } > > The way scratch_buffer_init and scratch_buffer_set_array_size are > implemented currently, a failure of the set_array_size here looks like > it doesn't necessarily need a scratch_buffer_free to be called after > failure because there's nothing to be freed. But I think calling > scratch_buffer_free here before returning here is the safer bet. > I haven't been including a scratch_buffer_free call on failure of scratch_buffer_set_array_size. Both failure code paths for scratch_buffer_set_array_size reset the buffer to the default character array and any heap allocated storage is freed. If there is agreement that a call to scratch_buffer_free is desired I'm happy to add it. Thanks, Joe