From e232cbe8c0872b18f0f286df80e00e21ce6ddefe Mon Sep 17 00:00:00 2001
From: Gerardo Ganis <Gerardo.Ganis@cern.ch>
Date: Fri, 28 Oct 2011 13:47:10 +0000
Subject: [PATCH] Fix another bunch of Coverity issues

git-svn-id: http://root.cern.ch/svn/root/trunk@41627 27541ba8-7e3a-0410-8455-c3a389f83636
---
 proof/pq2/src/pq2actions.cxx                 |  6 ++-
 proof/pq2/src/pq2main.cxx                    |  2 +-
 proof/pq2/src/pq2ping.cxx                    | 17 ++++++-
 proof/proof/src/TDataSetManager.cxx          |  3 +-
 proof/proof/src/TProof.cxx                   |  4 +-
 proof/proof/src/TProofMgr.cxx                | 15 +++++-
 proof/proof/src/TProofServ.cxx               | 53 +++++++++++++-------
 proof/proof/src/TProofServLite.cxx           | 11 ++--
 proof/proofplayer/src/TProofDraw.cxx         |  6 +--
 proof/proofplayer/src/TVirtualPacketizer.cxx |  4 +-
 10 files changed, 84 insertions(+), 37 deletions(-)

diff --git a/proof/pq2/src/pq2actions.cxx b/proof/pq2/src/pq2actions.cxx
index f4fb9f0f32b..e4fad501cf6 100644
--- a/proof/pq2/src/pq2actions.cxx
+++ b/proof/pq2/src/pq2actions.cxx
@@ -588,7 +588,8 @@ void do_anadist(const char *ds, const char *servers, const char *ignsrvs,
       // Name
       TString cname(ds);
       if (cname.BeginsWith("/")) cname.Remove(0,1);
-      if (cname.EndsWith("/")) cname.Remove(cname.Last('/'));
+      Ssiz_t ilst = kNPOS;
+      if (cname.EndsWith("/") && (ilst = cname.Last('/')) != kNPOS) cname.Remove(ilst);
       cname.ReplaceAll("/", "-");
       cname.ReplaceAll("*", "-star-");
       distinfo.SetName(cname);
@@ -1003,7 +1004,8 @@ int do_anadist_ds(TFileCollection *fc, const char *servers, const char *ignsrvs,
             u.SetOptions("");
             TString php(u.GetUrl());
             php.Remove(php.Index(u.GetFile()));
-            if (php.EndsWith("/")) php.Remove(php.Last('/'));
+            Ssiz_t ilst = kNPOS;
+            if (php.EndsWith("/") && ((ilst = php.Last('/')) != kNPOS)) php.Remove(ilst);
             if (fout) {
                fprintf(fout,"%s %s %s\n", u.GetFile(), php.Data(), fcd->GetName());
             } else {
diff --git a/proof/pq2/src/pq2main.cxx b/proof/pq2/src/pq2main.cxx
index cd97cba7169..f3b355e7a7c 100644
--- a/proof/pq2/src/pq2main.cxx
+++ b/proof/pq2/src/pq2main.cxx
@@ -324,7 +324,7 @@ int main(int argc, char **argv)
    }
    catch (std::exception& exc) {
       Printf("Standard exception caught: we abort whatever it is ...");
-      throw;
+      throw exc;
    }
    catch (const char *str) {
       Printf("Exception thrown: %s", str);
diff --git a/proof/pq2/src/pq2ping.cxx b/proof/pq2/src/pq2ping.cxx
index 360f5ca6315..b7b9d2c51ba 100644
--- a/proof/pq2/src/pq2ping.cxx
+++ b/proof/pq2/src/pq2ping.cxx
@@ -200,16 +200,29 @@ Int_t pingXproofdAt()
       return -1;
    }
    // Send the first bytes
+   int writeCount = -1;
    clnt_HS_t initHS;
    memset(&initHS, 0, sizeof(initHS));
    initHS.third  = (int)host2net((int)1);
    int len = sizeof(initHS);
-   s.SendRaw(&initHS, len);
+   writeCount = s.SendRaw(&initHS, len);
+   if (writeCount != len) {
+      if (gDebug > 0)
+         Printf("pingXproofdAt: 1st: wrong number of bytes sent: %d (expected: %d)",
+                writeCount, len);
+      return 1;
+   }
    // These 8 bytes are need by 'proofd' and discarded by XPD
    int dum[2];
    dum[0] = (int)host2net((int)4);
    dum[1] = (int)host2net((int)2012);
-   s.SendRaw(&dum[0], sizeof(dum));
+   writeCount = s.SendRaw(&dum[0], sizeof(dum));
+   if (writeCount != sizeof(dum)) {
+      if (gDebug > 0)
+         Printf("pingXproofdAt: 2nd: wrong number of bytes sent: %d (expected: %d)",
+                writeCount, (int) sizeof(dum));
+      return 1;
+   }
    // Read first server response
    int type;
    len = sizeof(type);
diff --git a/proof/proof/src/TDataSetManager.cxx b/proof/proof/src/TDataSetManager.cxx
index 61116f4f1ef..70ba1429a32 100644
--- a/proof/proof/src/TDataSetManager.cxx
+++ b/proof/proof/src/TDataSetManager.cxx
@@ -702,7 +702,8 @@ void TDataSetManager::MonitorUsedSpace(TVirtualMonitoringWriter *monitoring)
          list->Add(new TParameter<Long64_t>(user->String().Data(), size2->GetVal()));
       }
 
-      monitoring->SendParameters(list, group->String());
+      if (!monitoring->SendParameters(list, group->String()))
+         Warning("MonitorUsedSpace", "problems sending monitoring parameters");
       delete list;
    }
 }
diff --git a/proof/proof/src/TProof.cxx b/proof/proof/src/TProof.cxx
index fa478343d7b..45025978936 100644
--- a/proof/proof/src/TProof.cxx
+++ b/proof/proof/src/TProof.cxx
@@ -5629,8 +5629,8 @@ Int_t TProof::SendFile(const char *file, Int_t opt, const char *rfile, TSlave *w
    }
 
    // Get info about the file
-   Long64_t size;
-   Long_t id, flags, modtime;
+   Long64_t size = -1;
+   Long_t id, flags, modtime = 0;
    if (gSystem->GetPathInfo(file, &id, &size, &flags, &modtime) == 1) {
       Error("SendFile", "cannot stat file %s", file);
       return -1;
diff --git a/proof/proof/src/TProofMgr.cxx b/proof/proof/src/TProofMgr.cxx
index 48f028d19b5..7cb0bc225d8 100644
--- a/proof/proof/src/TProofMgr.cxx
+++ b/proof/proof/src/TProofMgr.cxx
@@ -649,16 +649,27 @@ Int_t TProofMgr::Ping(const char *url)
       return -1;
    }
    // Send the first bytes
+   int writeCount = -1;
    clnt_HS_t initHS;
    memset(&initHS, 0, sizeof(initHS));
    initHS.third  = (int)host2net((int)1);
    int len = sizeof(initHS);
-   s.SendRaw(&initHS, len);
+   if ((writeCount = s.SendRaw(&initHS, len)) != len) {
+      if (gDebug > 0)
+         ::Info("TProofMgr::Ping", "1st: wrong number of bytes sent: %d (expected: %d)",
+                                   writeCount, len);
+      return 1;
+   }
    // These 8 bytes are need by 'proofd' and discarded by XPD
    int dum[2];
    dum[0] = (int)host2net((int)4);
    dum[1] = (int)host2net((int)2012);
-   s.SendRaw(&dum[0], sizeof(dum));
+   if ((writeCount = s.SendRaw(&dum[0], sizeof(dum))) !=  sizeof(dum)) {
+      if (gDebug > 0)
+         ::Info("TProofMgr::Ping", "2nd: wrong number of bytes sent: %d (expected: %d)",
+                                   writeCount, (int) sizeof(dum));
+      return 1;
+   }
    // Read first server response
    int type;
    len = sizeof(type);
diff --git a/proof/proof/src/TProofServ.cxx b/proof/proof/src/TProofServ.cxx
index 43690bc076c..be0bedbaefc 100644
--- a/proof/proof/src/TProofServ.cxx
+++ b/proof/proof/src/TProofServ.cxx
@@ -1020,7 +1020,7 @@ Int_t TProofServ::CatMotd()
    lastname = TString(GetWorkDir()) + "/.prooflast";
    char *last = gSystem->ExpandPathName(lastname.Data());
    Long64_t size;
-   Long_t id, flags, modtime, lasttime;
+   Long_t id, flags, modtime, lasttime = 0;
    if (gSystem->GetPathInfo(last, &id, &size, &flags, &lasttime) == 1)
       lasttime = 0;
 
@@ -1065,7 +1065,10 @@ TObject *TProofServ::Get(const char *namecycle)
    // This method is called by TDirectory::Get() in case the object can not
    // be found locally.
 
-   fSocket->Send(namecycle, kPROOF_GETOBJECT);
+   if (fSocket->Send(namecycle, kPROOF_GETOBJECT) < 0) {
+      Error("Get", "problems sending request");
+      return (TObject *)0;
+   }
 
    TObject *idcur = 0;
 
@@ -1535,7 +1538,8 @@ Int_t TProofServ::HandleSocketInput(TMessage *mess, Bool_t all)
       case kPROOF_STATUS:
          Warning("HandleSocketInput:kPROOF_STATUS",
                "kPROOF_STATUS message is obsolete");
-         fSocket->Send(fProof->GetParallel(), kPROOF_STATUS);
+         if (fSocket->Send(fProof->GetParallel(), kPROOF_STATUS) < 0)
+            Warning("HandleSocketInput:kPROOF_STATUS", "problem sending of request");
          break;
 
       case kPROOF_GETSTATS:
@@ -2100,7 +2104,10 @@ Bool_t TProofServ::AcceptResults(Int_t connections, TVirtualProofPlayer *mergerP
          if (++numworkers >= connections)
             fMergingMonitor->Remove(fMergingSocket);
       } else {
-         s->Recv(mess);
+         if (s->Recv(mess) < 0) {
+            Error("AcceptResults", "problems receiving message");
+            continue;
+         }
          PDB(kSubmerger, 2)
             Info("AcceptResults", "message received: %d ", (mess ? mess->What() : 0));
          if (!mess) {
@@ -2459,7 +2466,8 @@ Int_t TProofServ::ReceiveFile(const char *file, Bool_t bin, Long64_t size)
 
    close(fd);
 
-   chmod(file, 0644);
+   if (chmod(file, 0644) != 0)
+      Warning("ReceiveFile", "error setting mode 0644 on file %s", file);
 
    return 0;
 }
@@ -3070,16 +3078,22 @@ Int_t TProofServ::SetupCommon()
       if (!dsms.IsNull()) {
          TString dsm;
          Int_t from  = 0;
-         dsms.Tokenize(dsm, from, ",");
-         // Get plugin manager to load the appropriate TDataSetManager
-         if (gROOT->GetPluginManager()) {
-            // Find the appropriate handler
-            h = gROOT->GetPluginManager()->FindHandler("TDataSetManager", dsm);
-            if (h && h->LoadPlugin() != -1) {
-               // make instance of the dataset manager
-               fDataSetManager =
-                  reinterpret_cast<TDataSetManager*>(h->ExecPlugin(3, fGroup.Data(),
-                                                          fUser.Data(), dsm.Data()));
+         while (dsms.Tokenize(dsm, from, ",")) {
+            if (fDataSetManager && !fDataSetManager->TestBit(TObject::kInvalidObject)) {
+               Warning("SetupCommon", "a valid dataset manager already initialized");
+               Warning("SetupCommon", "support for multiple managers not yet available");
+               break;
+            }
+            // Get plugin manager to load the appropriate TDataSetManager
+            if (gROOT->GetPluginManager()) {
+               // Find the appropriate handler
+               h = gROOT->GetPluginManager()->FindHandler("TDataSetManager", dsm);
+               if (h && h->LoadPlugin() != -1) {
+                  // make instance of the dataset manager
+                  fDataSetManager =
+                     reinterpret_cast<TDataSetManager*>(h->ExecPlugin(3, fGroup.Data(),
+                                                            fUser.Data(), dsm.Data()));
+               }
             }
          }
          // Check the result of the dataset manager initialization
@@ -4816,7 +4830,10 @@ void TProofServ::HandleCheckFile(TMessage *mess, TString *slb)
             Info("HandleCheckFile",
                  "package %s already on node", filenam.Data());
          if (IsMaster())
-            fProof->UploadPackage(fPackageDir + "/" + filenam);
+            if (fProof->UploadPackage(fPackageDir + "/" + filenam) != 0)
+               Info("HandleCheckFile",
+                    "problems with uploading package %s", filenam.Data());
+               
       } else {
          reply << (Int_t)0;
          if (fProtocol <= 19) reply.Reset(kPROOF_FATAL);
@@ -4843,7 +4860,9 @@ void TProofServ::HandleCheckFile(TMessage *mess, TString *slb)
             Info("HandleCheckFile",
                  "package %s already on node", filenam.Data());
          if (IsMaster())
-            fProof->UploadPackage(fPackageDir + "/" + filenam);
+            if (fProof->UploadPackage(fPackageDir + "/" + filenam) != 0)
+               Info("HandleCheckFile",
+                    "problems with uploading package %s", filenam.Data());
       } else {
          reply << (Int_t)0;
          if (fProtocol <= 19) reply.Reset(kPROOF_FATAL);
diff --git a/proof/proof/src/TProofServLite.cxx b/proof/proof/src/TProofServLite.cxx
index 1223f4965af..c71395f9db4 100644
--- a/proof/proof/src/TProofServLite.cxx
+++ b/proof/proof/src/TProofServLite.cxx
@@ -526,11 +526,12 @@ Int_t TProofServLite::SetupOnFork(const char *ord)
       while ((e = gSystem->GetDirEntry(dirp))) {
          ent.Form("%s/%s", sdir.Data(), e);
          FileStat_t st;
-         gSystem->GetPathInfo(ent.Data(), st);
-         if (st.fIsLink && ent.Contains(sord)) {
-            PDB(kGlobal, 1)
-               Info("SetupOnFork","unlinking: %s", ent.Data());
-            gSystem->Unlink(ent);
+         if (gSystem->GetPathInfo(ent.Data(), st) == 0) {
+            if (st.fIsLink && ent.Contains(sord)) {
+               PDB(kGlobal, 1)
+                  Info("SetupOnFork","unlinking: %s", ent.Data());
+               gSystem->Unlink(ent);
+            }
          }
       }
       gSystem->FreeDirectory(dirp);
diff --git a/proof/proofplayer/src/TProofDraw.cxx b/proof/proofplayer/src/TProofDraw.cxx
index 8657424b3c9..f75b932d5a5 100644
--- a/proof/proofplayer/src/TProofDraw.cxx
+++ b/proof/proofplayer/src/TProofDraw.cxx
@@ -1636,7 +1636,7 @@ void TProofDrawGraph::Terminate(void)
       }
       if (!hist->TestBit(kCanDelete)) {
          for (int i = 0; i < fGraph->GetN(); i++) {
-            Double_t x, y;
+            Double_t x = 0, y = 0;
             fGraph->GetPoint(i, x, y);
             hist->Fill(x, y, 1);
          }
@@ -1780,7 +1780,7 @@ void TProofDrawPolyMarker3D::Terminate(void)
             fPolyMarker3D->GetPoint(0, rmax[0], rmax[1], rmax[2]);
          }
          for (int i = 1; i < fPolyMarker3D->Size(); i++) {
-            Double_t v[3];
+            Double_t v[3] = {0};
             fPolyMarker3D->GetPoint(i, v[0], v[1], v[2]);
             for (int ii = 0; ii < 3; ii++) {
                if (v[ii] < rmin[ii]) rmin[ii] = v[ii];
@@ -1827,7 +1827,7 @@ void TProofDrawPolyMarker3D::Terminate(void)
       gPad->Update();
       if (!hist->TestBit(kCanDelete)) {
          for (int i = 0; i < fPolyMarker3D->Size(); i++) {
-            Float_t x, y, z;
+            Double_t x = 0, y = 0, z = 0;
             fPolyMarker3D->GetPoint(i, x, y, z);
             hist->Fill(x, y, z, 1);
          }
diff --git a/proof/proofplayer/src/TVirtualPacketizer.cxx b/proof/proofplayer/src/TVirtualPacketizer.cxx
index 9e114073d34..d8d5a590a37 100644
--- a/proof/proofplayer/src/TVirtualPacketizer.cxx
+++ b/proof/proofplayer/src/TVirtualPacketizer.cxx
@@ -150,8 +150,8 @@ TVirtualPacketizer::TVirtualPacketizer(TList *input, TProofProgressStatus *st)
 
    // Whether to send estimated values for the progress info
    TString estopt;
-   TProof::GetParameter(input, "PROOF_RateEstimation", estopt);
-   if (estopt.IsNull()) {
+   if (TProof::GetParameter(input, "PROOF_RateEstimation", estopt) != 0 || 
+       estopt.IsNull()) {
       // Parse option from the env
       estopt = gEnv->GetValue("Proof.RateEstimation", "");
    }
-- 
GitLab