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 7DFB63857C51 for ; Mon, 27 Feb 2023 10:00:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7DFB63857C51 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=1677492051; h=from:from:reply-to: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=c+TtiNysb5JNXq2vkN2O0gym71cd/5RI1/P79ZJGImM=; b=PrAEfVan4NsF3Q0nWpOQzQHh8DtlZjK19bXB2Q65Utrwcp8sewvzcbsLku7DVt4km7Eb7f IydgZ0N21HE09xGPgTlZCFZ0QYWlzqBS2zgAyNtG1DHUtXDuEyxGCrdkrPaf24vOUFlk1T 0JD5e4nYRCLZTVHS2DRQDfWulXdiVE0= 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-543-zqxIo8ppMwGocFIzPSawqA-1; Mon, 27 Feb 2023 05:00:47 -0500 X-MC-Unique: zqxIo8ppMwGocFIzPSawqA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 02EE2884343; Mon, 27 Feb 2023 10:00:47 +0000 (UTC) Received: from calimero.vinschen.de (unknown [10.39.192.122]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D603B2166B2B; Mon, 27 Feb 2023 10:00:46 +0000 (UTC) Received: by calimero.vinschen.de (Postfix, from userid 500) id 245E8A80856; Mon, 27 Feb 2023 11:00:45 +0100 (CET) Date: Mon, 27 Feb 2023 11:00:45 +0100 From: Corinna Vinschen To: Henrik Nilsson Cc: newlib@sourceware.org Subject: Re: [PATCH] nano-mallocr: Prevent NULL pointer de-reference in free_list Message-ID: Reply-To: newlib@sourceware.org Mail-Followup-To: Henrik Nilsson , newlib@sourceware.org References: <20230217055649.3591878-1-henrik.nilsson@bytequest.se> MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Spam-Status: No, score=-10.7 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_H2,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 Feb 23 07:59, Henrik Nilsson via Newlib wrote: > On Fri, Feb 17, 2023 at 06:56:49AM +0100, Henrik Nilsson wrote: > > The existing code checked if there was a chunk in free_list and > > that the tail was not the next chunk. > > > > The check if there is a chunk is not needed since it's already > > known but the case of a single chunk in free_list needs to be > > handled differently. > > --- > > newlib/libc/stdlib/nano-mallocr.c | 19 ++++++++++++++----- > > 1 file changed, 14 insertions(+), 5 deletions(-) > > > > diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c > > index b2273ba60..a2b50facc 100644 > > --- a/newlib/libc/stdlib/nano-mallocr.c > > +++ b/newlib/libc/stdlib/nano-mallocr.c > > @@ -333,14 +333,23 @@ void * nano_malloc(RARG malloc_size_t s) > > { > > p->size += alloc_size; > > > > - /* Remove chunk from free_list */ > > + /* Remove chunk from free_list. Since p != NULL there is > > + at least one chunk */ > > r = free_list; > > - while (r && p != r->next) > > + if (r->next == NULL) > > { > > - r = r->next; > > + /* There is only a single chunk, remove it */ > > + free_list = NULL; > > + } > > + else > > + { > > + /* Search for the chunk before the one to be removed */ > > + while (p != r->next) > > + { > > + r = r->next; > > + } > > + r->next = NULL; > > } > > - r->next = NULL; > > - > > r = p; > > } > > else > > -- > > 2.34.1 > > > Any comment on this patch? Pushed. Thanks, Corinna