Skip to content

Commit

Permalink
packetbuffer: fix logic for FreePacket()
Browse files Browse the repository at this point in the history
m_next_empty_packet_key is a random number that is incremented.  In FreePacket(),
the upper 32 bits are tested for equivalence.

However, the incrementing of m_next_empty_packet_key could overflow the lower 32 bits,
changing the upper 32.  This would cause the if statement to be false and the packet to
not be marked as empty.

Starting with m_next_empty_packet_key with the lower 32 bits cleared, prevents this error
by allowing 2^32 packets before overflowing, far more than can be reasonably expected.
  • Loading branch information
ulmus-scott authored and linuxdude42 committed Apr 28, 2022
1 parent 0c59286 commit ca4d1bc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 4 additions & 3 deletions mythtv/libs/libmythtv/recorders/rtp/packetbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

PacketBuffer::PacketBuffer(unsigned int bitrate) :
m_bitrate(bitrate),
m_next_empty_packet_key(MythRandom64())
m_next_empty_packet_key(MythRandom() << 32)
{
}

Expand Down Expand Up @@ -48,7 +48,8 @@ UDPPacket PacketBuffer::GetEmptyPacket(void)

void PacketBuffer::FreePacket(const UDPPacket &packet)
{
uint64_t top = packet.GetKey() & (0xFFFFFFFFULL<<32);
if (top == (m_next_empty_packet_key & (0xFFFFFFFFULL<<32)))
static constexpr uint64_t k_mask_upper_32 = ~((UINT64_C(1) << (64 - 32)) - 1);
uint64_t top = packet.GetKey() & k_mask_upper_32;
if (top == (m_next_empty_packet_key & k_mask_upper_32))
m_empty_packets[packet.GetKey()] = packet;
}
4 changes: 3 additions & 1 deletion mythtv/libs/libmythtv/recorders/rtp/packetbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class PacketBuffer
protected:
uint m_bitrate;

/// Packets key to use for next empty packet
/** @brief Packets key to use for next empty packet.
The upper 32 bits are random and the lower 32 bits are incremented from 0.
*/
uint64_t m_next_empty_packet_key;

/// Packets ready for reuse
Expand Down

0 comments on commit ca4d1bc

Please sign in to comment.