Skip to content

Commit 76b5a8d

Browse files
joejoe
authored andcommitted
ensure the lock is released for all returns in malloc
1 parent cee5bc2 commit 76b5a8d

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

lib/libbsdmalloc/malloc.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: malloc.c,v 1.11 2025/01/20 20:00:52 riastradh Exp $ */
1+
/* $NetBSD: malloc.c,v 1.12 2026/01/20 19:22:10 joe Exp $ */
22

33
/*
44
* Copyright (c) 1983, 1993
@@ -34,7 +34,7 @@
3434
#if 0
3535
static char sccsid[] = "@(#)malloc.c 8.1 (Berkeley) 6/4/93";
3636
#else
37-
__RCSID("$NetBSD: malloc.c,v 1.11 2025/01/20 20:00:52 riastradh Exp $");
37+
__RCSID("$NetBSD: malloc.c,v 1.12 2026/01/20 19:22:10 joe Exp $");
3838
#endif
3939
#endif /* LIBC_SCCS and not lint */
4040

@@ -197,8 +197,7 @@ malloc(size_t nbytes)
197197
n += pagesz;
198198
if (n) {
199199
if (sbrk((int)n) == (void *)-1) {
200-
mutex_unlock(&malloc_mutex);
201-
return (NULL);
200+
goto out;
202201
}
203202
}
204203
bucket = 0;
@@ -230,7 +229,7 @@ malloc(size_t nbytes)
230229
while (nbytes > amt + n) {
231230
amt <<= 1;
232231
if (amt == 0)
233-
return (NULL);
232+
goto out;
234233
bucket++;
235234
}
236235
/*
@@ -240,8 +239,7 @@ malloc(size_t nbytes)
240239
if ((op = nextf[bucket]) == NULL) {
241240
morecore(bucket);
242241
if ((op = nextf[bucket]) == NULL) {
243-
mutex_unlock(&malloc_mutex);
244-
return (NULL);
242+
goto out;
245243
}
246244
}
247245
/* remove from linked list */
@@ -262,6 +260,9 @@ malloc(size_t nbytes)
262260
*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
263261
#endif
264262
return ((void *)(op + 1));
263+
out:
264+
mutex_unlock(&malloc_mutex);
265+
return (NULL);
265266
}
266267

267268
/*

0 commit comments

Comments
 (0)