libbitcoin / libbitcoin-system

Bitcoin Cross-Platform C++ Development Toolkit
https://libbitcoin.info/
Other
1.28k stars 373 forks source link

g++12: three bogus -Wstringop-overflow warnings on data_chunk. #1461

Open evoskuil opened 1 month ago

evoskuil commented 1 month ago
                 from src/wallet/addresses/witness_address.cpp:19:
In member function ‘constexpr std::array<unsigned char, Size> libbitcoin::system::data_slice::to_array() const [with long unsigned int Size = 20]’,
    inlined from ‘constexpr libbitcoin::system::data_array<Size> libbitcoin::system::to_array(const data_slice&) [with long unsigned int Size = 20]’ at ./include/bitcoin/system/impl/data/data_array.ipp:39:32,
    inlined from ‘static libbitcoin::system::wallet::witness_address libbitcoin::system::wallet::witness_address::from_parameters(const libbitcoin::system::data_slice&, const std::string&, uint8_t)’ at src/wallet/addresses/witness_address.cpp:293:56:
./include/bitcoin/system/impl/data/data_slice.ipp:169:20: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
  169 |         out[index] = (*this)[index];
src/wallet/addresses/witness_address.cpp: In static member function ‘static libbitcoin::system::wallet::witness_address libbitcoin::system::wallet::witness_address::from_parameters(const libbitcoin::system::data_slice&, const std::string&, uint8_t)’:
src/wallet/addresses/witness_address.cpp:293:56: note: at offset 20 into destination object ‘<anonymous>’ of size 20
  293 |             return from_short(to_array<short_hash_size>(program), prefix);
      |                               ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
In member function ‘constexpr std::array<unsigned char, Size> libbitcoin::system::data_slice::to_array() const [with long unsigned int Size = 32]’,
    inlined from ‘constexpr libbitcoin::system::data_array<Size> libbitcoin::system::to_array(const data_slice&) [with long unsigned int Size = 32]’ at ./include/bitcoin/system/impl/data/data_array.ipp:39:32,
    inlined from ‘static libbitcoin::system::wallet::witness_address libbitcoin::system::wallet::witness_address::from_parameters(const libbitcoin::system::data_slice&, const std::string&, uint8_t)’ at src/wallet/addresses/witness_address.cpp:295:49:
./include/bitcoin/system/impl/data/data_slice.ipp:169:20: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
  169 |         out[index] = (*this)[index];
src/wallet/addresses/witness_address.cpp: In static member function ‘static libbitcoin::system::wallet::witness_address libbitcoin::system::wallet::witness_address::from_parameters(const libbitcoin::system::data_slice&, const std::string&, uint8_t)’:
src/wallet/addresses/witness_address.cpp:295:49: note: at offset 32 into destination object ‘<anonymous>’ of size 32
  295 |             return from_long(to_array<hash_size>(program), prefix);
      |                              ~~~~~~~~~~~~~~~~~~~^~~~~~~~~
  CXX      src/wallet/keys/libbitcoin_system_la-ec_scalar.lo
  CXX      src/wallet/keys/libbitcoin_system_la-ek_private.lo
In file included from ./include/bitcoin/system/data/data_slice.hpp:169,
                 from ./include/bitcoin/system/data/data_chunk.hpp:24,
                 from ./include/bitcoin/system/data/data_array.hpp:25,
                 from ./include/bitcoin/system/data/data.hpp:25,
                 from ./include/bitcoin/system/crypto/aes256.hpp:22,
                 from ./include/bitcoin/system/crypto/crypto.hpp:22,
                 from ./include/bitcoin/system/wallet/keys/ec_private.hpp:24,
                 from src/wallet/keys/ec_private.cpp:19:
In member function ‘constexpr std::array<unsigned char, Size> libbitcoin::system::data_slice::to_array() const [with long unsigned int Size = 38]’,
    inlined from ‘constexpr libbitcoin::system::data_array<Size> libbitcoin::system::to_array(const data_slice&) [with long unsigned int Size = 38]’ at ./include/bitcoin/system/impl/data/data_array.ipp:39:32,
    inlined from ‘static libbitcoin::system::wallet::ec_private libbitcoin::system::wallet::ec_private::from_string(const std::string&, uint8_t)’ at src/wallet/keys/ec_private.cpp:109:47:
./include/bitcoin/system/impl/data/data_slice.ipp:169:20: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
  169 |         out[index] = (*this)[index];
src/wallet/keys/ec_private.cpp: In static member function ‘static libbitcoin::system::wallet::ec_private libbitcoin::system::wallet::ec_private::from_string(const std::string&, uint8_t)’:
src/wallet/keys/ec_private.cpp:109:47: note: at offset 38 into destination object ‘<anonymous>’ of size 38
  109 |         return { to_array<wif_compressed_size>(decoded), address };
      |                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
  CXX      src/wallet/keys/libbitcoin_system_la-ek_public.lo
evoskuil commented 1 month ago

https://www.phoronix.com/news/Linux-Drop-GCC--Wstringop-of

evoskuil commented 1 month ago

I've reviewed the sources and these are all invalid warnings. The target array is allocated at compile time to copy loop iterations, based on the to_array<Size>(...) template arg, so there is no way it's not allocated. And the source for the copy is also safe, as it is an overloaded operator[] that is always safe to read at any position (fills zeros).