From 2e1b21083823fe205ae185c021c48c609f54c932 Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Mon, 13 May 2024 01:07:32 +0200
Subject: [PATCH] Use null character to terminate chunk buffers

---
 src/cluster/cluster.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/cluster/cluster.cpp b/src/cluster/cluster.cpp
index 7193c54e..faabf216 100644
--- a/src/cluster/cluster.cpp
+++ b/src/cluster/cluster.cpp
@@ -324,7 +324,7 @@ void cluster(const string& config_file, const string& data_file, const string& o
 		delete[] chunk_buffer;
 		MPI_Recv(&chunk_buffer_size, 1, MPI_INT, rr, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
 	      }
-	      fputc(10, output);
+	      fprintf(output, "\n");
 
 	      // now get the binary local file
 	      long buffer_size = 0;
@@ -454,7 +454,7 @@ void cluster(const string& config_file, const string& data_file, const string& o
 	  // If EOF is reached, do not send EOF character.
 	  long ptr_position = partial_output.tellg();
 	  if (ptr_position == partial_output_size) {
-	    chunk_buffer_size--;
+	    chunk_buffer[chunk_buffer_size - 1] = '\0';
 	  }
 	  // Send the size of the buffer that is being transmitted (Node-0 does not know whether it is full or not)
 	  MPI_Send(&chunk_buffer_size, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
@@ -464,8 +464,11 @@ void cluster(const string& config_file, const string& data_file, const string& o
 	long ptr_position = partial_output.tellg();
 	if (ptr_position < partial_output_size) {
 	  // Send the last partial buffer
-	  chunk_buffer_size = partial_output_size - ptr_position - 1;
+	  chunk_buffer_size = partial_output_size - ptr_position;
+	  delete[] chunk_buffer;
+	  chunk_buffer = new char[chunk_buffer_size];
 	  partial_output.read(chunk_buffer, chunk_buffer_size);
+	  chunk_buffer[chunk_buffer_size - 1] = '\0';
 	  // Send the size of the buffer that is being transmitted (Node-0 does not know whether it is full or not)
 	  MPI_Send(&chunk_buffer_size, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
 	  // Actually send the file contents to Node-0
-- 
GitLab