From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id 9905F3858D33 for ; Fri, 17 Feb 2023 05:57:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9905F3858D33 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-x12c.google.com with SMTP id p19so279783lfr.9 for ; Thu, 16 Feb 2023 21:57:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytequest.se; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=gjBK3a0xccfYb8rdLZpfA0trvruKx7qyl+1OSaIwK1Y=; b=e6dCXGiVDXO4xXciZxax4xIwtVit4wIeG49oXB54YCPcTAGtgryqDktnAomyX5E+P9 4CkwHbdIGbAECimXws6C7nUfuxjMdnNhn6J/E7MfEPATzUxwVSvTV0vTmE6m57vOtQ6g 7kmpE01zkzVPdmUgIPzet/adjHYWUcs9fzhXVtUfndTeuBkiwWinbyXNNoWnb2GqhQPz XgEzoi0dUTrp/WngN9THepg9BnCSliAptJmErgXezIp2GwVNKPdlvh3oxVpj4AAkcsFx fN4LTWeJx5n+23OM9PHPXkXRYJJC8Br7xNzokjmgbn6qmBCfYAeHmmEL8dnV3TpunG0c euzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gjBK3a0xccfYb8rdLZpfA0trvruKx7qyl+1OSaIwK1Y=; b=Q5ujoAZ2Wf3GrtJoTXIAxPPIhlPfTb/IJ5FGM7pZt8j5nC8IInzJl7GgJSg+W2Ic42 nLZHUwp+SS++2vhqlvQAVXKrFOELfyIUSw1PFPg5GxpZiiaaDonrXPERCV7wVBP6KzKi DqZQCnixj8JqrwutSlg+GNCIiY7dzYBUvCFPPWlp4UyoqO2+bwFkD8s+Sk8sDXlqicjy roCYoXaQBOz8AytKw+HXWrILNuP8ILT7N5x3uvZUYytHg5KqOQMi1sv/Zc7NVtabVYQt oxn19oaH4c+uADRFkXvriAHiG/BO2Apst0Oxf29tSK3jy2+bx7qWmCzgwEU6HAibriix edYw== X-Gm-Message-State: AO0yUKWz0TVsQi/mFZhkpJA4szRRI8RjunlI9AzGMoQQXpHaHjLAeK78 Afnj+/AxL1OPHEgUGPtzoKTu/f5lP+RPTV+cv8U+hw== X-Google-Smtp-Source: AK7set/DwdyiBNwGip5Dl6OPrWS7LNB/yzhtPrHOn4xcnqNfCTWVq4qHLLIjJ+FY0dw9bSl+BFWbGA== X-Received: by 2002:ac2:5a46:0:b0:4da:f436:d423 with SMTP id r6-20020ac25a46000000b004daf436d423mr99415lfn.21.1676613427680; Thu, 16 Feb 2023 21:57:07 -0800 (PST) Received: from henrik-Latitude-5420.min-mail.se (84-217-33-73.customers.ownit.se. [84.217.33.73]) by smtp.gmail.com with ESMTPSA id h18-20020ac250d2000000b004d57a760e4dsm568312lfm.37.2023.02.16.21.57.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Feb 2023 21:57:07 -0800 (PST) From: Henrik Nilsson To: newlib@sourceware.org Cc: Henrik Nilsson Subject: [PATCH] nano-mallocr: Prevent NULL pointer de-reference in free_list Date: Fri, 17 Feb 2023 06:56:49 +0100 Message-Id: <20230217055649.3591878-1-henrik.nilsson@bytequest.se> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: 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