Skip to content

Commit

Permalink
[core] Fixed wrong max socket ID value formula (#1816)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethouris committed Feb 19, 2021
1 parent 21058d5 commit 030b0d4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ m_ClosedSockets()

const double rand1_0 = double(rand())/RAND_MAX;

m_SocketIDGenerator = 1 + int(MAX_SOCKET_VAL * rand1_0);
// Motivation: in case when rand() returns the value equal to RAND_MAX,
// rand1_0 == 1, so the below formula will be
// 1 + (MAX_SOCKET_VAL-1) * 1 = 1 + MAX_SOCKET_VAL - 1 = MAX_SOCKET_VAL
// which is the highest allowed value for the socket.
m_SocketIDGenerator = 1 + int((MAX_SOCKET_VAL-1) * rand1_0);
m_SocketIDGenerator_init = m_SocketIDGenerator;

// XXX An unlikely exception thrown from the below calls
Expand Down Expand Up @@ -338,7 +342,7 @@ SRTSOCKET CUDTUnited::generateSocketID(bool for_group)
{
// We have a rollover on the socket value, so
// definitely we haven't made the Columbus mistake yet.
m_SocketIDGenerator = MAX_SOCKET_VAL-1;
m_SocketIDGenerator = MAX_SOCKET_VAL;
}

// Check all sockets if any of them has this value.
Expand Down Expand Up @@ -384,7 +388,7 @@ SRTSOCKET CUDTUnited::generateSocketID(bool for_group)
// The socket value is in use.
--sockval;
if (sockval <= 0)
sockval = MAX_SOCKET_VAL-1;
sockval = MAX_SOCKET_VAL;

// Before continuing, check if we haven't rolled back to start again
// This is virtually impossible, so just make an RTI error.
Expand Down
2 changes: 1 addition & 1 deletion srtcore/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ friend class CRendezvousQueue;
~CUDTUnited();

// Public constants
static const int32_t MAX_SOCKET_VAL = 1 << 29; // maximum value for a regular socket
static const int32_t MAX_SOCKET_VAL = SRTGROUP_MASK - 1; // maximum value for a regular socket

public:

Expand Down

0 comments on commit 030b0d4

Please sign in to comment.