[PATCH] jobs: Fix resource leak in makejob

Herbert Xu <[email protected]>
Newsgroups org.kernel.vger.dash
Message-ID <[email protected]>
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 <[email protected]>
---
 src/jobs.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

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;
-- 
2.39.2

-- 
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.