You need to enable JavaScript to run this app. GCC编译时带状态自定义分配器的list调用sort触发abort的问题
优惠活动
大模型
产品
解决方案
定价
更多

GCC编译时带状态自定义分配器的list调用sort触发abort的问题

问题概述
  • 相同代码在VC编译环境下可正常运行,在GCC 7.5至10.2版本的Linux平台编译运行时,会在执行list.sort阶段触发abort中断。
  • 中断触发逻辑:list.sort内部调用splice方法时,会按照合规逻辑校验两个分配器实例的相等性,但校验发现两个分配器不匹配——一个是用户传入list构造函数、携带自定义状态的分配器实例,另一个是STL内部通过默认构造生成的无状态分配器实例。
核心技术问询

C标准库实现选择通过分配器默认构造函数生成实例,而非复用用户传入的、用于节点分配的分配器实例,或是通过拷贝构造生成与用户实例状态一致的分配器,该行为是否符合C语言规范要求?

补充信息:该问题是libstdc++已知的66742号缺陷。

复现代码
#include <iostream>
#include <list>
#include <cassert>

using namespace std;

template <typename T>
class my_allocator
{
public:
    using value_type = typename std::remove_const<T>::type;
    using pointer = value_type*;
    using size_type = size_t;

    typedef std::true_type propagate_on_container_copy_assignment;
    typedef std::true_type propagate_on_container_move_assignment;
    typedef std::true_type propagate_on_container_swap;

    template<typename U>
    struct rebind {
        typedef my_allocator<U> other;
    };

    my_allocator() : m_internalState(0)
    {
        cout << "my_allocator(): internal state=" << m_internalState << endl;
    }

    my_allocator(const void* pAllocator) : m_internalState(pAllocator)
    {
        cout << "my_allocator(const void * pAllocator): internal state=" << m_internalState << endl;
    }

    template <typename U>
    my_allocator(const my_allocator<U>& myAllocator) noexcept : m_internalState(myAllocator.m_internalState)
    {
        cout << "my_allocator(const my_allocator<U>& myAllocator): internal state=" << m_internalState << endl;
    }

    pointer allocate(size_type count, const void* = nullptr)
    {
        return (pointer)malloc(sizeof(T) * count);
    }

    void deallocate(pointer p, size_type count = 0)
    {
        free(p);
    }

    bool operator==(const my_allocator& other) const
    {
        cout << "operator==(const my_allocator& other): this=" << m_internalState
            << ", other=" << other.m_internalState << endl;
        return m_internalState == other.m_internalState;
    }

    bool operator!=(const my_allocator& other) const
    {
        return !(this->operator==(other));
    }
private:
    template <typename U>
    friend class my_allocator;

    const void* m_internalState;
};

int main()
{
    // 下述所有断言均成立
    //using X = my_allocator<int>;
    //using Y = my_allocator<long>;
    //Y b((void*)1);
    //X a(b);
    //assert(Y(a) == b);
    //assert(a == X(b));
    //assert(a == b);

    cout << "Create list" << endl;

    auto allocator = my_allocator<int>((void*)2);
    auto l2 = std::list<int, my_allocator<int>>(allocator);

    l2.emplace_back(2);
    l2.emplace_back(1);

    cout << "Sort list" << endl;
    l2.sort(); // 此处触发abort

    cout << "Success" << endl;
    return 0;
}
不同环境运行输出对比

VC环境输出

Create list
my_allocator(const void * pAllocator): internal state=0000000000000002
my_allocator(const my_allocator<U>& myAllocator): internal state=0000000000000002
Sort list
operator==(const my_allocator& other): this=0000000000000002, other=0000000000000002
Success

GCC环境输出

Error(s):
Abort signal from abort(3) (SIGABRT)

Create list
my_allocator(const void * pAllocator): internal state=0x2
my_allocator(const my_allocator<U>& myAllocator): internal state=0x2
Sort list
my_allocator(): internal state=0
(此处为60余次默认构造分配器的重复输出)
operator==(const my_allocator& other): this=0, other=0x2
触发abort的调用堆栈

堆栈按外层调用到内层触发点的顺序排列

std::__cxx11::list<int, my_allocator<int>>::_M_check_equal_allocators(...)
std::__cxx11::list<int, my_allocator<int>>::splice(...)  // 带位置、目标链表、迭代器参数的重载
std::__cxx11::list<int, my_allocator<int>>::splice(...)  // 带位置、目标链表、两个迭代器参数的重载
std::__cxx11::list<int, my_allocator<int>>::sort(...)
main()
问题解答
  • 这种实现行为不符合C++语言规范要求,属于libstdc++的已知实现缺陷。
  • 按C++标准规定,std::list::sort是容器内部操作,所有涉及节点分配、移动的逻辑,都必须复用容器自身保存的分配器实例,不能独立默认构造分配器创建临时容器。
  • 缺陷根因:旧版libstdc++的sort实现走归并排序逻辑时,用于临时缓存节点的辅助链表,没有从原list拷贝带状态的分配器,反而直接默认构造了无状态的分配器实例,最终在内部调用splice做分配器相等性校验时,发现两个分配器状态不匹配,主动触发abort中断。
  • 该问题在GCC 11及之后版本已修复,修复后的实现全程复用原容器的分配器,不会触发这类校验错误。

内容的提问来源于stack exchange,提问作者Seva Parfenov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.29 06:54:03