0

我得到了一个-Wclass-memaccessgcc >= 8 ,我想知道我是否可以安全地忽略警告。

测试用例:

#include <array>
#include <boost/endian/buffers.hpp>


int main()
{
    static_assert(std::is_trivial<boost::endian::big_uint16_buf_t>::value);
    static_assert(std::is_trivially_copyable<boost::endian::big_uint16_buf_t>::value);

    // No warning when buffer is char*
    {
        std::array<char, 2> buffer{0x0, 0x1};

        boost::endian::big_uint16_buf_t v;
        std::memcpy(&v, buffer.data(), sizeof(v));
    }

    // -Wclass-memaccess when buffer is std::byte*
    {
        std::array<std::byte, 2> buffer{std::byte{0x0}, std::byte{0x1}};

        boost::endian::big_uint16_buf_t v;
        std::memcpy(&v, buffer.data(), sizeof(v));
    }
}

在魔杖盒上试试

背景:这种类型的代码用于解码二进制网络流量,memcpy 用于安全,具有严格的别名规则。

在更大的代码库上更广泛地使用该模式之前,我想确保它没有缺陷。

我的印象是复制到 boost::endian 缓冲区是安全的。奇怪的是,当缓冲区是指向的指针时没有警告char,但只有当缓冲区是指向的指针时std::byte

完整的警告信息是:

prog.cc: In function 'int main()':
prog.cc:23:49: warning: 'void* memcpy(void*, const void*, size_t)' copying an object of type 'boost::endian::big_uint16_buf_t' {aka 'class boost::endian::endian_buffer<boost::endian::order::big, short unsigned int, 16>'} with 'protected' member 'boost::endian::endian_buffer<boost::endian::order::big, short unsigned int, 16>::m_value' from an array of 'std::array<std::byte, 6>::value_type' {aka 'enum class std::byte'}; use assignment or copy-initialization instead [-Wclass-memaccess]
23 |         std::memcpy(&v, buffer.data(), sizeof(v));
    |                                                 ^
In file included from prog.cc:2:
/opt/wandbox/boost-1.70.0/gcc-9.1.0/include/boost/endian/buffers.hpp:375:11: note: 'boost::endian::big_uint16_buf_t' {aka 'class boost::endian::endian_buffer<boost::endian::order::big, short unsigned int, 16>'} declared here
375 |     class endian_buffer< order::big, T, n_bits, align::no >
    |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4

0 回答 0