@@ -346,7 +346,7 @@ static void Fill_IMA_ADPCM_block(Uint8 *decoded, Uint8 *encoded,
346346static int IMA_ADPCM_decode (Uint8 * * audio_buf , Uint32 * audio_len )
347347{
348348 struct IMA_ADPCM_decodestate * state ;
349- Uint8 * freeable , * encoded , * encoded_end , * decoded ;
349+ Uint8 * freeable , * encoded , * encoded_end , * decoded , * decoded_end ;
350350 Sint32 encoded_len , samplesleft ;
351351 unsigned int c , channels ;
352352
@@ -373,6 +373,7 @@ static int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
373373 return (-1 );
374374 }
375375 decoded = * audio_buf ;
376+ decoded_end = decoded + * audio_len ;
376377
377378 /* Get ready... Go! */
378379 while ( encoded_len >= IMA_ADPCM_state .wavefmt .blockalign ) {
@@ -392,6 +393,7 @@ static int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
392393 }
393394
394395 /* Store the initial sample we start with */
396+ if (decoded + 2 > decoded_end ) goto invalid_size ;
395397 decoded [0 ] = (Uint8 )(state [c ].sample & 0xFF );
396398 decoded [1 ] = (Uint8 )(state [c ].sample >>8 );
397399 decoded += 2 ;
@@ -402,6 +404,8 @@ static int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
402404 while ( samplesleft > 0 ) {
403405 for ( c = 0 ; c < channels ; ++ c ) {
404406 if (encoded + 4 > encoded_end ) goto invalid_size ;
407+ if (decoded + 4 * 4 * channels > decoded_end )
408+ goto invalid_size ;
405409 Fill_IMA_ADPCM_block (decoded , encoded ,
406410 c , channels , & state [c ]);
407411 encoded += 4 ;
0 commit comments