Crash when using simple constexpr array of initializer_list on MSVC (fine on gcc and clang)

60 Views Asked by At

Here is some simple program (reduced version when I was writing data for some unit tests):

#include <initializer_list>
#include <iostream>
#include <string_view>
#include <vector>

using namespace std::string_view_literals;

constexpr std::initializer_list<std::string_view> data[] {
    {},
    {
        "aa"sv,
        "bb"sv,
    },
    {
        "cc"sv,
        "dd"sv,
        "ee"sv,
    },
};

std::vector<std::string> banHashesDataGenerator(size_t index)
{
    if (index >= std::size(data)) {
        throw std::invalid_argument { "index out of range" };
    }
    std::vector<std::string> result;
    for (auto& item : data[index]) {
        result.emplace_back(std::string { item });
    }
    return result;
}

int main()
{
    for (auto& x : banHashesDataGenerator(1)) {
        std::cout << x << '\n';
    }

    return 0;
}

When I run compile it on Win10 with MSVC 19.38.33133 I see crash (with and without address sanitizer enabled).

C:\Users\marek r\Downloads>cl /std:c++20 /EHcs bug.cpp /fsanitize=address /Zi
Microsoft (R) C/C++ Optimizing Compiler Version 19.38.33133 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

bug.cpp
Microsoft (R) Incremental Linker Version 14.38.33133.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:bug.exe
/InferAsanLibs
/debug
bug.obj

C:\Users\marek r\Downloads>bug.exe
=================================================================
==21384==ERROR: AddressSanitizer: access-violation on unknown address 0x000000000000 (pc 0x7ff62bc718e1 bp 0x000000000000 sp 0x008189cff9a0 T0)
==21384==The signal is caused by a READ memory access.
==21384==Hint: address points to the zero page.
    #0 0x7ff62bc718e0 in std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>>::basic_string<char, struct std::char_traits<char>, class std::allocator<char>><class std::basic_string_view<char, struct std::char_traits<char>>, 0>(class std::basic_string_view<char, struct std::char_traits<char>> const &, class std::allocator<char> const &) (C:\Users\marek r\Downloads\bug.exe+0x1400018e0)
    #1 0x7ff62bc7140d in banHashesDataGenerator(unsigned __int64) (C:\Users\marek r\Downloads\bug.exe+0x14000140d)
    #2 0x7ff62bc715a8 in main (C:\Users\marek r\Downloads\bug.exe+0x1400015a8)
    #3 0x7ff62bcb9bbb in invoke_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
    #4 0x7ff62bcb9bbb in __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #5 0x7ffd2b8f7343  (C:\WINDOWS\System32\KERNEL32.DLL+0x180017343)
    #6 0x7ffd2cba26b0  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x1800526b0)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: access-violation (C:\Users\marek r\Downloads\bug.exe+0x1400018e0) in std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>>::basic_string<char, struct std::char_traits<char>, class std::allocator<char>><class std::basic_string_view<char, struct std::char_traits<char>>, 0>(class std::basic_string_view<char, struct std::char_traits<char>> const &, class std::allocator<char> const &)
==21384==ABORTING

C:\Users\marek r\Downloads>cl /std:c++20 /EHcs bug.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.38.33133 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

bug.cpp
Microsoft (R) Incremental Linker Version 14.38.33133.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:bug.exe
bug.obj

C:\Users\marek r\Downloads>bug.exe

CRASH

It is fine on gcc and clang.

Is this some known bug of MSVC? Is there some nice workaround on this issue (i prefer to have those constexpr test data). Or maybe I've missed something and my code is simply invalid?

0

There are 0 best solutions below