From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id ACFC13858D33 for ; Thu, 23 Feb 2023 06:59:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ACFC13858D33 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=bytequest.se Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=bytequest.se Received: by mail-lf1-x130.google.com with SMTP id t11so1856675lfr.1 for ; Wed, 22 Feb 2023 22:59:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytequest.se; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=QCNQDS9ceszuQnVItI0+AchIdyr7aW2xw0sB9Wl68rs=; b=SnA9Qnn+xbi35Z61EIz1payjJoTtwlkvQsM+6NT2kWD1MHzReB61aaE+i06Se4ETk8 92ibnvXA8zFm2dJJtEZgNylwwcu7QVkWsns3F/SRX/DOHp5LiroLYaqib9UDwSE0LJWO QcFyxBZPOvWZtEMTQn7mvjMcimRLMh+i+dNc7kfevt97GnamXC1RO22ryFokB1jLERQY jIgdPoQptuGTOTowIot45S4mZYGZepgyczNBuG8NR6grWHM1jp7b6rtKcJrJIzqGgyCx LDZXQ3ShyzyYefOX9+8SCSaRCDxr76vVlTKrv6n1ms+E3oHb8qJEm64s1YDvhBZpSvqW qlEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=QCNQDS9ceszuQnVItI0+AchIdyr7aW2xw0sB9Wl68rs=; b=bi+UxrSwZeYSlfAYiJwlfYG1LRvKDpNtPRlQ0tYBXyNoJtlbS5t8ScX15KjE4OuO41 J6zSw5WqsiJZjFwTj290zY5zF/EWUuq3orkLBoyUluFWJFCMsSPWOlUai7qpsewssIoT 7gKZM2lxgv18A98Xa9fH1+bKeP27YrnopgKNA6EXyr/LEN3i99g9VEJk4eE9dgnVZA21 yMIO+FxxZsI5hdsXIL2VNcS9OVZLGRjSxi9rTsJPfoxBuhfmxh4KeD2bGUP3B6WyP92w 4h1yhX9RGv+x0yYYO3aERnUgN81I5dxtyYuYLbh6inP7iSZUBE0WttLGMkJsQ6cUjjoX 22AA== X-Gm-Message-State: AO0yUKVm1lY+ZuzQKSTaff8PoaV93lFZ02LyHJWwKQ6uBv8IwKfuy9t5 SLR+iQJLaksBU2PomeFP6OKeWQFI0PBsPjef9XlsTA== X-Google-Smtp-Source: AK7set/wadTXZkT9gpZIaLXTNrVW+3CKYgFf61JlN4j8Xh6rqJ2BtIiz/2fnyi4flrIvZJhOCdclXQ== X-Received: by 2002:ac2:5a5c:0:b0:4d7:58c8:5f44 with SMTP id r28-20020ac25a5c000000b004d758c85f44mr3701789lfn.12.1677135584459; Wed, 22 Feb 2023 22:59:44 -0800 (PST) Received: from henrik-Latitude-5420 (84-217-33-73.customers.ownit.se. [84.217.33.73]) by smtp.gmail.com with ESMTPSA id m25-20020ac24ad9000000b004b4b5da5f80sm508397lfp.219.2023.02.22.22.59.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 22:59:44 -0800 (PST) Date: Thu, 23 Feb 2023 07:59:41 +0100 From: Henrik Nilsson To: newlib@sourceware.org Subject: Re: [PATCH] nano-mallocr: Prevent NULL pointer de-reference in free_list Message-ID: References: <20230217055649.3591878-1-henrik.nilsson@bytequest.se> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230217055649.3591878-1-henrik.nilsson@bytequest.se> X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_BARRACUDACENTRAL,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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, 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? BR, Henrik