Fix data race conditions detected by Coverity Scan

This commit is contained in:
Carles Fernandez 2023-11-28 09:48:59 +01:00
parent 15c6108fe4
commit 9034ce44cb
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
1 changed files with 3 additions and 2 deletions

View File

@ -164,6 +164,7 @@ bool Gr_Complex_Ip_Packet_Source::start()
// open the ethernet device
if (open() == true)
{
gr::thread::scoped_lock guard(d_setlock);
// start pcap capture thread
d_pcap_thread = new boost::thread(
#if HAS_GENERIC_LAMBDA
@ -181,6 +182,7 @@ bool Gr_Complex_Ip_Packet_Source::start()
bool Gr_Complex_Ip_Packet_Source::stop()
{
std::cout << "gr_complex_ip_packet_source STOP\n";
gr::thread::scoped_lock guard(d_setlock);
if (descr != nullptr)
{
pcap_breakloop(descr);
@ -294,11 +296,11 @@ void Gr_Complex_Ip_Packet_Source::pcap_callback(__attribute__((unused)) u_char *
const u_char *udp_payload = (reinterpret_cast<const u_char *>(uh) + sizeof(gr_udp_header));
if (fifo_items <= (FIFO_SIZE - payload_length_bytes))
{
gr::thread::scoped_lock guard(d_setlock);
int aligned_write_items = FIFO_SIZE - fifo_write_ptr;
if (aligned_write_items >= payload_length_bytes)
{
// write all in a single memcpy
gr::thread::scoped_lock guard(d_setlock);
memcpy(&fifo_buff[fifo_write_ptr], &udp_payload[0], payload_length_bytes); // size in bytes
fifo_write_ptr += payload_length_bytes;
if (fifo_write_ptr == FIFO_SIZE)
@ -310,7 +312,6 @@ void Gr_Complex_Ip_Packet_Source::pcap_callback(__attribute__((unused)) u_char *
else
{
// two step wrap write
gr::thread::scoped_lock guard(d_setlock);
memcpy(&fifo_buff[fifo_write_ptr], &udp_payload[0], aligned_write_items); // size in bytes
fifo_write_ptr = payload_length_bytes - aligned_write_items;
memcpy(&fifo_buff[0], &udp_payload[aligned_write_items], fifo_write_ptr); // size in bytes