Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Removed the possibility to use optlen==-1 for null terminated strings in srt_setsockopt #2849

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3793,7 +3793,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
20 changes: 20 additions & 0 deletions test/test_socket_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,26 @@ 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 = "test123";
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, test_string.c_str(), -1), SRT_ERROR);
EXPECT_EQ(srt_getlasterror(NULL), SRT_EINVPARAM);
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

EXPECT_EQ(srt_setsockopt(m_caller_sock, 0, SRTO_PACKETFILTER, test_string.c_str(), -1), SRT_ERROR);
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved
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);
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
Loading