From d2e0c7a5392c93306d95b4b4fad4aefe91094234 Mon Sep 17 00:00:00 2001
From: Bertrand Bellenot <bertrand.bellenot@cern.ch>
Date: Wed, 22 Mar 2017 16:50:03 +0100
Subject: [PATCH] Fix Jira issues #ROOT-8722 and ROOT-8723

Add a few tests like "if (obj->IsA() != TClass::Class())" to prevent casting a TClass object to its class type.
For example: if (obj->InheritsFrom("TDirectory")) returns true in the case of obj being a TClass (of a TDirectory class); then, calling ((TDirectory *)obj)->GetListOfKeys() fails (crash or random behavior)
---
 gui/gui/src/TGFileBrowser.cxx | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/gui/gui/src/TGFileBrowser.cxx b/gui/gui/src/TGFileBrowser.cxx
index d6b6391fbbf..6663dd41508 100644
--- a/gui/gui/src/TGFileBrowser.cxx
+++ b/gui/gui/src/TGFileBrowser.cxx
@@ -1053,16 +1053,18 @@ void TGFileBrowser::Clicked(TGListTreeItem *item, Int_t btn, Int_t x, Int_t y)
       }
    }
    fListTree->ClearViewPort();
-   if (selected && selected->InheritsFrom("TLeaf"))
-      selected = (TObject *)gROOT->ProcessLine(TString::Format("((TLeaf *)0x%lx)->GetBranch()->GetTree();", (ULong_t)selected));
-   if (selected && selected->InheritsFrom("TBranch"))
-      selected = (TObject *)gROOT->ProcessLine(TString::Format("((TBranch *)0x%lx)->GetTree();", (ULong_t)selected));
-   if (selected && selected->InheritsFrom("TTree")) {
-      // if a tree not attached to any directory (e.g. in a TFolder)
-      // then attach it to the current directory (gDirectory)
-      TDirectory *tdir = (TDirectory *)gROOT->ProcessLine(TString::Format("((TTree *)0x%lx)->GetDirectory();", (ULong_t)selected));
-      if (!tdir) {
-         gROOT->ProcessLine(TString::Format("((TTree *)0x%lx)->SetDirectory(gDirectory);", (ULong_t)selected));
+   if (selected && (selected->IsA() != TClass::Class())) {
+      if (selected->InheritsFrom("TLeaf"))
+         selected = (TObject *)gROOT->ProcessLine(TString::Format("((TLeaf *)0x%lx)->GetBranch()->GetTree();", (ULong_t)selected));
+      if (selected->InheritsFrom("TBranch"))
+         selected = (TObject *)gROOT->ProcessLine(TString::Format("((TBranch *)0x%lx)->GetTree();", (ULong_t)selected));
+      if (selected->InheritsFrom("TTree")) {
+         // if a tree not attached to any directory (e.g. in a TFolder)
+         // then attach it to the current directory (gDirectory)
+         TDirectory *tdir = (TDirectory *)gROOT->ProcessLine(TString::Format("((TTree *)0x%lx)->GetDirectory();", (ULong_t)selected));
+         if (!tdir) {
+            gROOT->ProcessLine(TString::Format("((TTree *)0x%lx)->SetDirectory(gDirectory);", (ULong_t)selected));
+         }
       }
    }
    if (selected && gPad && IsObjectEditable(selected->IsA())) {
@@ -1220,7 +1222,7 @@ void TGFileBrowser::DoubleClicked(TGListTreeItem *item, Int_t /*btn*/)
    TObject *obj = (TObject *) item->GetUserData();
    if (obj && !obj->InheritsFrom("TSystemFile")) {
       TString ext = obj->GetName();
-      if (obj->InheritsFrom("TDirectory")) {
+      if (obj->InheritsFrom("TDirectory") && (obj->IsA() != TClass::Class())) {
          if (((TDirectory *)obj)->GetListOfKeys())
             fNKeys = ((TDirectory *)obj)->GetListOfKeys()->GetEntries();
          else
@@ -1246,8 +1248,7 @@ void TGFileBrowser::DoubleClicked(TGListTreeItem *item, Int_t /*btn*/)
             }
          }
       }
-      else if (obj->InheritsFrom("TLeaf") ||
-          obj->InheritsFrom("TBranch")) {
+      else if (obj->InheritsFrom("TLeaf") || obj->InheritsFrom("TBranch")) {
          Chdir(item);
       }
       else if (obj->InheritsFrom("TRemoteObject")) {
-- 
GitLab