From 6b418928a56c63c84f7315d3052eb6ad6788dbf3 Mon Sep 17 00:00:00 2001
From: Moe Jette <jette1@llnl.gov>
Date: Thu, 17 Mar 2011 17:26:33 +0000
Subject: [PATCH] remove assert if job_resources struct has zero node count,
 fix short malloc that resulted in memory corruption

---
 src/common/job_resources.c                |  6 +-----
 src/plugins/select/linear/select_linear.c | 26 +++++++++++++++++------
 2 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/src/common/job_resources.c b/src/common/job_resources.c
index 01b3d7d84d2..da9b97fc82d 100644
--- a/src/common/job_resources.c
+++ b/src/common/job_resources.c
@@ -529,15 +529,13 @@ extern void pack_job_resources(job_resources_t *job_resrcs_ptr, Buf buffer,
 {
 	uint32_t cluster_flags = slurmdb_setup_cluster_flags();
 
-	if(protocol_version >= SLURM_2_2_PROTOCOL_VERSION) {
+	if (protocol_version >= SLURM_2_2_PROTOCOL_VERSION) {
 		if (job_resrcs_ptr == NULL) {
 			uint32_t empty = NO_VAL;
 			pack32(empty, buffer);
 			return;
 		}
 
-		xassert(job_resrcs_ptr->nhosts);
-
 		pack32(job_resrcs_ptr->nhosts, buffer);
 		pack32(job_resrcs_ptr->ncpus, buffer);
 		pack8(job_resrcs_ptr->node_req, buffer);
@@ -616,8 +614,6 @@ extern void pack_job_resources(job_resources_t *job_resrcs_ptr, Buf buffer,
 			return;
 		}
 
-		xassert(job_resrcs_ptr->nhosts);
-
 		pack32(job_resrcs_ptr->nhosts, buffer);
 		pack32(job_resrcs_ptr->ncpus, buffer);
 		pack8(job_resrcs_ptr->node_req, buffer);
diff --git a/src/plugins/select/linear/select_linear.c b/src/plugins/select/linear/select_linear.c
index 7bb07bfb942..a10b0becd81 100644
--- a/src/plugins/select/linear/select_linear.c
+++ b/src/plugins/select/linear/select_linear.c
@@ -1574,8 +1574,7 @@ static int _job_expand(struct job_record *from_job_ptr,
 	build_job_resources(new_job_resrcs_ptr, node_record_table_ptr,
 			    select_fast_schedule);
 	xfree(to_job_ptr->node_addr);
-	to_job_ptr->node_addr = xmalloc(sizeof(slurm_addr_t) *
-					to_job_ptr->total_nodes);
+	to_job_ptr->node_addr = xmalloc(sizeof(slurm_addr_t) * node_cnt);
 	first_bit = MIN(bit_ffs(from_job_resrcs_ptr->node_bitmap),
 			bit_ffs(to_job_resrcs_ptr->node_bitmap));
 	last_bit  = MAX(bit_fls(from_job_resrcs_ptr->node_bitmap),
@@ -1674,10 +1673,25 @@ static int _job_expand(struct job_record *from_job_ptr,
 	to_job_ptr->job_resrcs = new_job_resrcs_ptr;
 
 	to_job_ptr->total_cpus   += from_job_ptr->total_cpus;
-	from_job_ptr->total_cpus  = 0;
-	from_job_ptr->total_nodes = 0;
-	to_job_ptr->total_nodes   = new_job_resrcs_ptr->nhosts;
-	from_job_ptr->total_nodes = 0;
+	if (to_job_ptr->details) {
+		to_job_ptr->details->min_cpus = to_job_ptr->total_cpus;
+		to_job_ptr->details->max_cpus = to_job_ptr->total_cpus;
+	}
+	from_job_ptr->total_cpus   = 0;
+	from_job_resrcs_ptr->ncpus = 0;
+	if (from_job_ptr->details) {
+		from_job_ptr->details->min_cpus = 0;
+		from_job_ptr->details->max_cpus = 0;
+	}
+
+	from_job_ptr->total_nodes   = 0;
+	from_job_resrcs_ptr->nhosts = 0;
+	from_job_ptr->node_cnt      = 0;
+	if (from_job_ptr->details)
+		from_job_ptr->details->min_nodes = 0;
+	to_job_ptr->total_nodes     = new_job_resrcs_ptr->nhosts;
+	to_job_ptr->node_cnt        = new_job_resrcs_ptr->nhosts;
+
 	bit_or(to_job_ptr->node_bitmap, from_job_ptr->node_bitmap);
 	bit_nclear(from_job_ptr->node_bitmap, 0, (node_record_count - 1));
 	xfree(to_job_ptr->nodes);
-- 
GitLab