diff minijss/jloadxm.c @ 1402:b47109fce375

Make JSSExtInstrument.instConvSamples dynamically allocated and add check for XM_MaxInstSamples in XM loader.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 30 Oct 2017 18:12:38 +0200
parents 496d04ab495f
children e0f9200b94ad
line wrap: on
line diff
--- a/minijss/jloadxm.c	Mon Oct 30 18:01:25 2017 +0200
+++ b/minijss/jloadxm.c	Mon Oct 30 18:12:38 2017 +0200
@@ -773,6 +773,13 @@
         return DMERR_OK;
     }
 
+    if (xmI1.nsamples > XM_MaxInstSamples)
+    {
+        JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
+            "Extended instrument #%d has invalid number of samples %d.\n",
+            neinst, xmI1.nsamples);
+    }
+
     // Allocate instrument
     JSSDEBUG("Allocating extended instrument #%d [%d]\n",
         neinst, module->nextInstruments);
@@ -860,11 +867,18 @@
     einst->nsamples     = xmI1.nsamples;
 
     // Initialize the SNumForNotes conversion table
+    if ((einst->instConvTable = dmCalloc(XM_MaxInstruments + 1, sizeof(int))) == NULL)
+    {
+        JSSERROR(DMERR_MALLOC, DMERR_MALLOC,
+            "Could not allocate memory for instConvTable of instrument #%d.\n",
+            neinst);
+    }
+
     for (i = 0; i < XM_MaxInstruments; i++)
         einst->instConvTable[i] = jsetNotSet;
 
     // Read sample headers
-    for (nsample = 0; nsample < xmI1.nsamples; nsample++)
+    for (nsample = 0; nsample < einst->nsamples; nsample++)
     {
         if ((ret = jssXMLoadSampleInstrument(inFile, module, einst, neinst, nsample)) != DMERR_OK)
             return ret;