From: <sv...@op...> - 2024-04-14 14:16:38
|
Author: sagamusix Date: Sun Apr 14 16:16:32 2024 New Revision: 20598 URL: https://source.openmpt.org/browse/openmpt/?op=revision&rev=20598 Log: Merged revision(s) 20597 from trunk/OpenMPT: [Fix] MED: Possible crash when trying to access non-existing pattern (caught with afl++). ........ Modified: branches/OpenMPT-1.31/ (props changed) branches/OpenMPT-1.31/soundlib/Load_med.cpp branches/OpenMPT-1.31/soundlib/ModSequence.cpp branches/OpenMPT-1.31/soundlib/ModSequence.h Modified: branches/OpenMPT-1.31/soundlib/Load_med.cpp ============================================================================== --- branches/OpenMPT-1.31/soundlib/Load_med.cpp Sun Apr 14 16:15:51 2024 (r20597) +++ branches/OpenMPT-1.31/soundlib/Load_med.cpp Sun Apr 14 16:16:32 2024 (r20598) @@ -1447,7 +1447,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: branches/OpenMPT-1.31/soundlib/ModSequence.cpp ============================================================================== --- branches/OpenMPT-1.31/soundlib/ModSequence.cpp Sun Apr 14 16:15:51 2024 (r20597) +++ branches/OpenMPT-1.31/soundlib/ModSequence.cpp Sun Apr 14 16:16:32 2024 (r20598) @@ -125,6 +125,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: branches/OpenMPT-1.31/soundlib/ModSequence.h ============================================================================== --- branches/OpenMPT-1.31/soundlib/ModSequence.h Sun Apr 14 16:15:51 2024 (r20597) +++ branches/OpenMPT-1.31/soundlib/ModSequence.h Sun Apr 14 16:16:32 2024 (r20598) @@ -94,6 +94,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; |