std::map argument with empty brace-initializers for default argument segfaults in GCC

前端 未结 1 855
时光取名叫无心
时光取名叫无心 2021-02-05 02:17

Problem

I got a bug report from user reporting a segfault in library I develop.

The minimal example of the faulty code is:

#include          


        
1条回答
  •  [愿得一人]
    2021-02-05 02:40

    Looks like this bug was fixed in 4.8.3/4.9.0, the bug report which has a similar example and also seg-faults says:

    The attached minimal testcase has the following function with default-constructed default argument:

    void do_something( foo f = {} )
    {     std::cout << "default argument is at " << &f << std::endl;
    }
    

    The constructor for foo outputs its address; I got the following output from a single run: constructed foo @ 0x7ffff10bdb7f default argument is at 0x7ffff10bdb60

    It shows that only 1 foo was constructed, and not at the same address as that of the default argument. It's been a loooong week, but I can't see anything wrong with the code. In the real code on which this was based, a segfault was occurring when running the destructor of a foo that was move-constructed from the default argument, because the underlying memory was seemingly uninitialised.

    We can see from a live example that 4.9.0 does not demonstrate this problem.

    We can see this was intentional functionality from defect report 994 and the subsequent resolution N3217:

    This paper presents detailed wording changes relative to the current C++ Working Draft N3126 to implement brace-initializers for default arguments for functions, as proposed in N3139 "An Incomplete Language Feature" by Bjarne Stroustrup, thereby also addressing core issue 994.

    This is also covered in the proposal N3139: An Incomplete Language Feature.

    Interesting to note that Visual Studio also has a bug with respect to brace-initializers as default arguments which I think is still unresolved.

    0 讨论(0)
提交回复
热议问题