diff mbox series

jobs: Fix resource leak in makejob

Message ID ZlMSa5pG5S68WpOl@gondor.apana.org.au (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show
Series jobs: Fix resource leak in makejob | expand

Commit Message

Herbert Xu May 26, 2024, 10:43 a.m. UTC
If the ps ckmalloc call fails in makejob, the job will still be
marked as used and thus leaked.  Fix this by moving the ckmalloc
call earlier.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---
 src/jobs.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/src/jobs.c b/src/jobs.c
index 4cf6b8c..02ec6f4 100644
--- a/src/jobs.c
+++ b/src/jobs.c
@@ -765,8 +765,9 @@  err:
 
 struct job *makejob(int nprocs)
 {
-	int i;
+	struct procstat *ps;
 	struct job *jp;
+	int i;
 
 	for (i = njobs, jp = jobtab ; ; jp++) {
 		if (--i < 0) {
@@ -783,6 +784,9 @@  struct job *makejob(int nprocs)
 		break;
 	}
 	memset(jp, 0, sizeof(*jp));
+	ps = &jp->ps0;
+	if (nprocs > 1)
+		ps = ckmalloc(nprocs * sizeof(*ps));
 #if JOBS
 	if (jobctl)
 		jp->jobctl = 1;
@@ -790,10 +794,7 @@  struct job *makejob(int nprocs)
 	jp->prev_job = curjob;
 	curjob = jp;
 	jp->used = 1;
-	jp->ps = &jp->ps0;
-	if (nprocs > 1) {
-		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
-	}
+	jp->ps = ps;
 	TRACE(("makejob(%d) returns %%%d\n", nprocs,
 	    jobno(jp)));
 	return jp;