aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp_clock/octoclock/octoclock_impl.cpp
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2020-01-23 11:02:15 -0800
committeratrnati <54334261+atrnati@users.noreply.github.com>2020-02-04 08:53:01 -0600
commit4b1d346a80f860ebcf26712b721606c19dd904dd (patch)
tree31ee6a6a42ffdf3623b8695cfa17cec5f8adebf8 /host/lib/usrp_clock/octoclock/octoclock_impl.cpp
parent22db12c4b2b55225801ec1efb2465c7a06295b9e (diff)
downloaduhd-4b1d346a80f860ebcf26712b721606c19dd904dd.tar.gz
uhd-4b1d346a80f860ebcf26712b721606c19dd904dd.tar.bz2
uhd-4b1d346a80f860ebcf26712b721606c19dd904dd.zip
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.
Diffstat (limited to 'host/lib/usrp_clock/octoclock/octoclock_impl.cpp')
-rw-r--r--host/lib/usrp_clock/octoclock/octoclock_impl.cpp40
1 files changed, 27 insertions, 13 deletions
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) {
@@ -180,6 +177,27 @@ UHD_STATIC_BLOCK(register_octoclock_device)
}
/***********************************************************************
+ * 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
**********************************************************************/
octoclock_impl::octoclock_impl(const device_addr_t& _device_addr)
@@ -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<uint32_t>(++_sequence);
- pkt_out.len = 0;
+ auto pkt_out = make_octoclock_packet(uhd::htonx<uint32_t>(++_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<uint32_t>(++_sequence);
- pkt_out.len = 0;
- size_t len = 0;
+ auto pkt_out = make_octoclock_packet(uhd::htonx<uint32_t>(++_sequence));
+ size_t len = 0;
uint8_t octoclock_data[udp_simple::mtu];
const octoclock_packet_t* pkt_in =