Author: sagamusix
Date: Sun Apr 14 16:15:51 2024
New Revision: 20597
URL: https://source.openmpt.org/browse/openmpt/?op=revision&rev=20597
Log:
[Fix] MED: Possible crash when trying to access non-existing pattern.
Modified:
trunk/OpenMPT/soundlib/Load_med.cpp
trunk/OpenMPT/soundlib/ModSequence.cpp
trunk/OpenMPT/soundlib/ModSequence.h
Modified: trunk/OpenMPT/soundlib/Load_med.cpp
==============================================================================
--- trunk/OpenMPT/soundlib/Load_med.cpp Sun Apr 14 14:58:52 2024 (r20596)
+++ trunk/OpenMPT/soundlib/Load_med.cpp Sun Apr 14 16:15:51 2024 (r20597)
@@ -1627,7 +1627,7 @@
if(numSongs > 1)
{
- PATTERNINDEX firstPat = order.EnsureUnique(0);
+ PATTERNINDEX firstPat = order.EnsureUnique(order.GetFirstValidIndex());
if(firstPat != PATTERNINDEX_INVALID)
{
for(CHANNELINDEX chn = 0; chn < m_nChannels; chn++)
Modified: trunk/OpenMPT/soundlib/ModSequence.cpp
==============================================================================
--- trunk/OpenMPT/soundlib/ModSequence.cpp Sun Apr 14 14:58:52 2024 (r20596)
+++ trunk/OpenMPT/soundlib/ModSequence.cpp Sun Apr 14 16:15:51 2024 (r20597)
@@ -149,6 +149,18 @@
}
+ORDERINDEX ModSequence::GetFirstValidIndex() const noexcept
+{
+ const ORDERINDEX length = GetLength();
+ for(ORDERINDEX ord = 0; ord < length; ord++)
+ {
+ if(IsValidPat(ord))
+ return ord;
+ }
+ return ORDERINDEX_INVALID;
+}
+
+
void ModSequence::Remove(ORDERINDEX posBegin, ORDERINDEX posEnd) noexcept
{
if(posEnd < posBegin || posEnd >= size())
Modified: trunk/OpenMPT/soundlib/ModSequence.h
==============================================================================
--- trunk/OpenMPT/soundlib/ModSequence.h Sun Apr 14 14:58:52 2024 (r20596)
+++ trunk/OpenMPT/soundlib/ModSequence.h Sun Apr 14 16:15:51 2024 (r20597)
@@ -98,6 +98,9 @@
ORDERINDEX GetPreviousOrderIgnoringSkips(const ORDERINDEX start) const noexcept;
ORDERINDEX GetNextOrderIgnoringSkips(const ORDERINDEX start) const noexcept;
+ // Returns the first item that contains a pattern that actually exists, ORDERINDEX_INVALID if no such item exists.
+ ORDERINDEX GetFirstValidIndex() const noexcept;
+
// Find an order item that contains a given pattern number.
ORDERINDEX FindOrder(PATTERNINDEX pat, ORDERINDEX startSearchAt = 0, bool searchForward = true) const noexcept;
|