From f1435e0a1065b644517e40f2cf08a1f5548f711c Mon Sep 17 00:00:00 2001
From: Moe Jette <jette1@llnl.gov>
Date: Fri, 24 Jan 2003 23:54:58 +0000
Subject: [PATCH] Fix bug related to xfree of unitialized variable on error,
 fix typo in comment. add FREE_NULL function.

---
 src/slurmd/mgr.c    | 3 +--
 src/slurmd/req.c    | 5 +++--
 src/slurmd/shm.c    | 4 ++--
 src/slurmd/slurmd.h | 8 ++++++++
 4 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/src/slurmd/mgr.c b/src/slurmd/mgr.c
index c356a39cfa2..1f266e3e173 100644
--- a/src/slurmd/mgr.c
+++ b/src/slurmd/mgr.c
@@ -264,8 +264,7 @@ mgr_launch_batch_job(batch_job_launch_msg_t *msg, slurm_addr *cli)
 	if ((batchdir = _make_batch_dir(job)) == NULL) 
 		goto cleanup2;
 
-	if (job->argv[0])
-		xfree(job->argv[0]);
+	FREE_NULL(job->argv[0]);
 
 	if ((job->argv[0] = _make_batch_script(msg, batchdir)) == NULL)
 		goto cleanup3;
diff --git a/src/slurmd/req.c b/src/slurmd/req.c
index 8a6c8fb0dd6..a3bbb18953f 100644
--- a/src/slurmd/req.c
+++ b/src/slurmd/req.c
@@ -421,6 +421,7 @@ _rpc_reattach_tasks(slurm_msg_t *msg, slurm_addr *cli)
 	reattach_tasks_request_msg_t  *req = msg->data;
 	reattach_tasks_response_msg_t  resp;
 
+	memset(&resp, 0, sizeof(reattach_tasks_response_msg_t));
 	slurm_get_addr(cli, &port, host, sizeof(host));
 	req_uid = slurm_auth_uid(msg->cred);
 	req_gid = slurm_auth_gid(msg->cred);
@@ -483,8 +484,8 @@ _rpc_reattach_tasks(slurm_msg_t *msg, slurm_addr *cli)
 
 	slurm_send_only_node_msg(&resp_msg);
 
-	xfree(resp.gids);
-	xfree(resp.local_pids);
+	FREE_NULL(resp.gids);
+	FREE_NULL(resp.local_pids);
 
 }
 
diff --git a/src/slurmd/shm.c b/src/slurmd/shm.c
index 5034ede844a..500640f6e02 100644
--- a/src/slurmd/shm.c
+++ b/src/slurmd/shm.c
@@ -429,7 +429,7 @@ _shm_copy_step(job_step_t *j)
 	job_step_t *s;
 	task_t *t;
 
-	s = xmalloc(sizeof(*s));
+	s = xmalloc(sizeof(job_step_t));
 	_shm_step_copy(s, j);
 
 	for (t = _taskp(j->task_list); t; t = _taskp(t->next)) {
@@ -761,7 +761,7 @@ static void
 _shm_task_copy(task_t *to, task_t *from)
 {
 	*to = *from;
-	/* next and step are not valid for copying */
+	/* next and job_step are not valid for copying */
 	to->used = 1;
 	to->next = NULL;
 	to->job_step = NULL;
diff --git a/src/slurmd/slurmd.h b/src/slurmd/slurmd.h
index 9b9700bfbd8..0a87f054ef1 100644
--- a/src/slurmd/slurmd.h
+++ b/src/slurmd/slurmd.h
@@ -50,6 +50,14 @@
 #ifndef __USE_XOPEN_EXTENDED
 extern pid_t getsid(pid_t pid);		/* missing from <unistd.h> */
 #endif
+
+#define FREE_NULL(_X)			\
+	do {				\
+		if (_X) xfree (_X);	\
+		_X	= NULL; 	\
+	} while (0)
+
+
 /*
  * Global config type
  */
-- 
GitLab