From 7e8afa56b596a55c7e32f0a1bd46de1c8089cd44 Mon Sep 17 00:00:00 2001
From: Bertrand Bellenot <bertrand.bellenot@cern.ch>
Date: Fri, 5 Jun 2015 12:17:10 +0200
Subject: [PATCH] Fix several Coverity issues

Fix the following Coverity issues:
CID 61230 (Unchecked return value)
CID 56101 (Uninitialized scalar field)
CID 55010, 55005 (Dereference after null check)
CID 55004 (Explicit null dereferenced)
CID 54807 (Same on both sides)
CID 48060 (Dereference null return value)
---
 gui/gui/src/TGColorDialog.cxx    | 6 ++++--
 gui/gui/src/TGFileBrowser.cxx    | 3 ++-
 gui/gui/src/TGFont.cxx           | 8 +++++---
 gui/gui/src/TRootBrowserLite.cxx | 3 ++-
 gui/recorder/src/TRecorder.cxx   | 3 ++-
 test/stressGUI.cxx               | 5 +++--
 6 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/gui/gui/src/TGColorDialog.cxx b/gui/gui/src/TGColorDialog.cxx
index ac430b7ed76..7e81740af55 100644
--- a/gui/gui/src/TGColorDialog.cxx
+++ b/gui/gui/src/TGColorDialog.cxx
@@ -1476,8 +1476,10 @@ Bool_t TGColorDialog::ProcessMessage(Long_t msg, Long_t parm1, Long_t /*parm2*/)
                      *fRetColor = TColor::RGB2Pixel(atoi(fRtb->GetString()),
                                                     atoi(fGtb->GetString()),
                                                     atoi(fBtb->GetString()));
-                     if ((fRetTColor = gROOT->GetColor(TColor::GetColor(*fRetColor)))) {};
-                     fRetTColor->SetAlpha(TMath::Max((Double_t)0, TMath::Min((Double_t)1, atof(fAlb->GetString()))));
+                     if ((fRetTColor = gROOT->GetColor(TColor::GetColor(*fRetColor)))) {
+                        fRetTColor->SetAlpha(TMath::Max((Double_t)0, TMath::Min((Double_t)1,
+                                             atof(fAlb->GetString()))));
+                     }
                      CloseWindow();
                      break;
                   case kCDLG_CANCEL:
diff --git a/gui/gui/src/TGFileBrowser.cxx b/gui/gui/src/TGFileBrowser.cxx
index be4d07afa97..8d0a750733d 100644
--- a/gui/gui/src/TGFileBrowser.cxx
+++ b/gui/gui/src/TGFileBrowser.cxx
@@ -1318,7 +1318,8 @@ void TGFileBrowser::DoubleClicked(TGListTreeItem *item, Int_t /*btn*/)
                }
             }
          }
-         else if (obj->InheritsFrom("TCanvas") &&
+         else if (obj->InheritsFrom("TCanvas") && fNewBrowser &&
+                  fNewBrowser->GetTabRight() &&
                   fNewBrowser->GetTabRight()->GetTabTab(obj->GetName())) {
             // avoid potential crash when drawing a canvas with the same name
             // than a canvas already embedded in one of the browser's tab
diff --git a/gui/gui/src/TGFont.cxx b/gui/gui/src/TGFont.cxx
index 4560acf22ef..3848f811229 100644
--- a/gui/gui/src/TGFont.cxx
+++ b/gui/gui/src/TGFont.cxx
@@ -727,14 +727,15 @@ TGTextLayout *TGFont::ComputeTextLayout(const char *string, Int_t numChars,
          }
       }
       if ((start == special) && (special < end)) {
-
          // Handle the special character.
+         LayoutChunk_t *newchunk = 0;
 
          chunk = 0;
          if (*special == '\t') {
             newX = curX + fTabWidth;
             newX -= newX % fTabWidth;
-            NewChunk(layout, &maxChunks, start, 1, curX, newX, baseline)->fNumDisplayChars = -1;
+            newchunk = NewChunk(layout, &maxChunks, start, 1, curX, newX, baseline);
+            if (newchunk) newchunk->fNumDisplayChars = -1;
             start++;
             if ((start < end) && ((wrapLength <= 0) || (newX <= wrapLength))) {
 
@@ -745,7 +746,8 @@ TGTextLayout *TGFont::ComputeTextLayout(const char *string, Int_t numChars,
                continue;
             }
          } else {
-            NewChunk(layout, &maxChunks, start, 1, curX, 1000000000, baseline)->fNumDisplayChars = -1;
+            newchunk = NewChunk(layout, &maxChunks, start, 1, curX, 1000000000, baseline);
+            if (newchunk) newchunk->fNumDisplayChars = -1;
             start++;
             goto wrapLine;
          }
diff --git a/gui/gui/src/TRootBrowserLite.cxx b/gui/gui/src/TRootBrowserLite.cxx
index dc99dffd704..312ab7002bd 100644
--- a/gui/gui/src/TRootBrowserLite.cxx
+++ b/gui/gui/src/TRootBrowserLite.cxx
@@ -3048,7 +3048,8 @@ void TRootBrowserLite::BrowseTextFile(const char *file)
       fTextEdit = new TGTextEdit(fV2, fV2->GetWidth(), fV2->GetHeight(),
                                  kSunkenFrame | kDoubleBorder);
       TColor *col = gROOT->GetColor(19);
-      fTextEdit->SetBackgroundColor(col->GetPixel());
+      if (col)
+         fTextEdit->SetBackgroundColor(col->GetPixel());
       if (TGSearchDialog::SearchDialog()) {
          TGSearchDialog::SearchDialog()->Connect("TextEntered(char *)", "TGTextEdit",
                                                  fTextEdit, "Search(char *,Bool_t,Bool_t)");
diff --git a/gui/recorder/src/TRecorder.cxx b/gui/recorder/src/TRecorder.cxx
index 88be222256c..90182a411e9 100644
--- a/gui/recorder/src/TRecorder.cxx
+++ b/gui/recorder/src/TRecorder.cxx
@@ -1205,6 +1205,7 @@ TRecorderRecording::TRecorderRecording(TRecorder *r, const char *filename,
    // What is allocated here is deleted in destructor
 
    fRecorder = r;
+   fBeginPave = 0;
 
    // Remember window IDs of GUI recorder (appropriate events are
    // filtered = not recorded)
@@ -1725,7 +1726,7 @@ void TRecorderRecording::SetTypeOfConfigureNotify(Event_t *e)
    // On both platforms, we mark the events matching the criteria
    // (automatically generated in ROOT) as events that should be filtered
    // when replaying (TRecGuiEvent::kCNFilter)
-   if ((e->fX == 0 && e->fX == 0)) { // || e->fFormat == 32 ) {
+   if ((e->fX == 0 && e->fY == 0)) { // || e->fFormat == 32 ) {
       e->fUser[4] = TRecGuiEvent::kCNFilter;
       return;
    }
diff --git a/test/stressGUI.cxx b/test/stressGUI.cxx
index 9c12e112b33..5ebf751d277 100644
--- a/test/stressGUI.cxx
+++ b/test/stressGUI.cxx
@@ -360,8 +360,9 @@ Int_t FileSize(const char *filename)
    // Return the size of the file "filename".
 
    FileStat_t fs;
-   gSystem->GetPathInfo(filename, fs);
-   return (Int_t)fs.fSize;
+   if (gSystem->GetPathInfo(filename, fs) == 0)
+      return (Int_t)fs.fSize;
+   return 0;
 }
 
 //______________________________________________________________________________
-- 
GitLab