Skip to content

Commit

Permalink
Support address sanitizer for utest and fix some leaks. (#3242)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
winlinvip authored Nov 18, 2022
1 parent be0241e commit 368356c
Show file tree
Hide file tree
Showing 18 changed files with 360 additions and 154 deletions.
7 changes: 7 additions & 0 deletions trunk/src/app/srs_app_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ SrsResourceManager::~SrsResourceManager()

clear();

// Free all objects not in zombies.
std::vector<ISrsResource*>::iterator it;
for (it = conns_.begin(); it != conns_.end(); ++it) {
ISrsResource* resource = *it;
srs_freep(resource);
}

srs_freepa(conns_level0_cache_);
}

Expand Down
55 changes: 55 additions & 0 deletions trunk/src/kernel/srs_kernel_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions trunk/src/kernel/srs_kernel_codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);

/**
Expand Down
29 changes: 12 additions & 17 deletions trunk/src/kernel/srs_kernel_mp4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -5375,8 +5377,6 @@ srs_error_t SrsMp4Decoder::initialize(ISrsReadSeeker* rs)
}
break;
}

srs_freep(box);
}

if (brand == SrsMp4BoxBrandForbidden) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions trunk/src/protocol/srs_protocol_http_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
72 changes: 72 additions & 0 deletions trunk/src/protocol/srs_protocol_rtmp_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 368356c

Please sign in to comment.