Prevent double free on encoder/decoder init failure.

This commit is contained in:
Gregory Maxwell 2011-09-04 04:43:11 -04:00
parent a40721a92e
commit e03af4423d
2 changed files with 9 additions and 20 deletions

View file

@ -89,9 +89,8 @@ int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
OPUS_CLEAR((char*)st, opus_decoder_get_size(channels)); OPUS_CLEAR((char*)st, opus_decoder_get_size(channels));
/* Initialize SILK encoder */ /* Initialize SILK encoder */
ret = silk_Get_Decoder_Size(&silkDecSizeBytes); ret = silk_Get_Decoder_Size(&silkDecSizeBytes);
if( ret ) { if(ret)return OPUS_INTERNAL_ERROR;
return OPUS_INTERNAL_ERROR;
}
silkDecSizeBytes = align(silkDecSizeBytes); silkDecSizeBytes = align(silkDecSizeBytes);
st->silk_dec_offset = align(sizeof(OpusDecoder)); st->silk_dec_offset = align(sizeof(OpusDecoder));
st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes; st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes;
@ -103,22 +102,17 @@ int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
/* Reset decoder */ /* Reset decoder */
ret = silk_InitDecoder( silk_dec ); ret = silk_InitDecoder( silk_dec );
if( ret ) { if(ret)return OPUS_INTERNAL_ERROR;
goto failure;
}
/* Initialize CELT decoder */ /* Initialize CELT decoder */
ret = celt_decoder_init(celt_dec, Fs, channels); ret = celt_decoder_init(celt_dec, Fs, channels);
if (ret != OPUS_OK) if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR;
goto failure;
celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0)); celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
st->prev_mode = 0; st->prev_mode = 0;
st->frame_size = Fs/400; st->frame_size = Fs/400;
return OPUS_OK; return OPUS_OK;
failure:
opus_free(st);
return OPUS_INTERNAL_ERROR;
} }
OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error) OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error)

View file

@ -162,8 +162,7 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
st->Fs = Fs; st->Fs = Fs;
ret = silk_InitEncoder( silk_enc, &st->silk_mode ); ret = silk_InitEncoder( silk_enc, &st->silk_mode );
if (ret) if(ret)return OPUS_INTERNAL_ERROR;
goto failure;
/* default SILK parameters */ /* default SILK parameters */
st->silk_mode.nChannelsAPI = channels; st->silk_mode.nChannelsAPI = channels;
@ -183,8 +182,8 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
/* Create CELT encoder */ /* Create CELT encoder */
/* Initialize CELT encoder */ /* Initialize CELT encoder */
err = celt_encoder_init(celt_enc, Fs, channels); err = celt_encoder_init(celt_enc, Fs, channels);
if (err != OPUS_OK) if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR;
goto failure;
celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0)); celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
st->use_vbr = 0; st->use_vbr = 0;
@ -213,10 +212,6 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
st->bandwidth = OPUS_BANDWIDTH_FULLBAND; st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
return OPUS_OK; return OPUS_OK;
failure:
opus_free(st);
return OPUS_INTERNAL_ERROR;
} }
static unsigned char gen_toc(int mode, int framerate, int bandwidth, int silk_bandwidth, int channels) static unsigned char gen_toc(int mode, int framerate, int bandwidth, int silk_bandwidth, int channels)