From dd609a8a620d6a3217295027bdcee996716559e4 Mon Sep 17 00:00:00 2001
From: Jason Flatt <jflatt@cox.net>
Date: Tue, 18 Oct 2022 13:04:36 -0700
Subject: [PATCH] Avoid a null deref in sound output

---
 org.mamedev.MAME.yaml |  2 ++
 sound.patch           | 28 ++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 sound.patch

diff --git a/org.mamedev.MAME.yaml b/org.mamedev.MAME.yaml
index 6f85ed5..2e81606 100644
--- a/org.mamedev.MAME.yaml
+++ b/org.mamedev.MAME.yaml
@@ -81,3 +81,5 @@ modules:
         path: mame.ini
       - type: patch
         path: floppy.patch
+      - type: patch
+        path: sound.patch
diff --git a/sound.patch b/sound.patch
new file mode 100644
index 0000000..066c35c
--- /dev/null
+++ b/sound.patch
@@ -0,0 +1,28 @@
+diff --git a/src/emu/sound.cpp b/src/emu/sound.cpp
+index a0293063..c4989abb 100644
+--- a/src/emu/sound.cpp
++++ b/src/emu/sound.cpp
+@@ -761,6 +761,9 @@ read_stream_view sound_stream::update_view(attotime start, attotime end, u32 out
+ 	}
+ 	g_profiler.stop();
+ 
++	if (!m_output_view[outputnum].valid())
++		m_output_view[outputnum] = empty_view(start, end);
++
+ 	// return the requested view
+ 	return read_stream_view(m_output_view[outputnum], start);
+ }
+diff --git a/src/emu/sound.h b/src/emu/sound.h
+index 15f6a574..bfa45c45 100644
+--- a/src/emu/sound.h
++++ b/src/emu/sound.h
+@@ -284,6 +284,9 @@ public:
+ 		return *this;
+ 	}
+ 
++	// check basic constraints
++	bool valid() const { return m_buffer != nullptr; }
++
+ 	// return the local gain
+ 	sample_t gain() const { return m_gain; }
+