boostorg / histogram

Fast multi-dimensional generalized histogram with convenient interface for C++14
Boost Software License 1.0
313 stars 74 forks source link

Sum segfault when mixing empty growable axes with other axes #353

Closed henryiii closed 1 year ago

henryiii commented 2 years ago

If you mix an empty categorical axes with a fairly normal axis (one that is not another similar categorical axes either empty or not - that seems to be okay), you get a segfault when trying to check the inner sum.

Discovery via default repr in Hist in https://github.com/scikit-hep/hist/issues/337, also affects boost-histogram (both have the same default repr) and sums on Boost.Histogram.

#include <boost/histogram.hpp>
#include <boost/histogram/accumulators/ostream.hpp>
#include <iostream>

namespace bh = boost::histogram;

using category_str_growth
    = bh::axis::category<std::string, bh::use_default, bh::axis::option::growth_t>;

int main() {
    auto h = bh::make_weighted_histogram(bh::axis::regular{10,0,1}, category_str_growth{});

    std::cout << bh::algorithm::sum(h, bh::coverage::all) << std::endl;  // OK
    std::cout << bh::algorithm::sum(h, bh::coverage::inner) << std::endl; // Crash

    return 0;
}
henryiii commented 2 years ago

Interesting, Assertion failed: (iter_ < indices_.hist_->end()), function operator++, file indexed.hpp, line 258. is produced when you try to do an inner sum over this if you remove the growth on the category. Ideally this should return 0 in the correct (storage dependent) type, but the assertion is better than the segfault.

HDembinski commented 1 year ago

indexed iteration is broken when the last axis has size 0. Work on fixing this started in #356