Sat Feb 26 11:57:21 2022 UTC ()
make: fix memory leak in Lst_Remove (since 2020-10-23)

The code to free the list node (as opposed to the node data) was
accidentally removed in lst.c 1.83 from 2020-10-23 as part of cleaning
up an unnecessarily complicated function for traversing linked lists.

The memory leak only affected a few lists that actually used Lst_Remove.
Most lists are append-only and are freed using Lst_Done or Lst_Free at
the end, which correctly free the memory.


(rillig)
diff -r1.105 -r1.106 src/usr.bin/make/lst.c

cvs diff -r1.105 -r1.106 src/usr.bin/make/lst.c (expand / switch to unified diff)

--- src/usr.bin/make/lst.c 2021/03/15 16:45:30 1.105
+++ src/usr.bin/make/lst.c 2022/02/26 11:57:21 1.106
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lst.c,v 1.105 2021/03/15 16:45:30 rillig Exp $ */ 1/* $NetBSD: lst.c,v 1.106 2022/02/26 11:57:21 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -24,27 +24,27 @@ @@ -24,27 +24,27 @@
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 */ 33 */
34 34
35#include "make.h" 35#include "make.h"
36 36
37MAKE_RCSID("$NetBSD: lst.c,v 1.105 2021/03/15 16:45:30 rillig Exp $"); 37MAKE_RCSID("$NetBSD: lst.c,v 1.106 2022/02/26 11:57:21 rillig Exp $");
38 38
39static ListNode * 39static ListNode *
40LstNodeNew(ListNode *prev, ListNode *next, void *datum) 40LstNodeNew(ListNode *prev, ListNode *next, void *datum)
41{ 41{
42 ListNode *ln = bmake_malloc(sizeof *ln); 42 ListNode *ln = bmake_malloc(sizeof *ln);
43 43
44 ln->prev = prev; 44 ln->prev = prev;
45 ln->next = next; 45 ln->next = next;
46 ln->datum = datum; 46 ln->datum = datum;
47 47
48 return ln; 48 return ln;
49} 49}
50 50
@@ -153,26 +153,28 @@ void @@ -153,26 +153,28 @@ void
153Lst_Remove(List *list, ListNode *ln) 153Lst_Remove(List *list, ListNode *ln)
154{ 154{
155 /* unlink it from its neighbors */ 155 /* unlink it from its neighbors */
156 if (ln->next != NULL) 156 if (ln->next != NULL)
157 ln->next->prev = ln->prev; 157 ln->next->prev = ln->prev;
158 if (ln->prev != NULL) 158 if (ln->prev != NULL)
159 ln->prev->next = ln->next; 159 ln->prev->next = ln->next;
160 160
161 /* unlink it from the list */ 161 /* unlink it from the list */
162 if (list->first == ln) 162 if (list->first == ln)
163 list->first = ln->next; 163 list->first = ln->next;
164 if (list->last == ln) 164 if (list->last == ln)
165 list->last = ln->prev; 165 list->last = ln->prev;
 166
 167 free(ln);
166} 168}
167 169
168/* Replace the datum in the given node with the new datum. */ 170/* Replace the datum in the given node with the new datum. */
169void 171void
170LstNode_Set(ListNode *ln, void *datum) 172LstNode_Set(ListNode *ln, void *datum)
171{ 173{
172 assert(datum != NULL); 174 assert(datum != NULL);
173 175
174 ln->datum = datum; 176 ln->datum = datum;
175} 177}
176 178
177/* 179/*
178 * Replace the datum in the given node with NULL. 180 * Replace the datum in the given node with NULL.