Browse files

Fixed freeze on files with cutted-off unneeded instruments

  • Loading branch information...
1 parent 9bde522 commit 083445b685d9bd476628fe21b4cba270ca3e3a0f denis111 committed Dec 3, 2007
Showing with 17 additions and 3 deletions.
  1. +17 −3 loaders/load_xm.c
View
20 loaders/load_xm.c
@@ -20,7 +20,7 @@
/*==============================================================================
- $Id: load_xm.c,v 1.3 2005/04/07 19:57:38 realtech Exp $
+ $Id: load_xm.c,v 1.4 2007/12/03 20:35:43 denis111 Exp $
Fasttracker (XM) module loader
@@ -443,7 +443,7 @@ static void FixEnvelope(ENVPT *cur, int pts)
static BOOL LoadInstruments(void)
{
- int t,u;
+ int t,u, ck;
INSTRUMENT *d;
ULONG next=0;
UWORD wavcnt=0;
@@ -460,6 +460,13 @@ static BOOL LoadInstruments(void)
headend = _mm_ftell(modreader);
ih.size = _mm_read_I_ULONG(modreader);
headend += ih.size;
+ ck = _mm_ftell(modreader);
+ _mm_fseek(modreader,0,SEEK_END);
+ if ((headend<0) || (_mm_ftell(modreader)<headend) || (headend<ck)) {
+ _mm_fseek(modreader,ck,SEEK_SET);
+ break;
+ }
+ _mm_fseek(modreader,ck,SEEK_SET);
_mm_read_string(ih.name, 22, modreader);
ih.type = _mm_read_UBYTE(modreader);
ih.numsmp = _mm_read_I_UWORD(modreader);
@@ -493,7 +500,7 @@ static BOOL LoadInstruments(void)
/* read the remainder of the header
(2 bytes for 1.03, 22 for 1.04) */
- for(u=headend-_mm_ftell(modreader);u;u--) _mm_read_UBYTE(modreader);
+ if (headend>=_mm_ftell(modreader)) for(u=headend-_mm_ftell(modreader);u;u--) _mm_read_UBYTE(modreader);
/* we can't trust the envelope point count here, as some
modules have incorrect values (K_OSPACE.XM reports 32 volume
@@ -620,6 +627,13 @@ static BOOL LoadInstruments(void)
of.numsmp+=ih.numsmp;
} else {
/* read the remainder of the header */
+ ck = _mm_ftell(modreader);
+ _mm_fseek(modreader,0,SEEK_END);
+ if ((headend<0) || (_mm_ftell(modreader)<headend) || (headend<ck)) {
+ _mm_fseek(modreader,ck,SEEK_SET);
+ break;
+ }
+ _mm_fseek(modreader,ck,SEEK_SET);
for(u=headend-_mm_ftell(modreader);u;u--) _mm_read_UBYTE(modreader);
if(_mm_eof(modreader)) {

0 comments on commit 083445b

Please sign in to comment.