Skip to content
Permalink
Browse files

Fixed a potential crash when reading MIDI files with incorrect track …

…length chunks
  • Loading branch information...
ed95 committed Jun 12, 2019
1 parent b6c1116 commit 0ed474e412c7fa3647756b7169a1428e2778c7a5
Showing with 5 additions and 4 deletions.
  1. +5 −4 modules/juce_audio_basics/midi/juce_MidiFile.cpp
@@ -262,25 +262,26 @@ bool MidiFile::readFrom (InputStream& sourceStream, bool createMatchingNoteOffs)
if (size > 16 && MidiFileHelpers::parseMidiHeader (d, timeFormat, fileType, expectedTracks))
{
size -= (size_t) (d - static_cast<const uint8*> (data.getData()));

int track = 0;

while (size > 0 && track < expectedTracks)
for (;;)
{
auto chunkType = (int) ByteOrder::bigEndianInt (d);
d += 4;
auto chunkSize = (int) ByteOrder::bigEndianInt (d);
d += 4;

if (chunkSize <= 0)
if (chunkSize <= 0 || (size_t) chunkSize > size)
break;

if (chunkType == (int) ByteOrder::bigEndianInt ("MTrk"))
readNextTrack (d, chunkSize, createMatchingNoteOffs);

if (++track >= expectedTracks)
break;

size -= (size_t) chunkSize + 8;
d += chunkSize;
++track;
}

return true;

0 comments on commit 0ed474e

Please sign in to comment.
You can’t perform that action at this time.