From 61d8c9880e2257ffa7ad6697104aa9f31185e480 Mon Sep 17 00:00:00 2001
From: KyleKlenk <kyle.c.klenk@gmail.com>
Date: Thu, 30 Jun 2022 16:18:02 -0600
Subject: [PATCH] fixed issue with buffer overflow and added 3 attempts to
 create a file

---
 build/makefile-container                       | 16 ++++++++--------
 .../file_access_actor/file_access_actor.cpp    |  9 +++++++--
 .../source/actors/summa_actor/summa_client.cpp |  2 +-
 build/source/netcdf/def_output.f90             | 18 +++++++++++-------
 4 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/build/makefile-container b/build/makefile-container
index e7f087d..a7fad55 100644
--- a/build/makefile-container
+++ b/build/makefile-container
@@ -14,16 +14,16 @@ ACTORS_LIBRARIES = -L/usr/local/lib -L$(ROOT_DIR)/bin -lcaf_core -lcaf_io -lsumm
 
 
 # Production runs
-# FLAGS_NOAH = -O3 -ffree-form -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors
-# FLAGS_COMM = -O3 -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors
-# FLAGS_SUMMA = -O3 -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors
-# FLAGS_ACTORS = -O3 -Wfatal-errors -std=c++17
+FLAGS_NOAH = -O3 -ffree-form -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors
+FLAGS_COMM = -O3 -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors
+FLAGS_SUMMA = -O3 -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors
+FLAGS_ACTORS = -O3 -Wfatal-errors -std=c++17
 
 # # Debug runs
-FLAGS_NOAH = -g -O0 -ffree-form -ffree-line-length-none -fmax-errors=0 -fbacktrace -Wno-unused -Wno-unused-dummy-argument -fPIC
-FLAGS_COMM = -g -O0 -Wall -ffree-line-length-none -fmax-errors=0 -fbacktrace -fcheck=bounds -fPIC
-FLAGS_SUMMA = -g -O0 -Wall -ffree-line-length-none -fmax-errors=0 -fbacktrace -fcheck=bounds -fPIC
-FLAGS_ACTORS = -g -O0 -Wall -std=c++17
+# FLAGS_NOAH = -g -O0 -ffree-form -ffree-line-length-none -fmax-errors=0 -fbacktrace -Wno-unused -Wno-unused-dummy-argument -fPIC
+# FLAGS_COMM = -g -O0 -Wall -ffree-line-length-none -fmax-errors=0 -fbacktrace -fcheck=bounds -fPIC
+# FLAGS_SUMMA = -g -O0 -Wall -ffree-line-length-none -fmax-errors=0 -fbacktrace -fcheck=bounds -fPIC
+# FLAGS_ACTORS = -g -O0 -Wall -std=c++17
 
 
 
diff --git a/build/source/actors/file_access_actor/file_access_actor.cpp b/build/source/actors/file_access_actor/file_access_actor.cpp
index 94e499a..66c8d41 100644
--- a/build/source/actors/file_access_actor/file_access_actor.cpp
+++ b/build/source/actors/file_access_actor/file_access_actor.cpp
@@ -226,8 +226,13 @@ void initalizeFileAccessActor(stateful_actor<file_access_state>* self) {
     initFailedHRUTracker(&self->state.numGRU);
 
     // The CreateOutput Routine Can Fail So we may have to try it multiple times
-    int attempt = 0;
-    def_output(self->state.handle_ncid, &self->state.startGRU, &self->state.numGRU, &self->state.numGRU, &err);
+    int maxAttempt = 3;
+    for (int i = 0; i < maxAttempt; i++) {
+        def_output(self->state.handle_ncid, &self->state.startGRU, &self->state.numGRU, &self->state.numGRU, &err);
+        if (err == 0) {
+            break;
+        }
+    }
     if (err != 0) {
         aout(self) << "ERROR: Create_OutputFile\n";
         std::string function = "Create_Output_File";
diff --git a/build/source/actors/summa_actor/summa_client.cpp b/build/source/actors/summa_actor/summa_client.cpp
index cfea2af..b192b72 100644
--- a/build/source/actors/summa_actor/summa_client.cpp
+++ b/build/source/actors/summa_actor/summa_client.cpp
@@ -67,7 +67,7 @@ void connecting(stateful_actor<summa_client_state>* self, const std::string& hos
 behavior running(stateful_actor<summa_client_state>* self, const actor& server_actor) {
     char host[HOST_NAME_MAX];
     aout(self) << "Client Has Started Successfully" << std::endl;
-    gethostname(host, 1024);
+    gethostname(host, HOST_NAME_MAX);
     self->state.hostname = host;
 
     self->send(server_actor, connect_to_server_v, self, self->state.hostname);
diff --git a/build/source/netcdf/def_output.f90 b/build/source/netcdf/def_output.f90
index e90b0da..59a6cae 100755
--- a/build/source/netcdf/def_output.f90
+++ b/build/source/netcdf/def_output.f90
@@ -126,15 +126,19 @@ subroutine def_output(handle_ncid,startGRU,nGRU,nHRU,err) bind(C, name='def_outp
   err=0; message="def_output/"
 
   ! allocate space for the output file ID array
-  allocate(ncid%var(maxVarFreq))
-  ncid%var(:) = integerMissing
+  if (.not.allocated(ncid%var))then
+    allocate(ncid%var(maxVarFreq))
+    ncid%var(:) = integerMissing
+  endif
 
   ! initalize outputTimeStep - keeps track of the step the GRU is writing for
-  allocate(outputTimeStep(nGRU))
-  do iGRU = 1, nGRU
-    allocate(outputTimeStep(iGRU)%dat(maxVarFreq))
-    outputTimeStep(iGRU)%dat(:) = 1
-  end do 
+  if (.not.allocated(outputTimeStep))then
+    allocate(outputTimeStep(nGRU))
+    do iGRU = 1, nGRU
+      allocate(outputTimeStep(iGRU)%dat(maxVarFreq))
+      outputTimeStep(iGRU)%dat(:) = 1
+    end do
+  end if
 
   ! Set the global variable for the number of HRU and GRU in run
   nGRUrun = nGRU
-- 
GitLab