Skip to content

Segfault when providing a very small initial block_size to constructor of memory pool #113

@delins

Description

@delins

Even though it doesn't make much sense to do so, if you provide a very small initial block_size argument to memory_pool's constructor, it segfaults. This stripped down version of the example in README.md crashes for me:

#include <iostream>

#include <foonathan/memory/container.hpp> // vector, list, list_node_size
#include <foonathan/memory/memory_pool.hpp> // memory_pool

// alias namespace foonathan::memory as memory for easier access
#include <foonathan/memory/namespace_alias.hpp>


int main()
{
    using namespace memory::literals;

    // a memory pool RawAllocator
    // allocates a memory block - initially 4KiB - and splits it into chunks of list_node_size<int>::value big
    // list_node_size<int>::value is the size of each node of a std::list
    memory::memory_pool<> pool(memory::list_node_size<int>::value, 39); // <- problem here

    // just an alias for std::list<int, memory::std_allocator<int, memory::memory_pool<>>
    // a std::list using a memory_pool
    // std_allocator stores a reference to a RawAllocator and provides the Allocator interface
    memory::list<int, memory::memory_pool<>> list(pool);
    list.push_back(3);
    list.push_back(2);
    list.push_back(1);

    for (auto e : list)
        std::cout << e << ' ';
    std::cout << '\n';
}

If I change the 39 to 40 it's all fine. It seems to be related to the node_size argument: to larger node_size is, the larger block_size needs to be for it not to crash.

Tested with:

  • GCC 9.3.0
  • Ubuntu 20.04.2
  • Ryzen 3950X
  • foonathan/memory built in release mode on the same system, commit b4caa03.

When I compiled foonathan/memory in debug mode, the problem is caught in an assert and the following error is thrown:
[foonathan::memory] Assertion failure in function insert_impl (<path>/memory/src/detail/free_list.cpp:527): Assertion "no_nodes > 0" failed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions