From 4b1d346a80f860ebcf26712b721606c19dd904dd Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Thu, 23 Jan 2020 11:02:15 -0800 Subject: octoclock: Avoid usage of uninitialized memory The Octoclock host code would send uninitialized memory over the network, which would be flagged by tools such as Valgrind. This patch creates a factory function for OctoClock packets that initializes the memory to zero, defaults the proto version to the OctoClock default, and can provide a random sequence number if none is given. --- host/lib/usrp_clock/octoclock/octoclock_impl.cpp | 40 ++++++++++++++++-------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'host/lib/usrp_clock/octoclock/octoclock_impl.cpp') diff --git a/host/lib/usrp_clock/octoclock/octoclock_impl.cpp b/host/lib/usrp_clock/octoclock/octoclock_impl.cpp index b7ebc3473..146470a7b 100644 --- a/host/lib/usrp_clock/octoclock/octoclock_impl.cpp +++ b/host/lib/usrp_clock/octoclock/octoclock_impl.cpp @@ -99,12 +99,9 @@ device_addrs_t octoclock_find(const device_addr_t& hint) _hint["addr"], BOOST_STRINGIZE(OCTOCLOCK_UDP_CTRL_PORT)); // Send a query packet - octoclock_packet_t pkt_out; - pkt_out.proto_ver = OCTOCLOCK_FW_COMPAT_NUM; - // To avoid replicating sequence numbers between sessions - pkt_out.sequence = uint32_t(std::rand()); - pkt_out.len = 0; - pkt_out.code = OCTOCLOCK_QUERY_CMD; + auto pkt_out = make_octoclock_packet(); + pkt_out.len = 0; + pkt_out.code = OCTOCLOCK_QUERY_CMD; try { udp_transport->send(boost::asio::buffer(&pkt_out, sizeof(pkt_out))); } catch (const std::exception& ex) { @@ -179,6 +176,27 @@ UHD_STATIC_BLOCK(register_octoclock_device) device::register_device(&octoclock_find, &octoclock_make, device::CLOCK); } +/*********************************************************************** + * Helpers + **********************************************************************/ +octoclock_packet_t make_octoclock_packet() +{ + octoclock_packet_t new_pkt; + memset(&new_pkt, 0, sizeof(octoclock_packet_t)); + new_pkt.sequence = uint32_t(std::rand()); + new_pkt.proto_ver = OCTOCLOCK_FW_COMPAT_NUM; + return new_pkt; +} + +octoclock_packet_t make_octoclock_packet(const uint32_t sequence) +{ + octoclock_packet_t new_pkt; + memset(&new_pkt, 0, sizeof(octoclock_packet_t)); + new_pkt.sequence = sequence; + new_pkt.proto_ver = OCTOCLOCK_FW_COMPAT_NUM; + return new_pkt; +} + /*********************************************************************** * Structors **********************************************************************/ @@ -350,9 +368,7 @@ void octoclock_impl::_set_eeprom( uint32_t octoclock_impl::_get_fw_version(const std::string& oc) { - octoclock_packet_t pkt_out; - pkt_out.sequence = uhd::htonx(++_sequence); - pkt_out.len = 0; + auto pkt_out = make_octoclock_packet(uhd::htonx(++_sequence)); size_t len; uint8_t octoclock_data[udp_simple::mtu]; @@ -373,10 +389,8 @@ uint32_t octoclock_impl::_get_fw_version(const std::string& oc) void octoclock_impl::_get_state(const std::string& oc) { - octoclock_packet_t pkt_out; - pkt_out.sequence = uhd::htonx(++_sequence); - pkt_out.len = 0; - size_t len = 0; + auto pkt_out = make_octoclock_packet(uhd::htonx(++_sequence)); + size_t len = 0; uint8_t octoclock_data[udp_simple::mtu]; const octoclock_packet_t* pkt_in = -- cgit v1.2.3