From 2dc2f13e31cc8c1cfee6436cfc33d59a4984d810 Mon Sep 17 00:00:00 2001
From: scott snyder <snyder@bnl.gov>
Date: Mon, 30 Apr 2018 15:53:17 +0200
Subject: [PATCH] PyUtils: Fix length inconsistency in SetSize call.

The pythonization of TFile::root was calling the SetSize method
on a PyROOT buffer object.  The argument that SetSize takes is the
number of elements in the buffer, where the size of each element
depends on how the buffer was created.  Here, however, we were
passing to SetSize a number of bytes.

The buffer elements we were seeing were void*, which have size 8.
So the buffer size was getting misset by a factor of 8.
This was leading to a crash in the input file peeker.

This crash was however only observed in the muRunData test of TrigUpgradeTest,
and only when it was run with --stdcmalloc and MALLOC_CHECK_=3.
It's a bit a mystery why this apparently long-standing bug hasn't
caused problems before.



Former-commit-id: 5f553898f3b4118e8ce47eb78932349b750e2054
---
 Tools/PyUtils/python/RootUtils.py | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/Tools/PyUtils/python/RootUtils.py b/Tools/PyUtils/python/RootUtils.py
index a4c27788737..f1028f5fe62 100644
--- a/Tools/PyUtils/python/RootUtils.py
+++ b/Tools/PyUtils/python/RootUtils.py
@@ -21,10 +21,18 @@ import os
 import sys
 import re
 from pprint import pprint
+from array import array
 
 from .Decorators import memoize
 
 ### functions -----------------------------------------------------------------
+# Set buffer size, in bytes.
+# The argument to SetSize is in elements, not bytes.
+def _set_byte_size (buf, sz):
+    eltsz = array(buf.typecode).itemsize
+    buf.SetSize (sz / eltsz)
+    return
+
 def import_root(batch=True):
     """a helper method to wrap the 'import ROOT' statement to prevent ROOT
     from screwing up the display or loading graphics libraries when in batch
@@ -149,7 +157,7 @@ def _pythonize_tfile():
                 #self.seek(c_buf.sz+self.tell())
                 #print "-->2",self.tell()
                 buf = c_buf.buffer()
-                buf.SetSize(c_buf.sz)
+                _set_byte_size (buf, c_buf.sz)
                 return str(buf[:])
             return ''
         else:
@@ -160,7 +168,7 @@ def _pythonize_tfile():
                 c_buf = read_root_file(self, size)
                 if c_buf and c_buf.sz:
                     buf = c_buf.buffer()
-                    buf.SetSize(c_buf.sz)
+                    _set_byte_size (buf, c_buf.sz)
                     out.append(str(buf[:]))
                 else:
                     break
-- 
GitLab