foxygit / doom Log in
commit 88b54de4d015d8f09af24aee7083303798a83af0
Author:     Roman Fomin <rfomin@gmail.com>
AuthorDate: Mon Jun 19 12:22:17 2023 +0700
Commit:     Roman Fomin <rfomin@gmail.com>
CommitDate: Mon Jun 19 12:22:17 2023 +0700

    Add initialization checks to i_winmusic and i_flmusic modules.
---
 src/i_flmusic.c  | 35 +++++++++++++++++++++++++++++++----
 src/i_winmusic.c | 26 ++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/src/i_flmusic.c b/src/i_flmusic.c
index 1fd7129c..dae71803 100644
--- a/src/i_flmusic.c
+++ b/src/i_flmusic.c
@@ -121,6 +121,13 @@ static boolean I_FL_InitMusic(void)

     synth = new_fluid_synth(settings);

+    if (synth == NULL)
+    {
+        fprintf(stderr,
+                "I_FL_InitMusic: FluidSynth failed to initialize synth.\n");
+        return false;
+    }
+
     sf_id = fluid_synth_sfload(synth, fsynth_sf_path, true);
     if (sf_id == FLUID_FAILED)
     {
@@ -141,6 +148,10 @@ static boolean I_FL_InitMusic(void)

 static void I_FL_SetMusicVolume(int volume)
 {
+    if (synth == NULL)
+    {
+        return;
+    }
     // FluidSynth's default is 0.2. Make 1.0 the maximum.
     // 0 -- 0.2 -- 10.0
     fluid_synth_set_gain(synth, (float) volume / 127);
@@ -148,18 +159,27 @@ static void I_FL_SetMusicVolume(int volume)

 static void I_FL_PauseSong(void)
 {
-    fluid_player_stop(player);
+    if (player)
+    {
+        fluid_player_stop(player);
+    }
 }

 static void I_FL_ResumeSong(void)
 {
-    fluid_player_play(player);
+    if (player)
+    {
+        fluid_player_play(player);
+    }
 }

 static void I_FL_PlaySong(void *handle, boolean looping)
 {
-    fluid_player_set_loop(player, looping ? -1 : 1);
-    fluid_player_play(player);
+    if (player)
+    {
+        fluid_player_set_loop(player, looping ? -1 : 1);
+        fluid_player_play(player);
+    }
 }

 static void I_FL_StopSong(void)
@@ -183,6 +203,13 @@ static void *I_FL_RegisterSong(void *data, int len)

     player = new_fluid_player(synth);

+    if (player == NULL)
+    {
+        fprintf(stderr,
+                "I_FL_RegisterSong: FluidSynth failed to initialize player.\n");
+        return NULL;
+    }
+
     if (IsMid(data, len))
     {
         result = fluid_player_add_mem(player, data, len);
diff --git a/src/i_winmusic.c b/src/i_winmusic.c
index 3f00bdf3..af2bde5c 100644
--- a/src/i_winmusic.c
+++ b/src/i_winmusic.c
@@ -1383,6 +1383,7 @@ static boolean I_WIN_InitMusic(void)
     if (mmr != MMSYSERR_NOERROR)
     {
         MidiError("midiStreamOpen", mmr);
+        hMidiStream = NULL;
         return false;
     }

@@ -1429,6 +1430,11 @@ static void I_WIN_StopSong(void)
     CloseHandle(hPlayerThread);
     hPlayerThread = NULL;

+    if (!hMidiStream)
+    {
+        return;
+    }
+
     mmr = midiStreamStop(hMidiStream);
     if (mmr != MMSYSERR_NOERROR)
     {
@@ -1440,6 +1446,11 @@ static void I_WIN_PlaySong(void *handle, boolean looping)
 {
     MMRESULT mmr;

+    if (!hMidiStream)
+    {
+        return;
+    }
+
     song.looping = looping;

     hPlayerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlayerProc,
@@ -1461,6 +1472,11 @@ static void I_WIN_PauseSong(void)
 {
     MMRESULT mmr;

+    if (!hMidiStream)
+    {
+        return;
+    }
+
     mmr = midiStreamPause(hMidiStream);
     if (mmr != MMSYSERR_NOERROR)
     {
@@ -1472,6 +1488,11 @@ static void I_WIN_ResumeSong(void)
 {
     MMRESULT mmr;

+    if (!hMidiStream)
+    {
+        return;
+    }
+
     mmr = midiStreamRestart(hMidiStream);
     if (mmr != MMSYSERR_NOERROR)
     {
@@ -1522,6 +1543,11 @@ static void *I_WIN_RegisterSong(void *data, int len)
     MIDIPROPTEMPO prop_tempo;
     MMRESULT mmr;

+    if (!hMidiStream)
+    {
+        return NULL;
+    }
+
     // MUS files begin with "MUS"
     // Reject anything which doesnt have this signature