From e3ac83955a49f8b7bc7204a01bf84db257520b08 Mon Sep 17 00:00:00 2001 From: yomnes0 Date: Tue, 9 Jan 2024 10:37:38 +0100 Subject: [PATCH] [core] #2845 Add a check in srt_setsockopt to prevent optlen = -1 --- srtcore/api.cpp | 2 +- test/test_socket_options.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/srtcore/api.cpp b/srtcore/api.cpp index 7ec8ff570..a69f6dbd8 100644 --- a/srtcore/api.cpp +++ b/srtcore/api.cpp @@ -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 diff --git a/test/test_socket_options.cpp b/test/test_socket_options.cpp index b7acda37a..4239fa546 100644 --- a/test/test_socket_options.cpp +++ b/test/test_socket_options.cpp @@ -850,6 +850,15 @@ TEST_F(TestSocketOptions, StreamIDWrongLen) EXPECT_EQ(srt_getlasterror(NULL), SRT_EINVPARAM); } +//Check if setting -1 as optlen returns an error +TEST_F(TestSocketOptions, StringOptLenInvalid) +{ + + string stream_id = "test123"; + EXPECT_EQ(srt_setsockopt(m_caller_sock, 0, SRTO_STREAMID, stream_id.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.