From 368356c223521a393a268ffd15b5ec73832da5be Mon Sep 17 00:00:00 2001 From: Winlin Date: Fri, 18 Nov 2022 11:19:01 +0800 Subject: [PATCH] Support address sanitizer for utest and fix some leaks. (#3242) * MP4: Fix memory leak when error. * Kernel: Support free global objects for utest. * HTTP: Fix memory leak when error. * MP4: Support more sample rate for audio. * RTMP: Support free field for utest. * UTest: Support address sanitizer. --- trunk/src/app/srs_app_conn.cpp | 7 ++ trunk/src/kernel/srs_kernel_codec.cpp | 55 +++++++++++ trunk/src/kernel/srs_kernel_codec.hpp | 8 ++ trunk/src/kernel/srs_kernel_mp4.cpp | 29 +++--- .../src/protocol/srs_protocol_http_stack.cpp | 2 + .../src/protocol/srs_protocol_rtmp_stack.cpp | 72 ++++++++++++++ .../src/protocol/srs_protocol_rtmp_stack.hpp | 22 +++++ trunk/src/protocol/srs_protocol_utility.cpp | 21 ++-- trunk/src/utest/srs_utest.cpp | 9 +- trunk/src/utest/srs_utest_amf0.cpp | 15 +-- trunk/src/utest/srs_utest_avc.cpp | 18 ++-- trunk/src/utest/srs_utest_http.cpp | 1 + trunk/src/utest/srs_utest_kernel.cpp | 66 +++++++++---- trunk/src/utest/srs_utest_protocol.cpp | 63 +++++++----- trunk/src/utest/srs_utest_rtc.cpp | 5 +- trunk/src/utest/srs_utest_rtmp.cpp | 96 ++++++++----------- trunk/src/utest/srs_utest_service.cpp | 19 ++-- trunk/src/utest/srs_utest_srt.cpp | 6 +- 18 files changed, 360 insertions(+), 154 deletions(-) diff --git a/trunk/src/app/srs_app_conn.cpp b/trunk/src/app/srs_app_conn.cpp index b8a3e82a62..0fcada3a68 100644 --- a/trunk/src/app/srs_app_conn.cpp +++ b/trunk/src/app/srs_app_conn.cpp @@ -59,6 +59,13 @@ SrsResourceManager::~SrsResourceManager() clear(); + // Free all objects not in zombies. + std::vector::iterator it; + for (it = conns_.begin(); it != conns_.end(); ++it) { + ISrsResource* resource = *it; + srs_freep(resource); + } + srs_freepa(conns_level0_cache_); } diff --git a/trunk/src/kernel/srs_kernel_codec.cpp b/trunk/src/kernel/srs_kernel_codec.cpp index e62204ad7d..7991717966 100644 --- a/trunk/src/kernel/srs_kernel_codec.cpp +++ b/trunk/src/kernel/srs_kernel_codec.cpp @@ -68,6 +68,61 @@ string srs_audio_codec_id2str(SrsAudioCodecId codec) } } +SrsAudioSampleRate srs_audio_sample_rate_from_number(uint32_t v) +{ + if (v == 5512) return SrsAudioSampleRate5512; + if (v == 11025) return SrsAudioSampleRate11025; + if (v == 22050) return SrsAudioSampleRate22050; + if (v == 44100) return SrsAudioSampleRate44100; + + if (v == 12000) return SrsAudioSampleRate12000; + if (v == 24000) return SrsAudioSampleRate24000; + if (v == 48000) return SrsAudioSampleRate48000; + + if (v == 8000) return SrsAudioSampleRateNB8kHz; + if (v == 12000) return SrsAudioSampleRateMB12kHz; + if (v == 16000) return SrsAudioSampleRateWB16kHz; + if (v == 24000) return SrsAudioSampleRateSWB24kHz; + if (v == 48000) return SrsAudioSampleRateFB48kHz; + + return SrsAudioSampleRateForbidden; +} + +SrsAudioSampleRate srs_audio_sample_rate_guess_number(uint32_t v) +{ + if (v >= 48000) return SrsAudioSampleRate48000; + if (v >= 44100) return SrsAudioSampleRate44100; + if (v >= 24000) return SrsAudioSampleRate24000; + if (v >= 24000) return SrsAudioSampleRate24000; + if (v >= 22050) return SrsAudioSampleRate22050; + if (v >= 16000) return SrsAudioSampleRateWB16kHz; + if (v >= 12000) return SrsAudioSampleRate12000; + if (v >= 8000) return SrsAudioSampleRateNB8kHz; + if (v >= 5512) return SrsAudioSampleRate5512; + + return SrsAudioSampleRateForbidden; +} + +uint32_t srs_audio_sample_rate2number(SrsAudioSampleRate v) +{ + if (v == SrsAudioSampleRate5512) return 5512; + if (v == SrsAudioSampleRate11025) return 11025; + if (v == SrsAudioSampleRate22050) return 22050; + if (v == SrsAudioSampleRate44100) return 44100; + + if (v == SrsAudioSampleRate12000) return 12000; + if (v == SrsAudioSampleRate24000) return 24000; + if (v == SrsAudioSampleRate48000) return 48000; + + if (v == SrsAudioSampleRateNB8kHz) return 8000; + if (v == SrsAudioSampleRateMB12kHz) return 12000; + if (v == SrsAudioSampleRateWB16kHz) return 16000; + if (v == SrsAudioSampleRateSWB24kHz) return 24000; + if (v == SrsAudioSampleRateFB48kHz) return 48000; + + return 0; +} + string srs_audio_sample_rate2str(SrsAudioSampleRate v) { switch (v) { diff --git a/trunk/src/kernel/srs_kernel_codec.hpp b/trunk/src/kernel/srs_kernel_codec.hpp index dc4659cbf5..e4f781bc4e 100644 --- a/trunk/src/kernel/srs_kernel_codec.hpp +++ b/trunk/src/kernel/srs_kernel_codec.hpp @@ -196,6 +196,11 @@ enum SrsAudioSampleRate SrsAudioSampleRate11025 = 1, SrsAudioSampleRate22050 = 2, SrsAudioSampleRate44100 = 3, + + // For MP4, extra sampling rate to FLV. + SrsAudioSampleRate12000 = 12, + SrsAudioSampleRate24000 = 24, + SrsAudioSampleRate48000 = 48, // For Opus, support 8, 12, 16, 24, 48KHz // We will write a UINT8 sampling rate after FLV audio tag header. @@ -206,6 +211,9 @@ enum SrsAudioSampleRate SrsAudioSampleRateSWB24kHz = 24, // SWB (super-wideband) SrsAudioSampleRateFB48kHz = 48, // FB (fullband) }; +SrsAudioSampleRate srs_audio_sample_rate_from_number(uint32_t v); +SrsAudioSampleRate srs_audio_sample_rate_guess_number(uint32_t v); +uint32_t srs_audio_sample_rate2number(SrsAudioSampleRate v); std::string srs_audio_sample_rate2str(SrsAudioSampleRate v); /** diff --git a/trunk/src/kernel/srs_kernel_mp4.cpp b/trunk/src/kernel/srs_kernel_mp4.cpp index a8d990f671..fdc4f8c444 100644 --- a/trunk/src/kernel/srs_kernel_mp4.cpp +++ b/trunk/src/kernel/srs_kernel_mp4.cpp @@ -5237,6 +5237,8 @@ srs_error_t SrsMp4BoxReader::read(SrsSimpleStream* stream, SrsMp4Box** ppbox) srs_error_t err = srs_success; SrsMp4Box* box = NULL; + SrsAutoFree(SrsMp4Box, box); + while (true) { // For the first time to read the box, maybe it's a basic box which is only 4bytes header. // When we disconvery the real box, we know the size of the whole box, then read again and decode it. @@ -5277,10 +5279,9 @@ srs_error_t SrsMp4BoxReader::read(SrsSimpleStream* stream, SrsMp4Box** ppbox) continue; } - if (err != srs_success) { - srs_freep(box); - } else { + if (err == srs_success) { *ppbox = box; + box = NULL; } break; @@ -5352,6 +5353,7 @@ srs_error_t SrsMp4Decoder::initialize(ISrsReadSeeker* rs) while (true) { SrsMp4Box* box = NULL; + SrsAutoFree(SrsMp4Box, box); if ((err = load_next_box(&box, 0)) != srs_success) { return srs_error_wrap(err, "load box"); @@ -5375,8 +5377,6 @@ srs_error_t SrsMp4Decoder::initialize(ISrsReadSeeker* rs) } break; } - - srs_freep(box); } if (brand == SrsMp4BoxBrandForbidden) { @@ -5508,14 +5508,8 @@ srs_error_t SrsMp4Decoder::parse_moov(SrsMp4MovieBox* moov) SrsMp4AudioSampleEntry* mp4a = soun? soun->mp4a():NULL; if (mp4a) { uint32_t sr = mp4a->samplerate>>16; - if (sr >= 44100) { - sample_rate = SrsAudioSampleRate44100; - } else if (sr >= 22050) { - sample_rate = SrsAudioSampleRate22050; - } else if (sr >= 11025) { - sample_rate = SrsAudioSampleRate11025; - } else { - sample_rate = SrsAudioSampleRate5512; + if ((sample_rate = srs_audio_sample_rate_from_number(sr)) == SrsAudioSampleRateForbidden) { + sample_rate = srs_audio_sample_rate_guess_number(sr); } if (mp4a->samplesize == 16) { @@ -5580,16 +5574,17 @@ srs_error_t SrsMp4Decoder::load_next_box(SrsMp4Box** ppbox, uint32_t required_bo while (true) { SrsMp4Box* box = NULL; + SrsAutoFree(SrsMp4Box, box); + if ((err = do_load_next_box(&box, required_box_type)) != srs_success) { - srs_freep(box); return srs_error_wrap(err, "load box"); } if (!required_box_type || (uint32_t)box->type == required_box_type) { *ppbox = box; + box = NULL; break; } - srs_freep(box); } return err; @@ -5938,7 +5933,7 @@ srs_error_t SrsMp4Encoder::flush() SrsMp4AudioSampleEntry* mp4a = new SrsMp4AudioSampleEntry(); mp4a->data_reference_index = 1; - mp4a->samplerate = uint32_t(srs_flv_srates[sample_rate]) << 16; + mp4a->samplerate = srs_audio_sample_rate2number(sample_rate); if (sound_bits == SrsAudioSampleBits16bit) { mp4a->samplesize = 16; } else { @@ -6101,7 +6096,7 @@ SrsMp4ObjectType SrsMp4Encoder::get_audio_object_type() case SrsAudioCodecIdAAC: return SrsMp4ObjectTypeAac; case SrsAudioCodecIdMP3: - return (srs_flv_srates[sample_rate] > 24000) ? SrsMp4ObjectTypeMp1a : SrsMp4ObjectTypeMp3; // 11172 - 3 + return (srs_audio_sample_rate2number(sample_rate) > 24000) ? SrsMp4ObjectTypeMp1a : SrsMp4ObjectTypeMp3; // 11172 - 3 default: return SrsMp4ObjectTypeForbidden; } diff --git a/trunk/src/protocol/srs_protocol_http_stack.cpp b/trunk/src/protocol/srs_protocol_http_stack.cpp index ac579d4da2..5de1ad4ac7 100644 --- a/trunk/src/protocol/srs_protocol_http_stack.cpp +++ b/trunk/src/protocol/srs_protocol_http_stack.cpp @@ -689,12 +689,14 @@ srs_error_t SrsHttpServeMux::handle(std::string pattern, ISrsHttpHandler* handle srs_assert(handler); if (pattern.empty()) { + srs_freep(handler); return srs_error_new(ERROR_HTTP_PATTERN_EMPTY, "empty pattern"); } if (entries.find(pattern) != entries.end()) { SrsHttpMuxEntry* exists = entries[pattern]; if (exists->explicit_match) { + srs_freep(handler); return srs_error_new(ERROR_HTTP_PATTERN_DUPLICATED, "pattern=%s exists", pattern.c_str()); } } diff --git a/trunk/src/protocol/srs_protocol_rtmp_stack.cpp b/trunk/src/protocol/srs_protocol_rtmp_stack.cpp index 4f45d9efdb..5d716eaceb 100644 --- a/trunk/src/protocol/srs_protocol_rtmp_stack.cpp +++ b/trunk/src/protocol/srs_protocol_rtmp_stack.cpp @@ -3331,6 +3331,12 @@ SrsCreateStreamPacket::~SrsCreateStreamPacket() srs_freep(command_object); } +void SrsCreateStreamPacket::set_command_object(SrsAmf0Any* v) +{ + srs_freep(command_object); + command_object = v; +} + srs_error_t SrsCreateStreamPacket::decode(SrsBuffer* stream) { srs_error_t err = srs_success; @@ -3509,6 +3515,12 @@ SrsFMLEStartPacket::~SrsFMLEStartPacket() srs_freep(command_object); } +void SrsFMLEStartPacket::set_command_object(SrsAmf0Any* v) +{ + srs_freep(command_object); + command_object = v; +} + srs_error_t SrsFMLEStartPacket::decode(SrsBuffer* stream) { srs_error_t err = srs_success; @@ -3613,6 +3625,18 @@ SrsFMLEStartResPacket::~SrsFMLEStartResPacket() srs_freep(args); } +void SrsFMLEStartResPacket::set_args(SrsAmf0Any* v) +{ + srs_freep(args); + args = v; +} + +void SrsFMLEStartResPacket::set_command_object(SrsAmf0Any* v) +{ + srs_freep(command_object); + command_object = v; +} + srs_error_t SrsFMLEStartResPacket::decode(SrsBuffer* stream) { srs_error_t err = srs_success; @@ -3691,6 +3715,12 @@ SrsPublishPacket::~SrsPublishPacket() srs_freep(command_object); } +void SrsPublishPacket::set_command_object(SrsAmf0Any* v) +{ + srs_freep(command_object); + command_object = v; +} + srs_error_t SrsPublishPacket::decode(SrsBuffer* stream) { srs_error_t err = srs_success; @@ -3961,6 +3991,18 @@ SrsPlayResPacket::~SrsPlayResPacket() srs_freep(desc); } +void SrsPlayResPacket::set_command_object(SrsAmf0Any* v) +{ + srs_freep(command_object); + command_object = v; +} + +void SrsPlayResPacket::set_desc(SrsAmf0Object* v) +{ + srs_freep(desc); + desc = v; +} + int SrsPlayResPacket::get_prefer_cid() { return RTMP_CID_OverStream; @@ -4012,6 +4054,12 @@ SrsOnBWDonePacket::~SrsOnBWDonePacket() srs_freep(args); } +void SrsOnBWDonePacket::set_args(SrsAmf0Any* v) +{ + srs_freep(args); + args = v; +} + int SrsOnBWDonePacket::get_prefer_cid() { return RTMP_CID_OverConnection; @@ -4061,6 +4109,18 @@ SrsOnStatusCallPacket::~SrsOnStatusCallPacket() srs_freep(data); } +void SrsOnStatusCallPacket::set_args(SrsAmf0Any* v) +{ + srs_freep(args); + args = v; +} + +void SrsOnStatusCallPacket::set_data(SrsAmf0Object* v) +{ + srs_freep(data); + data = v; +} + int SrsOnStatusCallPacket::get_prefer_cid() { return RTMP_CID_OverStream; @@ -4111,6 +4171,12 @@ SrsOnStatusDataPacket::~SrsOnStatusDataPacket() srs_freep(data); } +void SrsOnStatusDataPacket::set_data(SrsAmf0Object* v) +{ + srs_freep(data); + data = v; +} + int SrsOnStatusDataPacket::get_prefer_cid() { return RTMP_CID_OverStream; @@ -4198,6 +4264,12 @@ SrsOnMetaDataPacket::~SrsOnMetaDataPacket() srs_freep(metadata); } +void SrsOnMetaDataPacket::set_metadata(SrsAmf0Object* v) +{ + srs_freep(metadata); + metadata = v; +} + srs_error_t SrsOnMetaDataPacket::decode(SrsBuffer* stream) { srs_error_t err = srs_success; diff --git a/trunk/src/protocol/srs_protocol_rtmp_stack.hpp b/trunk/src/protocol/srs_protocol_rtmp_stack.hpp index 36bd0d28fa..d9c76bb104 100644 --- a/trunk/src/protocol/srs_protocol_rtmp_stack.hpp +++ b/trunk/src/protocol/srs_protocol_rtmp_stack.hpp @@ -908,6 +908,8 @@ class SrsCreateStreamPacket : public SrsPacket public: SrsCreateStreamPacket(); virtual ~SrsCreateStreamPacket(); +public: + void set_command_object(SrsAmf0Any* v); // Decode functions for concrete packet to override. public: virtual srs_error_t decode(SrsBuffer* stream); @@ -982,6 +984,8 @@ class SrsFMLEStartPacket : public SrsPacket public: SrsFMLEStartPacket(); virtual ~SrsFMLEStartPacket(); +public: + void set_command_object(SrsAmf0Any* v); // Decode functions for concrete packet to override. public: virtual srs_error_t decode(SrsBuffer* stream); @@ -1014,6 +1018,9 @@ class SrsFMLEStartResPacket : public SrsPacket public: SrsFMLEStartResPacket(double _transaction_id); virtual ~SrsFMLEStartResPacket(); +public: + void set_args(SrsAmf0Any* v); + void set_command_object(SrsAmf0Any* v); // Decode functions for concrete packet to override. public: virtual srs_error_t decode(SrsBuffer* stream); @@ -1057,6 +1064,8 @@ class SrsPublishPacket : public SrsPacket public: SrsPublishPacket(); virtual ~SrsPublishPacket(); +public: + void set_command_object(SrsAmf0Any* v); // Decode functions for concrete packet to override. public: virtual srs_error_t decode(SrsBuffer* stream); @@ -1181,6 +1190,9 @@ class SrsPlayResPacket : public SrsPacket public: SrsPlayResPacket(); virtual ~SrsPlayResPacket(); +public: + void set_command_object(SrsAmf0Any* v); + void set_desc(SrsAmf0Object* v); // Encode functions for concrete packet to override. public: virtual int get_prefer_cid(); @@ -1204,6 +1216,8 @@ class SrsOnBWDonePacket : public SrsPacket public: SrsOnBWDonePacket(); virtual ~SrsOnBWDonePacket(); +public: + void set_args(SrsAmf0Any* v); // Encode functions for concrete packet to override. public: virtual int get_prefer_cid(); @@ -1232,6 +1246,9 @@ class SrsOnStatusCallPacket : public SrsPacket public: SrsOnStatusCallPacket(); virtual ~SrsOnStatusCallPacket(); +public: + void set_args(SrsAmf0Any* v); + void set_data(SrsAmf0Object* v); // Encode functions for concrete packet to override. public: virtual int get_prefer_cid(); @@ -1255,6 +1272,9 @@ class SrsOnStatusDataPacket : public SrsPacket public: SrsOnStatusDataPacket(); virtual ~SrsOnStatusDataPacket(); +public: + void set_data(SrsAmf0Object* v); + SrsAmf0Object* get_data(); // Encode functions for concrete packet to override. public: virtual int get_prefer_cid(); @@ -1303,6 +1323,8 @@ class SrsOnMetaDataPacket : public SrsPacket public: SrsOnMetaDataPacket(); virtual ~SrsOnMetaDataPacket(); +public: + void set_metadata(SrsAmf0Object* v); // Decode functions for concrete packet to override. public: virtual srs_error_t decode(SrsBuffer* stream); diff --git a/trunk/src/protocol/srs_protocol_utility.cpp b/trunk/src/protocol/srs_protocol_utility.cpp index 46c561e249..c52d41b906 100644 --- a/trunk/src/protocol/srs_protocol_utility.cpp +++ b/trunk/src/protocol/srs_protocol_utility.cpp @@ -665,6 +665,17 @@ bool srs_net_device_is_internet(const sockaddr* addr) } vector _srs_system_ips; +void srs_free_global_system_ips() +{ + vector& ips = _srs_system_ips; + + // Release previous IPs. + for (int i = 0; i < (int)ips.size(); i++) { + SrsIPAddress* ip = ips[i]; + srs_freep(ip); + } + ips.clear(); +} void discover_network_iface(ifaddrs* cur, vector& ips, stringstream& ss0, stringstream& ss1, bool ipv6, bool loopback) { @@ -702,14 +713,10 @@ void discover_network_iface(ifaddrs* cur, vector& ips, stringstre void retrieve_local_ips() { - vector& ips = _srs_system_ips; - // Release previous IPs. - for (int i = 0; i < (int)ips.size(); i++) { - SrsIPAddress* ip = ips[i]; - srs_freep(ip); - } - ips.clear(); + srs_free_global_system_ips(); + + vector& ips = _srs_system_ips; // Get the addresses. ifaddrs* ifap; diff --git a/trunk/src/utest/srs_utest.cpp b/trunk/src/utest/srs_utest.cpp index 2c15cf5418..df540bc236 100644 --- a/trunk/src/utest/srs_utest.cpp +++ b/trunk/src/utest/srs_utest.cpp @@ -81,6 +81,9 @@ srs_error_t prepare_main() { return err; } +// Free global data, for address sanitizer. +extern void srs_free_global_system_ips(); + // We could do something in the main of utest. // Copy from gtest-1.6.0/src/gtest_main.cc GTEST_API_ int main(int argc, char **argv) { @@ -97,7 +100,11 @@ GTEST_API_ int main(int argc, char **argv) { } testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + int r0 = RUN_ALL_TESTS(); + + srs_free_global_system_ips(); + + return r0; } MockEmptyLog::MockEmptyLog(SrsLogLevel l) diff --git a/trunk/src/utest/srs_utest_amf0.cpp b/trunk/src/utest/srs_utest_amf0.cpp index 78fe2c7676..5991932dd2 100644 --- a/trunk/src/utest/srs_utest_amf0.cpp +++ b/trunk/src/utest/srs_utest_amf0.cpp @@ -75,7 +75,7 @@ VOID TEST(ProtocolAMF0Test, ScenarioMain) EXPECT_EQ(0x03, bytes[0]); EXPECT_EQ(0x09, bytes[nb_bytes - 1]); } - SrsAutoFree(char, bytes); + SrsAutoFreeA(char, bytes); // decoding amf0 object from bytes // when user know the schema @@ -1301,7 +1301,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesString) EXPECT_TRUE(string("hello") == pp->to_str()); // For copy. - SrsAmf0Any* cp = p->copy(); + SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); EXPECT_TRUE(string("hello") == cp->to_str()); } @@ -1430,7 +1430,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesBoolean) EXPECT_FALSE(p->to_boolean()); // For copy. - SrsAmf0Any* cp = p->copy(); + SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); EXPECT_FALSE(cp->to_boolean()); } @@ -1527,7 +1527,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesNumber) EXPECT_TRUE(100.1 == p->to_number()); // For copy. - SrsAmf0Any* cp = p->copy(); + SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); EXPECT_TRUE(100.1 == cp->to_number()); } @@ -1790,6 +1790,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesObject) // For copy. SrsAmf0Any* cp = p->copy(); + SrsAutoFree(SrsAmf0Any, cp); EXPECT_TRUE(NULL != cp->to_object()); } @@ -1992,7 +1993,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesObjectEOF) HELPER_EXPECT_SUCCESS(pp->read(&b)); // For copy. - SrsAmf0Any* cp = p->copy(); + SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); EXPECT_TRUE(cp->is_object_eof()); } @@ -2084,7 +2085,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesEcmaArray) EXPECT_TRUE(NULL != pp->to_ecma_array()); // For copy. - SrsAmf0Any* cp = p->copy(); + SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); EXPECT_TRUE(NULL != cp->to_ecma_array()); } @@ -2211,7 +2212,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesStrictArray) EXPECT_TRUE(NULL != pp->to_strict_array()); // For copy. - SrsAmf0Any* cp = p->copy(); + SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); EXPECT_TRUE(NULL != cp->to_strict_array()); } diff --git a/trunk/src/utest/srs_utest_avc.cpp b/trunk/src/utest/srs_utest_avc.cpp index b464a73804..faf6d77456 100644 --- a/trunk/src/utest/srs_utest_avc.cpp +++ b/trunk/src/utest/srs_utest_avc.cpp @@ -222,7 +222,7 @@ VOID TEST(SrsAVCTest, H264IPBFrame) EXPECT_EQ(SrsVideoAvcFrameTraitSequenceHeader, uint8_t(flv[1])); EXPECT_EQ(01, flv[2]); EXPECT_EQ(02, flv[3]); EXPECT_EQ(03, flv[4]); EXPECT_STREQ("Hello", HELPER_ARR2STR(flv+5, 5).c_str()); - srs_freep(flv); + srs_freepa(flv); } // For muxing I/P/B frame. @@ -493,7 +493,7 @@ VOID TEST(SrsAVCTest, AACMuxToFLV) EXPECT_EQ(6, nb_flv); EXPECT_EQ(0x23, (uint8_t)flv[0]); EXPECT_STREQ("Hello", HELPER_ARR2STR(flv+1,5).c_str()); - srs_freep(flv); + srs_freepa(flv); } // For Opus frame. @@ -508,7 +508,7 @@ VOID TEST(SrsAVCTest, AACMuxToFLV) EXPECT_EQ(6, nb_flv); EXPECT_EQ(0xd3, (uint8_t)flv[0]); EXPECT_STREQ("Hello", HELPER_ARR2STR(flv+1,5).c_str()); - srs_freep(flv); + srs_freepa(flv); } // For Speex frame. @@ -523,7 +523,7 @@ VOID TEST(SrsAVCTest, AACMuxToFLV) EXPECT_EQ(6, nb_flv); EXPECT_EQ(0xb3, (uint8_t)flv[0]); EXPECT_STREQ("Hello", HELPER_ARR2STR(flv+1,5).c_str()); - srs_freep(flv); + srs_freepa(flv); } // For AAC frame. @@ -539,7 +539,7 @@ VOID TEST(SrsAVCTest, AACMuxToFLV) EXPECT_EQ(0xa3, (uint8_t)flv[0]); EXPECT_EQ(0x04, (uint8_t)flv[1]); EXPECT_STREQ("Hello", HELPER_ARR2STR(flv+2,5).c_str()); - srs_freep(flv); + srs_freepa(flv); } if (true) { SrsRawAacStream h; @@ -553,7 +553,7 @@ VOID TEST(SrsAVCTest, AACMuxToFLV) EXPECT_EQ(0xa6, (uint8_t)flv[0]); EXPECT_EQ(0x04, (uint8_t)flv[1]); EXPECT_STREQ("Hello", HELPER_ARR2STR(flv+2,5).c_str()); - srs_freep(flv); + srs_freepa(flv); } if (true) { SrsRawAacStream h; @@ -567,7 +567,7 @@ VOID TEST(SrsAVCTest, AACMuxToFLV) EXPECT_EQ(0xa5, (uint8_t)flv[0]); EXPECT_EQ(0x04, (uint8_t)flv[1]); EXPECT_STREQ("Hello", HELPER_ARR2STR(flv+2,5).c_str()); - srs_freep(flv); + srs_freepa(flv); } if (true) { SrsRawAacStream h; @@ -581,7 +581,7 @@ VOID TEST(SrsAVCTest, AACMuxToFLV) EXPECT_EQ(0xa7, (uint8_t)flv[0]); EXPECT_EQ(0x04, (uint8_t)flv[1]); EXPECT_STREQ("Hello", HELPER_ARR2STR(flv+2,5).c_str()); - srs_freep(flv); + srs_freepa(flv); } if (true) { SrsRawAacStream h; @@ -595,7 +595,7 @@ VOID TEST(SrsAVCTest, AACMuxToFLV) EXPECT_EQ(0xaf, (uint8_t)flv[0]); EXPECT_EQ(0x04, (uint8_t)flv[1]); EXPECT_STREQ("Hello", HELPER_ARR2STR(flv+2,5).c_str()); - srs_freep(flv); + srs_freepa(flv); } } diff --git a/trunk/src/utest/srs_utest_http.cpp b/trunk/src/utest/srs_utest_http.cpp index 52efafd594..967586183d 100644 --- a/trunk/src/utest/srs_utest_http.cpp +++ b/trunk/src/utest/srs_utest_http.cpp @@ -2081,6 +2081,7 @@ VOID TEST(ProtocolHTTPTest, QueryEscape) } else { HELPER_ASSERT_FAILED(SrsHttpUri::query_unescape(d.in, value)); } + srs_freep(d.err); } } diff --git a/trunk/src/utest/srs_utest_kernel.cpp b/trunk/src/utest/srs_utest_kernel.cpp index 16fc71a9ba..461f1aeca5 100644 --- a/trunk/src/utest/srs_utest_kernel.cpp +++ b/trunk/src/utest/srs_utest_kernel.cpp @@ -227,8 +227,11 @@ MockSrsFileReader::MockSrsFileReader(const char* src, int nb_src) seekable = true; uf = new MockSrsFile(); - uf->write((void*)src, nb_src, NULL); - uf->lseek(0, SEEK_SET, NULL); + srs_error_t err = uf->write((void*)src, nb_src, NULL); + srs_freep(err); + + err = uf->lseek(0, SEEK_SET, NULL); + srs_freep(err); } MockSrsFileReader::~MockSrsFileReader() @@ -255,20 +258,25 @@ bool MockSrsFileReader::is_open() int64_t MockSrsFileReader::tellg() { off_t offset = 0; - lseek(0, SEEK_CUR, &offset); + srs_error_t err = lseek(0, SEEK_CUR, &offset); + srs_freep(err); + return offset; } void MockSrsFileReader::skip(int64_t _size) { int64_t offset = tellg() + _size; - lseek(offset, SEEK_SET, NULL); + srs_error_t err = lseek(offset, SEEK_SET, NULL); + srs_freep(err); } int64_t MockSrsFileReader::seek2(int64_t _offset) { off_t offset = 0; - lseek(_offset, SEEK_SET, &offset); + srs_error_t err = lseek(_offset, SEEK_SET, &offset); + srs_freep(err); + return offset; } @@ -277,7 +285,8 @@ int64_t MockSrsFileReader::filesize() int64_t cur = tellg(); off_t offset = 0; - lseek(0, SEEK_END, &offset); + srs_error_t err = lseek(0, SEEK_END, &offset); + srs_freep(err); seek2(cur); return offset; @@ -298,7 +307,8 @@ srs_error_t MockSrsFileReader::lseek(off_t offset, int whence, off_t* seeked) void MockSrsFileReader::mock_append_data(const char* data, int size) { - uf->write((void*)data, size, NULL); + srs_error_t err = uf->write((void*)data, size, NULL); + srs_freep(err); } void MockSrsFileReader::mock_reset_offset() @@ -1127,6 +1137,11 @@ VOID TEST(KernelFLVTest, CoverVodStreamErrorCase) HELPER_EXPECT_FAILED(d.seek2(1)); } +} + +VOID TEST(KernelFLVTest, CoverVodStreamErrorCase2) +{ + srs_error_t err; if (true) { MockSrsFileReader r("HELLO", 5); @@ -3076,22 +3091,21 @@ VOID TEST(KernelUtility, RTMPUtils2) VOID TEST(KernelErrorTest, CoverAll) { + srs_error_t err; if (true) { - EXPECT_TRUE(srs_is_system_control_error(srs_error_new(ERROR_CONTROL_RTMP_CLOSE, "err"))); - EXPECT_TRUE(srs_is_system_control_error(srs_error_new(ERROR_CONTROL_REPUBLISH, "err"))); - EXPECT_TRUE(srs_is_system_control_error(srs_error_new(ERROR_CONTROL_REDIRECT, "err"))); + EXPECT_TRUE(srs_is_system_control_error(err = srs_error_new(ERROR_CONTROL_RTMP_CLOSE, "err"))); srs_freep(err); + EXPECT_TRUE(srs_is_system_control_error(err = srs_error_new(ERROR_CONTROL_REPUBLISH, "err"))); srs_freep(err); + EXPECT_TRUE(srs_is_system_control_error(err = srs_error_new(ERROR_CONTROL_REDIRECT, "err"))); srs_freep(err); } if (true) { - srs_error_t err = srs_error_new(ERROR_CONTROL_RTMP_CLOSE, "control error"); - EXPECT_TRUE(srs_is_system_control_error(err)); - srs_freep(err); + EXPECT_TRUE(srs_is_system_control_error(err = srs_error_new(ERROR_CONTROL_RTMP_CLOSE, "control error"))); srs_freep(err); } if (true) { - EXPECT_TRUE(srs_is_client_gracefully_close(srs_error_new(ERROR_SOCKET_READ, "err"))); - EXPECT_TRUE(srs_is_client_gracefully_close(srs_error_new(ERROR_SOCKET_READ_FULLY, "err"))); - EXPECT_TRUE(srs_is_client_gracefully_close(srs_error_new(ERROR_SOCKET_WRITE, "err"))); + EXPECT_TRUE(srs_is_client_gracefully_close(err = srs_error_new(ERROR_SOCKET_READ, "err"))); srs_freep(err); + EXPECT_TRUE(srs_is_client_gracefully_close(err = srs_error_new(ERROR_SOCKET_READ_FULLY, "err"))); srs_freep(err); + EXPECT_TRUE(srs_is_client_gracefully_close(err = srs_error_new(ERROR_SOCKET_WRITE, "err"))); srs_freep(err); } if (true) { @@ -5646,8 +5660,11 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) 0x32, 0x0f, 0x18, 0x31, 0x96, 0x01, 0x00, 0x05, 0x68, 0xeb, 0xec, 0xb2, 0x2c }; HELPER_EXPECT_SUCCESS(fmt.on_video(0, (char*)raw, sizeof(raw))); + + uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); + SrsAutoFreeA(uint8_t, cp); HELPER_EXPECT_SUCCESS(enc.write_sample( - SrsMp4HandlerTypeVIDE, fmt.video->frame_type, 0, 0, mock_copy_bytes(fmt.raw, fmt.nb_raw), fmt.nb_raw + SrsMp4HandlerTypeVIDE, fmt.video->frame_type, 0, 0, cp, fmt.nb_raw )); } @@ -5656,8 +5673,11 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) 0xaf, 0x00, 0x12, 0x10 }; HELPER_EXPECT_SUCCESS(fmt.on_audio(0, (char*)raw, sizeof(raw))); + + uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); + SrsAutoFreeA(uint8_t, cp); HELPER_EXPECT_SUCCESS(enc.write_sample( - SrsMp4HandlerTypeSOUN, 0x00, 0, 0, mock_copy_bytes(fmt.raw, fmt.nb_raw), fmt.nb_raw + SrsMp4HandlerTypeSOUN, 0x00, 0, 0, cp, fmt.nb_raw )); } @@ -5672,8 +5692,11 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e }; HELPER_EXPECT_SUCCESS(fmt.on_audio(0, (char*)raw, sizeof(raw))); + + uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); + SrsAutoFreeA(uint8_t, cp); HELPER_EXPECT_SUCCESS(enc.write_sample( - SrsMp4HandlerTypeSOUN, 0x00, 34, 34, mock_copy_bytes(fmt.raw, fmt.nb_raw), fmt.nb_raw + SrsMp4HandlerTypeSOUN, 0x00, 34, 34, cp, fmt.nb_raw )); } @@ -5691,8 +5714,11 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) 0xb2, 0x72, 0x5a }; HELPER_EXPECT_SUCCESS(fmt.on_video(0, (char*)raw, sizeof(raw))); + + uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); + SrsAutoFreeA(uint8_t, cp); HELPER_EXPECT_SUCCESS(enc.write_sample( - SrsMp4HandlerTypeVIDE, fmt.video->frame_type, 40, 40, mock_copy_bytes(fmt.raw, fmt.nb_raw), fmt.nb_raw + SrsMp4HandlerTypeVIDE, fmt.video->frame_type, 40, 40, cp, fmt.nb_raw )); } diff --git a/trunk/src/utest/srs_utest_protocol.cpp b/trunk/src/utest/srs_utest_protocol.cpp index da1cd56b15..1b15e9b206 100644 --- a/trunk/src/utest/srs_utest_protocol.cpp +++ b/trunk/src/utest/srs_utest_protocol.cpp @@ -85,6 +85,8 @@ MockBufferIO::MockBufferIO() MockBufferIO::~MockBufferIO() { + srs_freep(in_err); + srs_freep(out_err); } int MockBufferIO::length() @@ -397,7 +399,7 @@ VOID TEST(ProtocolHandshakeTest, VerifyFPC0C1) // manually validate the c1 // @see: calc_c1_digest char* c1s1_joined_bytes = new char[1536 -32]; - SrsAutoFree(char, c1s1_joined_bytes); + SrsAutoFreeA(char, c1s1_joined_bytes); HELPER_ASSERT_SUCCESS( c1.payload->copy_to(&c1, c1s1_joined_bytes, 1536 - 32, false)); bool is_valid; @@ -599,6 +601,13 @@ VOID TEST(ProtocolUtilityTest, GenerateTcUrl) EXPECT_STREQ("rtmp://demo:19351/live", tcUrl.c_str()); } +void srs_utest_free_message_array(SrsMessageArray* arr) +{ + for (int i = 0; i < arr->max; i++) { + srs_freep(arr->msgs[i]); + } +} + /** * shared ptr message array test */ @@ -614,6 +623,9 @@ VOID TEST(ProtocolMsgArrayTest, MessageArray) if (true) { SrsMessageArray arr(3); + + SrsMessageArray* parr = &arr; + SrsAutoFreeH(SrsMessageArray, parr, srs_utest_free_message_array); arr.msgs[0] = msg.copy(); EXPECT_EQ(1, msg.count()); @@ -624,18 +636,21 @@ VOID TEST(ProtocolMsgArrayTest, MessageArray) arr.msgs[2] = msg.copy(); EXPECT_EQ(3, msg.count()); } - EXPECT_EQ(3, msg.count()); + EXPECT_EQ(0, msg.count()); if (true) { SrsMessageArray arr(3); + + SrsMessageArray* parr = &arr; + SrsAutoFreeH(SrsMessageArray, parr, srs_utest_free_message_array); arr.msgs[0] = msg.copy(); - EXPECT_EQ(4, msg.count()); + EXPECT_EQ(1, msg.count()); arr.msgs[2] = msg.copy(); - EXPECT_EQ(5, msg.count()); + EXPECT_EQ(2, msg.count()); } - EXPECT_EQ(5, msg.count()); + EXPECT_EQ(0, msg.count()); } /** @@ -2720,7 +2735,7 @@ VOID TEST(ProtocolStackTest, ProtocolSendVMessage) uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; - SrsCommonMessage* msg = new SrsCommonMessage(); + SrsCommonMessage* msg = new SrsCommonMessage(); SrsAutoFree(SrsCommonMessage, msg); msg->size = sizeof(data); msg->payload = new char[msg->size]; memcpy(msg->payload, data, msg->size); @@ -2831,7 +2846,7 @@ VOID TEST(ProtocolStackTest, ProtocolSendSrsCreateStreamPacket) SrsProtocol proto(&bio); SrsCreateStreamPacket* pkt = new SrsCreateStreamPacket(); - pkt->command_object = SrsAmf0Any::null(); + pkt->set_command_object(SrsAmf0Any::null()); HELPER_EXPECT_SUCCESS(proto.send_and_free_packet(pkt, 0)); char buf[] = { @@ -2856,7 +2871,7 @@ VOID TEST(ProtocolStackTest, ProtocolSendSrsFMLEStartPacket) SrsFMLEStartPacket* pkt = new SrsFMLEStartPacket(); pkt->command_name = "FMLEStart"; - pkt->command_object = SrsAmf0Any::null(); + pkt->set_command_object(SrsAmf0Any::null()); pkt->stream_name = "livestream"; HELPER_EXPECT_SUCCESS(proto.send_and_free_packet(pkt, 0)); @@ -2885,8 +2900,8 @@ VOID TEST(ProtocolStackTest, ProtocolSendSrsFMLEStartResPacket) SrsFMLEStartResPacket* pkt = new SrsFMLEStartResPacket(1); pkt->command_name = "FMLEStart"; - pkt->command_object = SrsAmf0Any::null(); - pkt->args = args; + pkt->set_command_object(SrsAmf0Any::null()); + pkt->set_args(args); args->set("stream" , SrsAmf0Any::str("livestream")); args->set("start" , SrsAmf0Any::number(0)); @@ -2914,7 +2929,7 @@ VOID TEST(ProtocolStackTest, ProtocolSendSrsPublishPacket) SrsPublishPacket* pkt = new SrsPublishPacket(); pkt->command_name = "publish"; - pkt->command_object = SrsAmf0Any::null(); + pkt->set_command_object(SrsAmf0Any::null()); pkt->stream_name = "livestream"; pkt->type = "live"; @@ -2945,8 +2960,8 @@ VOID TEST(ProtocolStackTest, ProtocolSendSrsPlayResPacket) SrsPlayResPacket* pkt = new SrsPlayResPacket(); pkt->command_name = "_result"; - pkt->command_object = SrsAmf0Any::null(); - pkt->desc = args; + pkt->set_command_object(SrsAmf0Any::null()); + pkt->set_desc(args); args->set("stream" , SrsAmf0Any::str("livestream")); args->set("start" , SrsAmf0Any::number(0)); @@ -2979,7 +2994,7 @@ VOID TEST(ProtocolStackTest, ProtocolSendSrsOnBWDonePacket) SrsOnBWDonePacket* pkt = new SrsOnBWDonePacket(); pkt->command_name = "onBWDone"; - pkt->args = SrsAmf0Any::null(); + pkt->set_args(SrsAmf0Any::null()); HELPER_EXPECT_SUCCESS(proto.send_and_free_packet(pkt, 0)); char buf[] = { @@ -3009,8 +3024,8 @@ VOID TEST(ProtocolStackTest, ProtocolSendSrsOnStatusCallPacket) SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket(); pkt->command_name = "onStatus"; - pkt->args = SrsAmf0Any::null(); - pkt->data = args; + pkt->set_args(SrsAmf0Any::null()); + pkt->set_data(args); HELPER_EXPECT_SUCCESS(proto.send_and_free_packet(pkt, 0)); char buf[] = { @@ -3045,7 +3060,7 @@ VOID TEST(ProtocolStackTest, ProtocolSendSrsOnStatusDataPacket) SrsOnStatusDataPacket* pkt = new SrsOnStatusDataPacket(); pkt->command_name = "onData"; - pkt->data = args; + pkt->set_data(args); HELPER_EXPECT_SUCCESS(proto.send_and_free_packet(pkt, 0)); char buf[] = { @@ -3104,7 +3119,7 @@ VOID TEST(ProtocolStackTest, ProtocolSendSrsOnMetaDataPacket) SrsOnMetaDataPacket* pkt = new SrsOnMetaDataPacket(); pkt->name = "onMetaData"; - pkt->metadata = args; + pkt->set_metadata(args); HELPER_EXPECT_SUCCESS(proto.send_and_free_packet(pkt, 0)); uint8_t buf[] = { @@ -3299,7 +3314,7 @@ VOID TEST(ProtocolStackTest, ProtocolAckSizeFlow) } if (true) { - SrsCommonMessage* msg = new SrsCommonMessage(); + SrsCommonMessage* msg = new SrsCommonMessage(); SrsAutoFree(SrsCommonMessage, msg); msg->header.payload_length = msg->size = 4096; msg->payload = new char[msg->size]; @@ -3340,15 +3355,14 @@ VOID TEST(ProtocolStackTest, ProtocolAckSizeFlow) } // recv auto send acked size. #1 if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); ASSERT_TRUE(msg->header.is_ackledgement()); } // send again if (true) { - SrsCommonMessage* msg = new SrsCommonMessage(); + SrsCommonMessage* msg = new SrsCommonMessage(); SrsAutoFree(SrsCommonMessage, msg); msg->header.payload_length = msg->size = 4096; msg->payload = new char[msg->size]; @@ -3425,12 +3439,11 @@ VOID TEST(ProtocolStackTest, ProtocolPingFlow) } // recv ping if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); ASSERT_TRUE(msg->header.is_user_control_message()); - SrsPacket* pkt = NULL; + SrsPacket* pkt = NULL; SrsAutoFree(SrsPacket, pkt); HELPER_ASSERT_SUCCESS(proto.decode_message(msg, &pkt)); SrsUserControlPacket* spkt = dynamic_cast(pkt); ASSERT_TRUE(spkt != NULL); diff --git a/trunk/src/utest/srs_utest_rtc.cpp b/trunk/src/utest/srs_utest_rtc.cpp index 42f8993c1c..467347f766 100644 --- a/trunk/src/utest/srs_utest_rtc.cpp +++ b/trunk/src/utest/srs_utest_rtc.cpp @@ -68,7 +68,7 @@ VOID TEST(KernelRTCTest, RtpSTAPPayloadException) EXPECT_TRUE(nalu_type == kStapA); ISrsRtpPayloader* payload = new SrsRtpSTAPPayload(); - EXPECT_TRUE((err = payload->decode(&buf)) != srs_success); + HELPER_ASSERT_FAILED(payload->decode(&buf)); srs_freep(payload); } @@ -710,7 +710,8 @@ VOID TEST(KernelRTCTest, NACKFetchRTPPacket) SrsRtcPlayStream play(&s, SrsContextId()); SrsRtcTrackDescription ds; - SrsRtcVideoSendTrack *track = new SrsRtcVideoSendTrack(&s, &ds); + SrsRtcVideoSendTrack* track = new SrsRtcVideoSendTrack(&s, &ds); + SrsAutoFree(SrsRtcVideoSendTrack, track); // The RTP queue will free the packet. if (true) { diff --git a/trunk/src/utest/srs_utest_rtmp.cpp b/trunk/src/utest/srs_utest_rtmp.cpp index 09e2662233..b668f193a9 100644 --- a/trunk/src/utest/srs_utest_rtmp.cpp +++ b/trunk/src/utest/srs_utest_rtmp.cpp @@ -435,10 +435,9 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages) // Always response ACK message. HELPER_EXPECT_SUCCESS(p.set_in_window_ack_size(1)); - SrsCommonMessage* msg; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); io.in_buffer.append(&bytes); HELPER_EXPECT_FAILED(p.recv_message(&msg)); - srs_freep(msg); } } @@ -884,8 +883,7 @@ VOID TEST(ProtocolRTMPTest, RecvMessage) uint8_t bytes[] = {0x01, 0x00, 0x00}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg; - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_FAILED(p.recv_message(&msg)); } @@ -896,8 +894,7 @@ VOID TEST(ProtocolRTMPTest, RecvMessage) uint8_t bytes[] = {0x00, 0x00}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg; - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_FAILED(p.recv_message(&msg)); } @@ -908,8 +905,7 @@ VOID TEST(ProtocolRTMPTest, RecvMessage) uint8_t bytes[] = {0x00}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg; - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_FAILED(p.recv_message(&msg)); } @@ -917,8 +913,7 @@ VOID TEST(ProtocolRTMPTest, RecvMessage) MockBufferIO io; SrsProtocol p(&io); - SrsCommonMessage* msg; - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_FAILED(p.recv_message(&msg)); } } @@ -934,8 +929,7 @@ VOID TEST(ProtocolRTMPTest, RecvMessage2) uint8_t bytes[] = {0x03, 0,0,0, 0,0,4, 0, 0,0,0,0, 1,2,3}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg; - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_FAILED(p.recv_message(&msg)); } @@ -948,10 +942,12 @@ VOID TEST(ProtocolRTMPTest, RecvMessage2) uint8_t bytes[] = {0x03, 0,0,0, 0,0,4, 0, 0,0,0,0, 1,2,3}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg; - SrsAutoFree(SrsCommonMessage, msg); - HELPER_EXPECT_FAILED(p.recv_message(&msg)); + if (true) { + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + HELPER_EXPECT_FAILED(p.recv_message(&msg)); + } + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); uint8_t bytes2[] = {0x43, 0,0,0, 0,0,5, 0, 0,0,0,0, 1,2,3}; io.in_buffer.append((char*)bytes2, sizeof(bytes2)); HELPER_EXPECT_FAILED(p.recv_message(&msg)); @@ -964,8 +960,7 @@ VOID TEST(ProtocolRTMPTest, RecvMessage2) uint8_t bytes[] = {0x03}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg; - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_FAILED(p.recv_message(&msg)); } @@ -976,8 +971,7 @@ VOID TEST(ProtocolRTMPTest, RecvMessage2) uint8_t bytes[] = {0x43, 0,0,0, 0,0,0, 0}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg; - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_FAILED(p.recv_message(&msg)); } } @@ -1060,8 +1054,7 @@ VOID TEST(ProtocolRTMPTest, RecvMessage4) io.in_buffer.append(&io.out_buffer); - SrsCommonMessage* msg; - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); EXPECT_EQ(256, p.out_chunk_size); @@ -1078,8 +1071,7 @@ VOID TEST(ProtocolRTMPTest, RecvMessage4) io.in_buffer.append(&io.out_buffer); - SrsCommonMessage* msg; - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); EXPECT_EQ(256, p.in_buffer_length); @@ -1534,9 +1526,8 @@ VOID TEST(ProtocolRTMPTest, ServerCommandMessage) SrsProtocol p(&tio); - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); - srs_freep(msg); EXPECT_EQ(1024, p.in_chunk_size); } @@ -2239,7 +2230,7 @@ VOID TEST(ProtocolRTMPTest, CoverAll) EXPECT_EQ(0, r.get_recv_bytes()); EXPECT_EQ(0, r.get_send_bytes()); - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_FAILED(r.recv_message(&msg)); SrsCallPacket* pkt = new SrsCallPacket(); @@ -2262,7 +2253,7 @@ VOID TEST(ProtocolRTMPTest, CoverAll) EXPECT_EQ(0, r.get_recv_bytes()); EXPECT_EQ(0, r.get_send_bytes()); - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_FAILED(r.recv_message(&msg)); SrsCallPacket* pkt = new SrsCallPacket(); @@ -2585,8 +2576,8 @@ VOID TEST(ProtocolRTMPTest, ConnectAppWithArgs) if (true) { tio.in_buffer.append(&io.out_buffer); - SrsCommonMessage* msg = NULL; - SrsConnectAppPacket* pkt = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsConnectAppPacket* pkt = NULL; SrsAutoFree(SrsConnectAppPacket, pkt); HELPER_ASSERT_SUCCESS(p.expect_message(&msg, &pkt)); SrsAmf0Any* prop = pkt->command_object->get_property("tcUrl"); @@ -2616,9 +2607,8 @@ VOID TEST(ProtocolRTMPTest, AgentMessageCodec) } if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); - srs_freep(msg); } } @@ -2633,14 +2623,11 @@ VOID TEST(ProtocolRTMPTest, AgentMessageCodec) } if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_ASSERT_SUCCESS(p.recv_message(&msg)); - SrsPacket* pkt = NULL; + SrsPacket* pkt = NULL; SrsAutoFree(SrsPacket, pkt); HELPER_EXPECT_SUCCESS(p.decode_message(msg, &pkt)); - - srs_freep(msg); - srs_freep(pkt); } } @@ -2655,9 +2642,8 @@ VOID TEST(ProtocolRTMPTest, AgentMessageCodec) } if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); - srs_freep(msg); } } @@ -2672,14 +2658,11 @@ VOID TEST(ProtocolRTMPTest, AgentMessageCodec) } if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_ASSERT_SUCCESS(p.recv_message(&msg)); - SrsPacket* pkt = NULL; + SrsPacket* pkt = NULL; SrsAutoFree(SrsPacket, pkt); HELPER_EXPECT_SUCCESS(p.decode_message(msg, &pkt)); - - srs_freep(msg); - srs_freep(pkt); } } } @@ -2733,17 +2716,15 @@ VOID TEST(ProtocolRTMPTest, CheckStreamID) } if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); EXPECT_EQ(1, msg->header.stream_id); - srs_freep(msg); } if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); EXPECT_EQ(2, msg->header.stream_id); - srs_freep(msg); } } } @@ -2767,9 +2748,8 @@ VOID TEST(ProtocolRTMPTest, AgentMessageTransform) } if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); - srs_freep(msg); } } @@ -2788,9 +2768,8 @@ VOID TEST(ProtocolRTMPTest, AgentMessageTransform) } if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); - srs_freep(msg); } } @@ -2809,9 +2788,8 @@ VOID TEST(ProtocolRTMPTest, AgentMessageTransform) } if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); - srs_freep(msg); } } @@ -2830,9 +2808,8 @@ VOID TEST(ProtocolRTMPTest, AgentMessageTransform) } if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); - srs_freep(msg); } } } @@ -2865,9 +2842,8 @@ VOID TEST(ProtocolRTMPTest, MergeReadHandler) r.set_merge_read(true, &h); if (true) { - SrsCommonMessage* msg = NULL; + SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); HELPER_EXPECT_SUCCESS(r.recv_message(&msg)); - srs_freep(msg); } EXPECT_TRUE(h.nn > 0); @@ -2925,6 +2901,8 @@ VOID TEST(ProtocolRTMPTest, CreateRTMPMessage) } } +extern void srs_utest_free_message_array(SrsMessageArray* arr); + VOID TEST(ProtocolRTMPTest, OthersAll) { if (true) { @@ -2995,6 +2973,10 @@ VOID TEST(ProtocolRTMPTest, OthersAll) if (true) { SrsMessageArray h(10); + + SrsMessageArray* parr = &h; + SrsAutoFreeH(SrsMessageArray, parr, srs_utest_free_message_array); + h.msgs[0] = new SrsSharedPtrMessage(); h.msgs[1] = new SrsSharedPtrMessage(); EXPECT_TRUE(NULL != h.msgs[0]); diff --git a/trunk/src/utest/srs_utest_service.cpp b/trunk/src/utest/srs_utest_service.cpp index b93cc2f9d9..f5f0592069 100644 --- a/trunk/src/utest/srs_utest_service.cpp +++ b/trunk/src/utest/srs_utest_service.cpp @@ -636,6 +636,7 @@ VOID TEST(HTTPServerTest, ContentLength) SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE)); ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); + SrsAutoFree(ISrsHttpMessage, msg); char buf[32]; ssize_t nread = 0; ISrsHttpResponseReader* r = msg->body_reader(); @@ -664,6 +665,7 @@ VOID TEST(HTTPServerTest, HTTPChunked) SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE)); ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); + SrsAutoFree(ISrsHttpMessage, msg); char buf[32]; ssize_t nread = 0; ISrsHttpResponseReader* r = msg->body_reader(); @@ -720,6 +722,7 @@ VOID TEST(HTTPServerTest, InfiniteChunked) SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE)); ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); + SrsAutoFree(ISrsHttpMessage, msg); char buf[32]; ssize_t nread = 0; ISrsHttpResponseReader* r = msg->body_reader(); @@ -1110,7 +1113,7 @@ VOID TEST(TCPServerTest, CoverUtility) hints.ai_family = AF_INET; addrinfo* r = NULL; - SrsAutoFree(addrinfo, r); + SrsAutoFreeH(addrinfo, r, freeaddrinfo); ASSERT_TRUE(!getaddrinfo("127.0.0.1", NULL, &hints, &r)); EXPECT_FALSE(srs_net_device_is_internet((sockaddr*)r->ai_addr)); @@ -1123,7 +1126,7 @@ VOID TEST(TCPServerTest, CoverUtility) hints.ai_family = AF_INET; addrinfo* r = NULL; - SrsAutoFree(addrinfo, r); + SrsAutoFreeH(addrinfo, r, freeaddrinfo); ASSERT_TRUE(!getaddrinfo("192.168.0.1", NULL, &hints, &r)); EXPECT_FALSE(srs_net_device_is_internet((sockaddr*)r->ai_addr)); @@ -1168,7 +1171,7 @@ VOID TEST(TCPServerTest, CoverUtility) hints.ai_family = AF_INET6; addrinfo* r = NULL; - SrsAutoFree(addrinfo, r); + SrsAutoFreeH(addrinfo, r, freeaddrinfo); ASSERT_TRUE(!getaddrinfo("2001:da8:6000:291:21f:d0ff:fed4:928c", NULL, &hints, &r)); EXPECT_TRUE(srs_net_device_is_internet((sockaddr*)r->ai_addr)); @@ -1179,7 +1182,7 @@ VOID TEST(TCPServerTest, CoverUtility) hints.ai_family = AF_INET6; addrinfo* r = NULL; - SrsAutoFree(addrinfo, r); + SrsAutoFreeH(addrinfo, r, freeaddrinfo); ASSERT_TRUE(!getaddrinfo("3ffe:dead:beef::1", NULL, &hints, &r)); EXPECT_TRUE(srs_net_device_is_internet((sockaddr*)r->ai_addr)); @@ -1192,7 +1195,7 @@ VOID TEST(TCPServerTest, CoverUtility) hints.ai_family = AF_INET6; addrinfo* r = NULL; - SrsAutoFree(addrinfo, r); + SrsAutoFreeH(addrinfo, r, freeaddrinfo); ASSERT_TRUE(!getaddrinfo("::", NULL, &hints, &r)); EXPECT_FALSE(srs_net_device_is_internet((sockaddr*)r->ai_addr)); @@ -1205,7 +1208,7 @@ VOID TEST(TCPServerTest, CoverUtility) hints.ai_family = AF_INET6; addrinfo* r = NULL; - SrsAutoFree(addrinfo, r); + SrsAutoFreeH(addrinfo, r, freeaddrinfo); ASSERT_TRUE(!getaddrinfo("fec0::", NULL, &hints, &r)); EXPECT_FALSE(srs_net_device_is_internet((sockaddr*)r->ai_addr)); @@ -1218,7 +1221,7 @@ VOID TEST(TCPServerTest, CoverUtility) hints.ai_family = AF_INET6; addrinfo* r = NULL; - SrsAutoFree(addrinfo, r); + SrsAutoFreeH(addrinfo, r, freeaddrinfo); ASSERT_TRUE(!getaddrinfo("FE80::", NULL, &hints, &r)); EXPECT_FALSE(srs_net_device_is_internet((sockaddr*)r->ai_addr)); @@ -1231,7 +1234,7 @@ VOID TEST(TCPServerTest, CoverUtility) hints.ai_family = AF_INET6; addrinfo* r = NULL; - SrsAutoFree(addrinfo, r); + SrsAutoFreeH(addrinfo, r, freeaddrinfo); ASSERT_TRUE(!getaddrinfo("::1", NULL, &hints, &r)); EXPECT_FALSE(srs_net_device_is_internet((sockaddr*)r->ai_addr)); diff --git a/trunk/src/utest/srs_utest_srt.cpp b/trunk/src/utest/srs_utest_srt.cpp index ff2cd0a26a..773df5cb8f 100644 --- a/trunk/src/utest/srs_utest_srt.cpp +++ b/trunk/src/utest/srs_utest_srt.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -194,6 +195,7 @@ VOID TEST(ServiceStSRTTest, ListenConnectAccept) HELPER_EXPECT_SUCCESS(srs_srt_socket(&srt_client_fd)); SrsSrtSocket* srt_client_socket = new SrsSrtSocket(_srt_eventloop->poller(), srt_client_fd); + SrsAutoFree(SrsSrtSocket, srt_client_socket); // No client connected, accept will timeout. srs_srt_t srt_fd = srs_srt_socket_invalid(); @@ -202,6 +204,7 @@ VOID TEST(ServiceStSRTTest, ListenConnectAccept) err = srt_server.accept(&srt_fd); EXPECT_EQ(srs_error_code(err), ERROR_SRT_TIMEOUT); EXPECT_EQ(srt_fd, srs_srt_socket_invalid()); + srs_freep(err); // Client connect to server HELPER_EXPECT_SUCCESS(srt_client_socket->connect(server_ip, server_port)); @@ -308,6 +311,7 @@ VOID TEST(ServiceStSRTTest, ReadWrite) // Recv msg from client, but client no send any msg, so will be timeout. err = srt_server_accepted_socket->recvmsg(buf, sizeof(buf), &nb_read); EXPECT_EQ(srs_error_code(err), ERROR_SRT_TIMEOUT); + srs_freep(err); } } @@ -524,7 +528,7 @@ VOID TEST(ServiceSRTTest, Encrypt) if (true) { int pbkeylens[4] = {0, 16, 24, 32}; - for (int i = 0; i < sizeof(pbkeylens) / sizeof(pbkeylens[0]); ++i) { + for (int i = 0; i < (int)(sizeof(pbkeylens) / sizeof(pbkeylens[0])); ++i) { srs_srt_t srt_client_fd = srs_srt_socket_invalid(); HELPER_EXPECT_SUCCESS(srs_srt_socket_with_default_option(&srt_client_fd)); HELPER_EXPECT_SUCCESS(srs_srt_set_streamid(srt_client_fd, streamid));