OSDN Git Service

Fix sf3 load failure causing null pointer access
authorStarg <starg@users.osdn.me>
Sat, 2 Jun 2018 15:47:46 +0000 (00:47 +0900)
committerStarg <starg@users.osdn.me>
Sat, 2 Jun 2018 15:47:46 +0000 (00:47 +0900)
timidity/decode.c
timidity/decode.h
timidity/sndfont.c

index f78ab6a..2aff8cf 100644 (file)
@@ -21,6 +21,7 @@
 #include "timidity.h"
 #include "common.h"
 #include "controls.h"
+#include "instrum.h"
 #include "decode.h"
 
 #ifdef HAVE_LIBVORBIS
@@ -33,6 +34,8 @@ extern int load_vorbis_dll(void);     // w32g_vorbis_dll.c
 extern int load_vorbisfile_dll(void);  // w32g_vorbisfile_dll.c
 #endif
 
+static sample_t DummySampleData[128];
+
 static size_t oggvorbis_read_callback(void *ptr, size_t size, size_t nmemb, void *datasource)
 {
     struct timidity_file *tf = (struct timidity_file *)datasource;
@@ -53,7 +56,7 @@ static long oggvorbis_tell_callback(void *datasource)
 
 SampleDecodeResult decode_oggvorbis(struct timidity_file *tf)
 {
-    SampleDecodeResult sdr = {0};
+    SampleDecodeResult sdr = {.data = DummySampleData, .data_type = SAMPLE_TYPE_INT16};
     OggVorbis_File vf;
 
     if (load_vorbis_dll() != 0) {
@@ -96,6 +99,7 @@ SampleDecodeResult decode_oggvorbis(struct timidity_file *tf)
        data_length = (data_length > 0 ? data_length : 4096);
        ptr_size_t current_size = 0;
     sdr.data = (sample_t *)safe_large_malloc(data_length);
+       sdr.data_alloced = 1;
 
     while (1) {
         int bitstream = 0;
@@ -125,11 +129,13 @@ SampleDecodeResult decode_oggvorbis(struct timidity_file *tf)
 cleanup:
     ov_clear(&vf);
 
-    if (sdr.data) {
+    if (sdr.data_alloced) {
         safe_free(sdr.data);
-        sdr.data = NULL;
     }
 
+    sdr.data = DummySampleData;
+       sdr.data_alloced = 0;
+
     return sdr;
 }
 
@@ -138,7 +144,7 @@ cleanup:
 SampleDecodeResult decode_oggvorbis(struct timidity_file *tf)
 {
     ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "ogg vorbis decoder support is disabled");
-    return (SampleDecodeResult){.data = NULL, .data_type = 0};
+       return (SampleDecodeResult){.data = DummySampleData, .data_type = SAMPLE_TYPE_INT16};
 }
 
 #endif // HAVE_LIBVORBIS
index a1f926c..b0e370f 100644 (file)
@@ -3,6 +3,8 @@
 
 typedef struct SampleDecodeResult {
     sample_t *data;
+       uint8 data_alloced;
+       int data_type;
     splen_t data_length;
     int channels;
 } SampleDecodeResult;
index be0218f..8e01c6e 100644 (file)
@@ -841,8 +841,8 @@ static Instrument *load_from_file(SFInsts *rec, InstList *ip)
                 SampleDecodeResult sdr = decode_oggvorbis(ctf);
                 close_file(ctf);
                 sample->data = sdr.data;
-                sample->data_alloced = 1;
-                sample->data_type = SAMPLE_TYPE_INT16;
+                sample->data_alloced = sdr.data_alloced;
+                sample->data_type = sdr.data_type;
                                sample->data_length = sdr.data_length;
 
                                if (!(sample->modes & MODES_LOOPING)) {