From 085928d175eba0aeec51442e457e654447bbfa79 Mon Sep 17 00:00:00 2001
From: Mark Jones <jones@jlab.org>
Date: Tue, 2 Sep 2014 16:33:01 -0400
Subject: [PATCH] Add "+1" to length when using new char[] Found when doing
 valgrind --tool=memcheck --track-origins=yes --leak-check=full hcana Return
 errors like below ==14467== Invalid write of size 1 ==14467==    at
 0x4A0794F: __GI_strcpy (mc_replace_strmem.c:313) ==14467==    by 0x57137A3:
 THcHodoscope::Setup(char const*, char const*) (THcHodoscope.cxx:112)
 ==14467==    by 0x5713A7E: THcHodoscope::Init(TDatime const&)
 (THcHodoscope.cxx:142) ==14467==    by 0x4D38C29: THaApparatus::Init(TDatime
 const&) (THaApparatus.C:193) ==14467==    by 0x4D67E57:
 THaAnalyzer::InitModules(TList const*, TDatime&, int, char const*)
 (THaAnalyzer.C:433) ==14467==    by 0x4D68B1F:
 THaAnalyzer::DoInit(THaRunBase*) (THaAnalyzer.C:667) ==14467==    by
 0x4D68126: THaAnalyzer::Init(THaRunBase*) (THaAnalyzer.C:467) ==14467==    by
 0x4D6B31A: THaAnalyzer::Process(THaRunBase*) (THaAnalyzer.C:1289) ==14467==  
  by 0x4E21E97: G__haDict_704_0_6(G__value*, char const*, G__param*, int)
 (haDict.C:23491) ==14467==    by 0x6DB6148: Cint::G__ExceptionWrapper(int
 (*)(G__value*, char const*, G__param*, int), G__value*, char*, G__param*,
 int) (in /u/apps/root/5.34.13/root/lib/libCint.so.5.34) ==14467==    by
 0x6E5A3A0: G__execute_call (in /u/apps/root/5.34.13/root/lib/libCint.so.5.34)
 ==14467==    by 0x6E5B201: G__call_cppfunc (in
 /u/apps/root/5.34.13/root/lib/libCint.so.5.34) ==14467==  Address 0xd7636a2
 is 0 bytes after a block of size 2 alloc'd ==14467==    at 0x4A0674C:
 operator new[](unsigned long) (vg_replace_malloc.c:305) ==14467==    by
 0x5713757: THcHodoscope::Setup(char const*, char const*)
 (THcHodoscope.cxx:111) ==14467==    by 0x5713A7E: THcHodoscope::Init(TDatime
 const&) (THcHodoscope.cxx:142) ==14467==    by 0x4D38C29:
 THaApparatus::Init(TDatime const&) (THaApparatus.C:193) ==14467==    by
 0x4D67E57: THaAnalyzer::InitModules(TList const*, TDatime&, int, char const*)
 (THaAnalyzer.C:433) ==14467==    by 0x4D68B1F:
 THaAnalyzer::DoInit(THaRunBase*) (THaAnalyzer.C:667) ==14467==    by
 0x4D68126: THaAnalyzer::Init(THaRunBase*) (THaAnalyzer.C:467) ==14467==    by
 0x4D6B31A: THaAnalyzer::Process(THaRunBase*) (THaAnalyzer.C:1289) ==14467==  
  by 0x4E21E97: G__haDict_704_0_6(G__value*, char const*, G__param*, int)
 (haDict.C:23491) ==14467==    by 0x6DB6148: Cint::G__ExceptionWrapper(int
 (*)(G__value*, char const*, G__param*, int), G__value*, char*, G__param*,
 int) (in /u/apps/root/5

---
 src/THcDC.cxx           | 2 +-
 src/THcDriftChamber.cxx | 2 --
 src/THcHodoscope.cxx    | 2 +-
 src/THcShower.cxx       | 2 +-
 4 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/THcDC.cxx b/src/THcDC.cxx
index 227becf..9bd76b2 100644
--- a/src/THcDC.cxx
+++ b/src/THcDC.cxx
@@ -122,7 +122,7 @@ void THcDC::Setup(const char* name, const char* description)
   }
   fPlaneNames = new char* [fNPlanes];
   for(Int_t i=0;i<fNPlanes;i++) {
-    fPlaneNames[i] = new char[plane_names[i].length()];
+    fPlaneNames[i] = new char[plane_names[i].length()+1];
     strcpy(fPlaneNames[i], plane_names[i].c_str());
   }
 
diff --git a/src/THcDriftChamber.cxx b/src/THcDriftChamber.cxx
index e7d6a89..aec4071 100644
--- a/src/THcDriftChamber.cxx
+++ b/src/THcDriftChamber.cxx
@@ -112,8 +112,6 @@ void THcDriftChamber::AddPlane(THcDriftChamberPlane *plane)
    }
   }
   fNPlanes++;
-  cout << fHMSStyleChambers << "P" << fNPlanes << " " << YPlaneNum << " " << YPlanePNum << " "
-       << YPlaneInd << " " << YPlanePInd << endl;
   return;
 }
 
diff --git a/src/THcHodoscope.cxx b/src/THcHodoscope.cxx
index f59fb46..bf4b47e 100644
--- a/src/THcHodoscope.cxx
+++ b/src/THcHodoscope.cxx
@@ -108,7 +108,7 @@ void THcHodoscope::Setup(const char* name, const char* description)
   }
   fPlaneNames = new char* [fNPlanes];
   for(Int_t i=0;i<fNPlanes;i++) {
-    fPlaneNames[i] = new char[plane_names[i].length()];
+    fPlaneNames[i] = new char[plane_names[i].length()+1];
     strcpy(fPlaneNames[i], plane_names[i].c_str());
   }
 
diff --git a/src/THcShower.cxx b/src/THcShower.cxx
index b2f328b..1133b95 100644
--- a/src/THcShower.cxx
+++ b/src/THcShower.cxx
@@ -83,7 +83,7 @@ void THcShower::Setup(const char* name, const char* description)
 
   fLayerNames = new char* [fNLayers];
   for(Int_t i=0;i<fNLayers;i++) {
-    fLayerNames[i] = new char[layer_names[i].length()];
+    fLayerNames[i] = new char[layer_names[i].length()+1];
     strcpy(fLayerNames[i], layer_names[i].c_str());
   }
   
-- 
GitLab