Skip to content

Commit

Permalink
[core] Removed the possibility to use optlen=-1 in srt_setsockopt (Ha…
Browse files Browse the repository at this point in the history
…ivision#2849).

It was used to auto-determine the length of a null-terminated string
for the SRTO_BINDTODEVICE socket option.
  • Loading branch information
yomnes0 authored Jan 25, 2024
1 parent 6e0ab4e commit 3dba3f4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3807,7 +3807,7 @@ int srt::CUDT::getsockopt(SRTSOCKET u, int, SRT_SOCKOPT optname, void* pw_optval

int srt::CUDT::setsockopt(SRTSOCKET u, int, SRT_SOCKOPT optname, const void* optval, int optlen)
{
if (!optval)
if (!optval || optlen < 0)
return APIError(MJ_NOTSUP, MN_INVAL, 0);

try
Expand Down
11 changes: 3 additions & 8 deletions srtcore/socketconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,8 @@ struct CSrtConfigSetter<SRTO_BINDTODEVICE>
using namespace std;

string val;
if (optlen == -1)
val = (const char *)optval;
else
val.assign((const char *)optval, optlen);

val.assign((const char *)optval, optlen);
if (val.size() >= IFNAMSIZ)
{
LOGC(kmlog.Error, log << "SRTO_BINDTODEVICE: device name too long (max: IFNAMSIZ=" << IFNAMSIZ << ")");
Expand Down Expand Up @@ -597,10 +595,7 @@ struct CSrtConfigSetter<SRTO_CONGESTION>
static void set(CSrtConfig& co, const void* optval, int optlen)
{
std::string val;
if (optlen == -1)
val = (const char*)optval;
else
val.assign((const char*)optval, optlen);
val.assign((const char*)optval, optlen);

// Translate alias
if (val == "vod")
Expand Down
23 changes: 23 additions & 0 deletions test/test_socket_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,29 @@ TEST_F(TestSocketOptions, StreamIDWrongLen)
EXPECT_EQ(srt_getlasterror(NULL), SRT_EINVPARAM);
}

//Check if setting -1 as optlen returns an error
TEST_F(TestSocketOptions, StringOptLenInvalid)
{
const string test_string = "test1234567";
const string srto_congestion_string ="live";
const string fec_config = "fec,cols:10,rows:10";

EXPECT_EQ(srt_setsockopt(m_caller_sock, 0, SRTO_STREAMID, test_string.c_str(), -1), SRT_ERROR);
EXPECT_EQ(srt_getlasterror(NULL), SRT_EINVPARAM);

EXPECT_EQ(srt_setsockopt(m_caller_sock, 0, SRTO_BINDTODEVICE, test_string.c_str(), -1), SRT_ERROR);
EXPECT_EQ(srt_getlasterror(NULL), SRT_EINVPARAM);

EXPECT_EQ(srt_setsockopt(m_caller_sock, 0, SRTO_CONGESTION, srto_congestion_string.c_str(), -1), SRT_ERROR);
EXPECT_EQ(srt_getlasterror(NULL), SRT_EINVPARAM);

EXPECT_EQ(srt_setsockopt(m_caller_sock, 0, SRTO_PACKETFILTER, fec_config.c_str(), -1), SRT_ERROR);
EXPECT_EQ(srt_getlasterror(NULL), SRT_EINVPARAM);

EXPECT_EQ(srt_setsockopt(m_caller_sock, 0, SRTO_PASSPHRASE, test_string.c_str(), -1), SRT_ERROR);
EXPECT_EQ(srt_getlasterror(NULL), SRT_EINVPARAM);
}

// Try to set/get a 13-character string in SRTO_STREAMID.
// This tests checks that the StreamID is set to the correct size
// while it is transmitted as 16 characters in the Stream ID HS extension.
Expand Down

0 comments on commit 3dba3f4

Please sign in to comment.