@@ -222,11 +222,12 @@ static struct IMA_ADPCM_decoder {
222222 struct IMA_ADPCM_decodestate state [2 ];
223223} IMA_ADPCM_state ;
224224
225- static int InitIMA_ADPCM (WaveFMT * format )
225+ static int InitIMA_ADPCM (WaveFMT * format , int length )
226226{
227- Uint8 * rogue_feel ;
227+ Uint8 * rogue_feel , * rogue_feel_end ;
228228
229229 /* Set the rogue pointer to the IMA_ADPCM specific data */
230+ if (length < sizeof (* format )) goto too_short ;
230231 IMA_ADPCM_state .wavefmt .encoding = SDL_SwapLE16 (format -> encoding );
231232 IMA_ADPCM_state .wavefmt .channels = SDL_SwapLE16 (format -> channels );
232233 IMA_ADPCM_state .wavefmt .frequency = SDL_SwapLE32 (format -> frequency );
@@ -235,11 +236,16 @@ static int InitIMA_ADPCM(WaveFMT *format)
235236 IMA_ADPCM_state .wavefmt .bitspersample =
236237 SDL_SwapLE16 (format -> bitspersample );
237238 rogue_feel = (Uint8 * )format + sizeof (* format );
239+ rogue_feel_end = (Uint8 * )format + length ;
238240 if ( sizeof (* format ) == 16 ) {
239241 rogue_feel += sizeof (Uint16 );
240242 }
243+ if (rogue_feel + 2 > rogue_feel_end ) goto too_short ;
241244 IMA_ADPCM_state .wSamplesPerBlock = ((rogue_feel [1 ]<<8 )|rogue_feel [0 ]);
242245 return (0 );
246+ too_short :
247+ SDL_SetError ("Unexpected length of a chunk with an IMA ADPCM format" );
248+ return (-1 );
243249}
244250
245251static Sint32 IMA_ADPCM_nibble (struct IMA_ADPCM_decodestate * state ,Uint8 nybble )
@@ -471,7 +477,7 @@ SDL_AudioSpec * SDL_LoadWAV_RW (SDL_RWops *src, int freesrc,
471477 break ;
472478 case IMA_ADPCM_CODE :
473479 /* Try to understand this */
474- if ( InitIMA_ADPCM (format ) < 0 ) {
480+ if ( InitIMA_ADPCM (format , lenread ) < 0 ) {
475481 was_error = 1 ;
476482 goto done ;
477483 }
0 commit comments