diff --git a/python/templates/CollectionData.cc.jinja2 b/python/templates/CollectionData.cc.jinja2 index 6561d744a..a84710d16 100644 --- a/python/templates/CollectionData.cc.jinja2 +++ b/python/templates/CollectionData.cc.jinja2 @@ -38,6 +38,21 @@ if (!isSubsetColl) { m_data.reset(buffers.dataAsVector<{{ class.full_type }}Data>()); + // The following is ugly code for the case when reading garbage data from ROOT + // after calling dataAsVector, which can trigger infinite loops that consume + // all the available memory and this works at least for GCC 15 and Clang 20 in + // {Debug,RelWithDebInfo,Release} modes. + // https://github.com/AIDASoft/podio/pull/817#issuecomment-3266748609 and + // https://github.com/AIDASoft/podio/pull/842 + volatile std::uint64_t s = m_data->size(); + if (s > 1e15) throw std::runtime_error("Bad data after reading: a collection is too big"); + else + if (s == 0) + for ([[maybe_unused]] const auto& _ : *m_data.get()) + throw std::runtime_error("Bad data after reading: zero-sized collection with data"); + // end of ugly + + {% for member in VectorMembers %} m_vec_{{ member.name }}.reset(podio::CollectionReadBuffers::asVector<{{ member.full_type }}>(m_vecmem_info[{{ loop.index0 }}].second)); {% endfor %}