From 2f627581ef794582ff05d4fc0034a8c4ea41c2d8 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Mon, 25 Jan 2021 13:35:47 +0100 Subject: [PATCH] Fix error handling issues detected by Coverity Scan 2020.09 --- .../PVT/libs/monitor_pvt_udp_sink.cc | 5 +++- .../two_bit_packed_file_signal_source.cc | 3 +- .../signal_source/libs/rtl_tcp_commands.cc | 5 +++- .../signal_source/libs/rtl_tcp_dongle_info.cc | 6 ++-- .../tracking/libs/tcp_communication.cc | 24 ++++++++++++--- src/core/monitor/gnss_synchro_udp_sink.cc | 6 +++- src/core/receiver/tcp_cmd_interface.cc | 29 ++++++++++++------- 7 files changed, 57 insertions(+), 21 deletions(-) diff --git a/src/algorithms/PVT/libs/monitor_pvt_udp_sink.cc b/src/algorithms/PVT/libs/monitor_pvt_udp_sink.cc index 240ee419d..0d54d6bcd 100644 --- a/src/algorithms/PVT/libs/monitor_pvt_udp_sink.cc +++ b/src/algorithms/PVT/libs/monitor_pvt_udp_sink.cc @@ -59,7 +59,10 @@ bool Monitor_Pvt_Udp_Sink::write_monitor_pvt(const Monitor_Pvt* const monitor_pv try { - socket.send(boost::asio::buffer(outbound_data)); + if (socket.send(boost::asio::buffer(outbound_data)) == 0) + { + return false; + } } catch (boost::system::system_error const& e) { diff --git a/src/algorithms/signal_source/adapters/two_bit_packed_file_signal_source.cc b/src/algorithms/signal_source/adapters/two_bit_packed_file_signal_source.cc index ed43e34c8..c31bf3390 100644 --- a/src/algorithms/signal_source/adapters/two_bit_packed_file_signal_source.cc +++ b/src/algorithms/signal_source/adapters/two_bit_packed_file_signal_source.cc @@ -92,6 +92,8 @@ TwoBitPackedFileSignalSource::TwoBitPackedFileSignalSource( item_size_ = sizeof(char); } + reverse_interleaving_ = false; + is_complex_ = true; if (sample_type_ == "real") { is_complex_ = false; @@ -99,7 +101,6 @@ TwoBitPackedFileSignalSource::TwoBitPackedFileSignalSource( else if (sample_type_ == "iq") { is_complex_ = true; - reverse_interleaving_ = false; } else if (sample_type_ == "qi") { diff --git a/src/algorithms/signal_source/libs/rtl_tcp_commands.cc b/src/algorithms/signal_source/libs/rtl_tcp_commands.cc index b1685adf7..5ba7320fa 100644 --- a/src/algorithms/signal_source/libs/rtl_tcp_commands.cc +++ b/src/algorithms/signal_source/libs/rtl_tcp_commands.cc @@ -32,6 +32,9 @@ boost::system::error_code rtl_tcp_command(RTL_TCP_COMMAND id, unsigned param, bo std::memcpy(&data[1], &nparam, sizeof(nparam)); boost::system::error_code ec; - socket.send(boost::asio::buffer(data), 0, ec); + if (socket.send(boost::asio::buffer(data), 0, ec) == 0) + { + // 0 bytes sent + } return ec; } diff --git a/src/algorithms/signal_source/libs/rtl_tcp_dongle_info.cc b/src/algorithms/signal_source/libs/rtl_tcp_dongle_info.cc index eafb05f6c..8bacdee8a 100644 --- a/src/algorithms/signal_source/libs/rtl_tcp_dongle_info.cc +++ b/src/algorithms/signal_source/libs/rtl_tcp_dongle_info.cc @@ -35,8 +35,8 @@ boost::system::error_code Rtl_Tcp_Dongle_Info::read(boost::asio::ip::tcp::socket boost::system::error_code ec; unsigned char data[sizeof(char) * 4 + sizeof(uint32_t) * 2]; - socket.receive(boost::asio::buffer(data), 0, ec); - if (!ec) + size_t received_bits = socket.receive(boost::asio::buffer(data), 0, ec); + if (!ec && (received_bits > 0)) { std::memcpy(magic_, data, 4); @@ -46,7 +46,7 @@ boost::system::error_code Rtl_Tcp_Dongle_Info::read(boost::asio::ip::tcp::socket tuner_type_ = boost::asio::detail::socket_ops::network_to_host_long(type); uint32_t count; - std ::memcpy(&count, &data[8], 4); + std::memcpy(&count, &data[8], 4); tuner_gain_count_ = boost::asio::detail::socket_ops::network_to_host_long(count); } diff --git a/src/algorithms/tracking/libs/tcp_communication.cc b/src/algorithms/tracking/libs/tcp_communication.cc index 4ca44e734..17cd20c09 100644 --- a/src/algorithms/tracking/libs/tcp_communication.cc +++ b/src/algorithms/tracking/libs/tcp_communication.cc @@ -66,10 +66,16 @@ void Tcp_Communication::send_receive_tcp_packet_galileo_e1(boost::array> controlc; } + catch (...) + { + std::cerr << "Exception reading TCP data\n"; + } } diff --git a/src/core/monitor/gnss_synchro_udp_sink.cc b/src/core/monitor/gnss_synchro_udp_sink.cc index fa1216433..3eb61578d 100644 --- a/src/core/monitor/gnss_synchro_udp_sink.cc +++ b/src/core/monitor/gnss_synchro_udp_sink.cc @@ -57,10 +57,14 @@ bool Gnss_Synchro_Udp_Sink::write_gnss_synchro(const std::vector& try { - socket.send(boost::asio::buffer(outbound_data)); + if (socket.send(boost::asio::buffer(outbound_data)) == 0) + { + std::cerr << "Gnss_Synchro_Udp_Sink sent 0 bytes\n"; + } } catch (boost::system::system_error const& e) { + std::cerr << e.what() << '\n'; return false; } } diff --git a/src/core/receiver/tcp_cmd_interface.cc b/src/core/receiver/tcp_cmd_interface.cc index 0c60f00e5..d18a2da62 100644 --- a/src/core/receiver/tcp_cmd_interface.cc +++ b/src/core/receiver/tcp_cmd_interface.cc @@ -326,7 +326,7 @@ void TcpCmdInterface::run_cmd_server(int tcp_port) acceptor.accept(socket, not_throw); if (not_throw) { - std::cout << "TcpCmdInterface: Error when binding the port in the socket\n"; + std::cerr << "TcpCmdInterface: Error when binding the port in the socket\n"; continue; } @@ -336,7 +336,10 @@ void TcpCmdInterface::run_cmd_server(int tcp_port) { std::string response; boost::asio::streambuf b; - boost::asio::read_until(socket, b, '\n', error); + if (boost::asio::read_until(socket, b, '\n', error) == 0) + { + std::cerr << "TcpCmdInterface: Error reading messages: " << error.message() << '\n'; + } std::istream is(&b); std::string line; std::getline(is, line); @@ -352,7 +355,10 @@ void TcpCmdInterface::run_cmd_server(int tcp_port) { error = boost::asio::error::eof; // send cmd response - socket.write_some(boost::asio::buffer("OK\n"), not_throw); + if (socket.write_some(boost::asio::buffer("OK\n"), not_throw) == 0) + { + std::cerr << "Error: 0 bytes sent in cmd response\n"; + } } else { @@ -374,10 +380,13 @@ void TcpCmdInterface::run_cmd_server(int tcp_port) } // send cmd response - socket.write_some(boost::asio::buffer(response), not_throw); + if (socket.write_some(boost::asio::buffer(response), not_throw) == 0) + { + std::cerr << "Error: 0 bytes sent in cmd response\n"; + } if (not_throw) { - std::cout << "Error sending(" << not_throw.value() << "): " << not_throw.message() << '\n'; + std::cerr << "Error sending(" << not_throw.value() << "): " << not_throw.message() << '\n'; break; } } @@ -385,11 +394,11 @@ void TcpCmdInterface::run_cmd_server(int tcp_port) if (error == boost::asio::error::eof) { - std::cout << "TcpCmdInterface: EOF detected\n"; + std::cerr << "TcpCmdInterface: EOF detected\n"; } else { - std::cout << "TcpCmdInterface unexpected error: " << error << '\n'; + std::cerr << "TcpCmdInterface unexpected error: " << error << '\n'; } // Close socket @@ -397,16 +406,16 @@ void TcpCmdInterface::run_cmd_server(int tcp_port) } catch (const boost::exception &e) { - std::cout << "TcpCmdInterface: Boost exception\n"; + std::cerr << "TcpCmdInterface: Boost exception\n"; } catch (const std::exception &ex) { - std::cout << "TcpCmdInterface: Exception " << ex.what() << '\n'; + std::cerr << "TcpCmdInterface: Exception " << ex.what() << '\n'; } } } catch (const boost::exception &e) { - std::cout << "TCP Command Interface exception: address already in use\n"; + std::cerr << "TCP Command Interface exception: address already in use\n"; } }