Skip to content

Possible missing length check when reading a key signature meta message #32

@KNSN92

Description

@KNSN92

Hello! I'm using this crate in one of my projects, and it has been very useful. However, I think I may have found a bug.

I tried to read some MIDI files with this crate, but parsing stopped before the end of the track. After investigating event.rs, I noticed that there does not seem to be a length check when reading a key signature meta message.

Because of this, if a MIDI file contains an invalid key signature meta message with insufficient data length, parsing stops immediately when that meta event is read. When the strict feature is enabled, it returns an error like this:

malformed midi: malformed event
  caused by: invalid midi: failed to parse event
  caused by: invalid midi: failed to read meta event
  caused by: invalid midi: failed to read the expected integer

Is this intentional behavior, or could this be a missing check?

Source:

midly/src/event.rs

Lines 575 to 577 in 0f652b5

0x59 => {
MetaMessage::KeySignature(u8::read(&mut data)? as i8, u8::read(&mut data)? != 0)
}

I think it could be fixed with something like this:

0x59 if data.len() >= 2 => {
    MetaMessage::KeySignature(u8::read(&mut data)? as i8, u8::read(&mut data)? != 0)
}

After applying this change locally, I was able to process the MIDI files properly.

Sorry if my English is awkward.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions