changeset 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 aaed8fa9a11f
children 6aa0897265e8
files minijss/jloadxm.c minijss/jssmod.c minijss/jssmod.h
diffstat 3 files changed, 18 insertions(+), 5 deletions(-) [+]
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;
--- a/minijss/jssmod.c	Mon Oct 30 18:01:25 2017 +0200
+++ b/minijss/jssmod.c	Mon Oct 30 18:12:38 2017 +0200
@@ -430,10 +430,7 @@
 
     // Initialize the requisite fields
     for (i = 0; i < jsetNNotes; i++)
-    {
         inst->sNumForNotes[i] = jsetNotSet;
-        inst->instConvTable[i] = jsetNotSet;
-    }
 
     return inst;
 }
@@ -446,6 +443,7 @@
 #ifndef JSS_LIGHT
         dmFree(inst->desc);
 #endif
+        dmFree(inst->instConvTable);
         dmFree(inst);
     }
 }
--- a/minijss/jssmod.h	Mon Oct 30 18:01:25 2017 +0200
+++ b/minijss/jssmod.h	Mon Oct 30 18:12:38 2017 +0200
@@ -119,7 +119,8 @@
 #endif
     int nsamples,
         sNumForNotes[jsetNNotes],
-        instConvTable[jsetNNotes];
+        *instConvTable;
+
     JSSEnvelope volumeEnv, panningEnv;
     int vibratoType,
         vibratoSweep,