From: <sv...@op...> - 2024-11-30 15:52:42
|
Author: sagamusix Date: Sat Nov 30 16:52:35 2024 New Revision: 22357 URL: https://source.openmpt.org/browse/openmpt/?op=revision&rev=22357 Log: [Fix] Avoid undefined behaviour when writing past end of span (it was guaranteed to be a valid write, but still UB according to span API contract). Modified: trunk/OpenMPT/soundlib/Snd_fx.cpp Modified: trunk/OpenMPT/soundlib/Snd_fx.cpp ============================================================================== --- trunk/OpenMPT/soundlib/Snd_fx.cpp Sat Nov 30 14:50:41 2024 (r22356) +++ trunk/OpenMPT/soundlib/Snd_fx.cpp Sat Nov 30 16:52:35 2024 (r22357) @@ -5436,6 +5436,7 @@ } else { // SysEx message, find end of message + sendLen = outSize - sendPos; for(uint32 i = sendPos + 1; i < outSize; i++) { if(out[i] == 0xF7) @@ -5445,12 +5446,6 @@ break; } } - if(sendLen == 0) - { - // Didn't find end, so "invent" end of SysEx message - out[outSize++] = 0xF7; - sendLen = outSize - sendPos; - } } } else if(!(out[sendPos] & 0x80)) { @@ -5658,14 +5653,32 @@ firstNibble = true; } } + // Finish current byte if(!firstNibble) - { - // Finish current byte outPos++; - } if(updateZxxParam < 0x80) chn.lastZxxParam = updateZxxParam; + // Add end of SysEx byte if necessary + for(size_t i = 0; i < outPos; i++) + { + if(out[i] != 0xF0) + continue; + if(outPos - i >= 4 && (out[i + 1] == 0xF0 || out[i + 1] == 0xF1)) + { + // Internal message + i += 3; + } else + { + // Real SysEx + while(i < outPos && out[i] != 0xF7) + i++; + if(i == outPos && outPos < out.size()) + out[outPos++] = 0xF7; + } + + } + out = out.first(outPos); } |