From bbd3df2cc30c1215ab6f307526cb8871020871b1 Mon Sep 17 00:00:00 2001
From: Philippe Canal <pcanal@fnal.gov>
Date: Fri, 2 Mar 2012 04:42:38 +0000
Subject: [PATCH] Fix cov 36308 (deref null return)

git-svn-id: http://root.cern.ch/svn/root/trunk@43209 27541ba8-7e3a-0410-8455-c3a389f83636
---
 tree/tree/src/TTree.cxx | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/tree/tree/src/TTree.cxx b/tree/tree/src/TTree.cxx
index 0d02a559e26..3b3f107698f 100644
--- a/tree/tree/src/TTree.cxx
+++ b/tree/tree/src/TTree.cxx
@@ -2490,7 +2490,11 @@ TFile* TTree::ChangeFile(TFile* file)
    }
    Int_t compress = file->GetCompressionSettings();
    TFile* newfile = TFile::Open(fname, "recreate", "chain files", compress);
-   Printf("Fill: Switching to new file: %s", fname);
+   if (newfile == 0) {
+      Error("Fill: Failed to open new file %s, continuing as a memory tree.",fname); 
+   } else {
+      Printf("Fill: Switching to new file: %s", fname);
+   }
    // The current directory may contain histograms and trees.
    // These objects must be moved to the new file.
    TBranch* branch = 0;
@@ -2521,7 +2525,7 @@ TFile* TTree::ChangeFile(TFile* file)
          continue;
       }
       // Not a TH1 or a TTree, move object to new file.
-      newfile->Append(obj);
+      if (newfile) newfile->Append(obj);
       file->Remove(obj);
    }
    delete file;
@@ -5718,6 +5722,8 @@ TTree* TTree::MergeTrees(TList* li, Option_t* /* option */)
          
          // Once the cloning is done, separate the trees,
          // to avoid as many side-effects as possible
+         // The list of clones is guaranteed to exist since we
+         // just cloned the tree.
          tree->GetListOfClones()->Remove(newtree);
          tree->ResetBranchAddresses();
          newtree->ResetBranchAddresses();
-- 
GitLab