When running test_bitcoin
under Valgrind I found the following issue:
0$ valgrind src/test/test_bitcoin
1...
2==10465== Use of uninitialised value of size 8
3==10465== at 0x6D09B61: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
4==10465== by 0x6D0B1BB: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<unsigned long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, unsigned long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
5==10465== by 0x6D0B36C: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, unsigned long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
6==10465== by 0x6D17699: std::ostream& std::ostream::_M_insert<unsigned long>(unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
7==10465== by 0x4CAAD7: operator<< (ostream:171)
8==10465== by 0x4CAAD7: formatValue<ServiceFlags> (tinyformat.h:345)
9==10465== by 0x4CAAD7: void tinyformat::detail::FormatArg::formatImpl<ServiceFlags>(std::ostream&, char const*, char const*, int, void const*) (tinyformat.h:523)
10==10465== by 0x1924D4: format (tinyformat.h:510)
11==10465== by 0x1924D4: tinyformat::detail::formatImpl(std::ostream&, char const*, tinyformat::detail::FormatArg const*, int) (tinyformat.h:803)
12==10465== by 0x553A55: vformat (tinyformat.h:947)
13==10465== by 0x553A55: format<ServiceFlags> (tinyformat.h:957)
14==10465== by 0x553A55: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > tinyformat::format<ServiceFlags>(char const*, ServiceFlags const&) (tinyformat.h:966)
15==10465== by 0x54C952: getnetworkinfo(JSONRPCRequest const&) (net.cpp:462)
16==10465== by 0x28EDB5: CallRPC(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) (rpc_tests.cpp:31)
17==10465== by 0x293947: rpc_tests::rpc_togglenetwork::test_method() (rpc_tests.cpp:88)
18==10465== by 0x2950E5: rpc_tests::rpc_togglenetwork_invoker() (rpc_tests.cpp:84)
19==10465== by 0x182496: invoke<void (*)()> (callback.hpp:56)
20==10465== by 0x182496: boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()>::invoke() (callback.hpp:89)
21...
The read of the uninitialized variable nLocalServices
is triggered by g_connman->GetLocalServices()
in getnetworkinfo(const JSONRPCRequest& request)
(net.cpp:462
):
0UniValue getnetworkinfo(const JSONRPCRequest& request)
1{
2...
3 if(g_connman)
4 obj.push_back(Pair("localservices", strprintf("%016x", g_connman->GetLocalServices())));
5...
6}
The reason for the uninitialized nLocalServices
is that CConnman::Start(...)
is not called
by the tests, and hence the initialization normally performed by CConnman::Start(...)
is
not done.
This commit adds a method Init(const Options& connOptions)
which is called by both the
constructor and CConnman::Start(...)
. This method initializes nLocalServices
and the other
relevant values from the supplied Options
object.