From bb0759f57569cf447b1f1d51fc6c20aa517806e1 Mon Sep 17 00:00:00 2001
From: Randy Heit <rheit@zdoom.fake>
Date: Thu, 31 Jul 2008 23:09:58 +0000
Subject: [PATCH] - Fixed: Attempting to load 0-length sounds caused a crash.

SVN r1100 (trunk)
---
 docs/rh-log.txt         |  1 +
 src/sound/fmodsound.cpp | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/docs/rh-log.txt b/docs/rh-log.txt
index 25698b4d3..751e8dde4 100644
--- a/docs/rh-log.txt
+++ b/docs/rh-log.txt
@@ -1,4 +1,5 @@
 July 31, 2008
+- Fixed: Attempting to load 0-length sounds caused a crash.
 - Removed filename-lowercasing from zipdir.
 - Fixed: The ouch face fix was lost when SBARINFO mugshot became the only
   mugshot present.
diff --git a/src/sound/fmodsound.cpp b/src/sound/fmodsound.cpp
index 5580f3178..e886ecabb 100644
--- a/src/sound/fmodsound.cpp
+++ b/src/sound/fmodsound.cpp
@@ -99,6 +99,8 @@ EXTERN_CVAR (Int, snd_samplerate)
 EXTERN_CVAR (Bool, snd_pitched)
 EXTERN_CVAR (Int, snd_channels)
 
+extern int sfx_empty;
+
 // PUBLIC DATA DEFINITIONS -------------------------------------------------
 
 ReverbContainer *ForcedEnvironment;
@@ -1991,6 +1993,7 @@ void FMODSoundRenderer::DoLoad(void **slot, sfxinfo_t *sfx)
 	errcount = 0;
 	while (errcount < 2)
 	{
+		sample = NULL;
 		if (sfxdata != NULL)
 		{
 			delete[] sfxdata;
@@ -2052,6 +2055,11 @@ void FMODSoundRenderer::DoLoad(void **slot, sfxinfo_t *sfx)
 		{
 			exinfo.length = size;
 		}
+		if (exinfo.length == 0)
+		{
+			DPrintf("Sample has a length of 0\n");
+			break;
+		}
 		result = Sys->createSound((char *)sfxstart, samplemode, &exinfo, &sample);
 		if (result != FMOD_OK)
 		{
@@ -2094,7 +2102,7 @@ void FMODSoundRenderer::getsfx(sfxinfo_t *sfx)
 	// If the sound doesn't exist, replace it with the empty sound.
 	if (sfx->lumpnum == -1)
 	{
-		sfx->lumpnum = Wads.GetNumForName("dsempty", ns_sounds);
+		sfx->lumpnum = sfx_empty;
 	}
 	
 	// See if there is another sound already initialized with this lump. If so,
@@ -2109,6 +2117,11 @@ void FMODSoundRenderer::getsfx(sfxinfo_t *sfx)
 		}
 	}
 	DoLoad(&sfx->data, sfx);
+	// If the sound failed to load, make it the empty sound.
+	if (sfx->data == NULL)
+	{
+		sfx->lumpnum = sfx_empty;
+	}
 }
 
 //==========================================================================