Skip to content

boost::container::flat_map force_copy violates strict-aliasing rule #252

Closed
@prus1337

Description

@prus1337

Hi everyone!

g++ v13.1.0, v13.2.0, boost v1.82, MSYS2 MINGW64_NT-10.0-19044.

Discussion links:
msys2/MINGW-packages#17977
msys2/MINGW-packages#17977 (comment)

The following example has different behavior depending on compiler optimization flags:

#include <boost/container/flat_map.hpp>

#include <cstdint>
#include <iostream>
#include <string>

int main()
{
    boost::container::flat_map<std::string, size_t> map{};
    const std::string key = "test";
    const size_t value = 13;
	
    auto [it, succeeded] = map.emplace(key, value);
	
    if (succeeded)
    {
        std::cout << "emplace result: " << it->first << " -> " << it->second << "\n";
        std::cout << "emplace iterator result: " << "it == map.end() is " << std::boolalpha << (it == map.end()) << "\n";
		
        it = map.find(key);
        std::cout << "find result: " << it->first << " -> " << it->second << "\n";
        std::cout << "find iterator result: " << "it == map.end() is " << std::boolalpha << (it == map.end()) << "\n"; 
    }
	
    return 0;
}

Expected behavior

Without optimization flags (-O0, -O1) everything fine.
g++ -O0 -o boost_v1.82_flat_map_it_bug boost_v1.82_flat_map_it_bug.cpp

$ ./boost_v1.82_flat_map_it_bug
emplace result: test -> 13
emplace iterator result: it == map.end() is false
find result: test -> 13
find iterator result: it == map.end() is false

Actual behavior

With optimization flags (-O2, -O3) iterator has bad value.
g++ -O2 -o boost_v1.82_flat_map_it_bug boost_v1.82_flat_map_it_bug.cpp

./boost_v1.82_flat_map_it_bug
emplace result: test -> 13
emplace iterator result: it == map.end() is true
find result: test -> 13
find iterator result: it == map.end() is true

The issue does not happen in Linux with gcc 13.2.1 toolchain.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions