From 26243ab9fe1171f70053e9aec4b20e9f7de9e4ef Mon Sep 17 00:00:00 2001
From: Konstanty Bialkowski <metaplasma@users.sourceforge.net>
Date: Tue, 19 Jul 2011 23:24:08 +1000
Subject: [PATCH] Fix AMS and DSM too large by one - SA45131/C

AMS and DSM use 1 based indexing for samples. Therefore the maximum
number of instruments is MAX_SAMPLES - 1.
---
 libmodplug/src/load_ams.cpp |    2 +-
 libmodplug/src/load_dsm.cpp |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libmodplug/src/load_ams.cpp b/libmodplug/src/load_ams.cpp
index 774b2be..4a29087 100644
--- a/libmodplug/src/load_ams.cpp
+++ b/libmodplug/src/load_ams.cpp
@@ -52,7 +52,7 @@ BOOL CSoundFile::ReadAMS(LPCBYTE lpStream, DWORD dwMemLength)
 	
 	if ((!lpStream) || (dwMemLength < 1024)) return FALSE;
 	if ((pfh->verhi != 0x01) || (strncmp(pfh->szHeader, "Extreme", 7))
-	 || (!pfh->patterns) || (!pfh->orders) || (!pfh->samples) || (pfh->samples > MAX_SAMPLES)
+	 || (!pfh->patterns) || (!pfh->orders) || (!pfh->samples) || (pfh->samples >= MAX_SAMPLES)
 	 || (pfh->patterns > MAX_PATTERNS) || (pfh->orders > MAX_ORDERS))
 	{
 		return ReadAMS2(lpStream, dwMemLength);
diff --git a/libmodplug/src/load_dsm.cpp b/libmodplug/src/load_dsm.cpp
index 4f51469..03819a5 100644
--- a/libmodplug/src/load_dsm.cpp
+++ b/libmodplug/src/load_dsm.cpp
@@ -101,7 +101,7 @@ BOOL CSoundFile::ReadDSM(LPCBYTE lpStream, DWORD dwMemLength)
 	if (m_nChannels < 4) m_nChannels = 4;
 	if (m_nChannels > 16) m_nChannels = 16;
 	m_nSamples = psong->numsmp;
-	if (m_nSamples > MAX_SAMPLES) m_nSamples = MAX_SAMPLES;
+	if (m_nSamples >= MAX_SAMPLES) m_nSamples = MAX_SAMPLES - 1;
 	m_nDefaultSpeed = psong->speed;
 	m_nDefaultTempo = psong->bpm;
 	m_nDefaultGlobalVolume = psong->globalvol << 2;
-- 
1.7.0.1

