Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MU3] Fix 310158: Don't null terminate MIDI sequence track name messages #7003

Closed
wants to merge 4 commits into from

Conversation

drebelsky
Copy link
Contributor

Resolves: https://musescore.org/en/node/310158

This PR makes it so that the null terminator from a part's name is not copied.

  • I signed CLA
  • I made sure the code in the PR follows the coding rules
  • I made sure the code compiles on my machine
  • I made sure there are no unnecessary changes in the code
  • I made sure the title of the PR reflects the core meaning of the issue you are solving
  • I made sure the commit message(s) contain a description and answer the question "Why do those changes fix that particular issue?" or "Why are those changes really necessary as improvements?"
  • I made sure the commit message title starts with "fix #424242:" if there is a related issue
  • I created the test (mtest, vtest, script test) to verify the changes I made

@Jojo-Schmitz
Copy link
Contributor

Jojo-Schmitz commented Dec 3, 2020

the mtests are not happy, which I guess is understandable, but needs fixing anyway?

I don't quite understand why the vtests failed, but can't imagine this to be your (PR's) fault.

@drebelsky
Copy link
Contributor Author

It looks like the layout code for 3.x has been changed since I opened the PR, but since my commits don't change these, I believe they could be merged without problems. I could also pull in the 3.x branch if that makes more sense.

@Jojo-Schmitz
Copy link
Contributor

As far as I can tell the build in CI is always done against the then latest commit.
But try a rebase to see if it helps here

@@ -90,7 +90,7 @@ void ExportMidi::writeHeader()
Staff* staff = cs->staff(staffIdx);

QByteArray partName = staff->partName().toUtf8();
int len = partName.length() + 1;
int len = partName.length();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line was correct. An increasing of the length must be here.

See Qt's documentation: https://doc.qt.io/qt-5/qbytearray.html#data

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're doing a memcpy instead of a strcpy, I don't think the + 1 is necessary.

@psmokotnin
Copy link
Contributor

I would fix it here.

putvl(event.len());
write(event.edata(), event.len());

@drebelsky
Copy link
Contributor Author

I feel like it makes sense to store the non-null-terminated string as the data for the event since it's the actual data. Fixing it in

putvl(event.len());
write(event.edata(), event.len());

probably works, but then we have to do a special check for a track name message. We could also change
ev.setLen(len);
, but then the actual data length and nominal data lengths would be different.

@psmokotnin
Copy link
Contributor

But creation of char string without a null termination is very crashable way.

uchar* _edata { nullptr }; // always zero terminated (_data[_len] == 0; )

It's good to fix it where file are saved instead of creation of broken MIDI events. For an example, midi import adds null termination while reading a file.

@Jojo-Schmitz
Copy link
Contributor

Please squash the commits git rebase -i HEAD~3.

@@ -135,8 +135,14 @@ void MidiFile::writeEvent(const MidiEvent& event)
case ME_META:
put(ME_META);
put(event.metaType());
putvl(event.len());
write(event.edata(), event.len());
if (event.metaType() == META_TRACK_NAME) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why track name type only? What about the other text types (0x01 - 0x07)?

Copy link
Contributor

@Jojo-Schmitz Jojo-Schmitz Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the issue is about track name only I guess, "Sequence Track Name MIDI messages wrongly null-terminated"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously, we have to check out what the MIDI standard says here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In https://www.midi.org/component/edocman/rp-001-v1-0-standard-midi-files-specification-96-1-4-pdf/fdocument?Itemid=9999, it looks like types 0x01–0x07 are text types, and https://www.midi.org/component/edocman/smf-device-name-and-program-name-meta-events/fdocument?Itemid=9999 adds 0x08 and 0x09. In

META_TITLE = 0x10, // mscore extension
META_SUBTITLE = 0x11, // mscore extension
META_COMPOSER = 0x12, // mscore extension
META_TRANSLATOR = 0x13, // mscore extension
META_POET = 0x14, // mscore extension
, we define some "mscore extensions" which are also (I believe) text types. Should we null terminate these extensions? Also, tangentially related, would it make sense if we changed the (nonstandard) extensions to be sent as System Exclusive messages or sequencer specific meta messages, instead?

@igorkorsukov igorkorsukov changed the title Fix 310158: Don't null terminate MIDI sequence track name messages [MU3] Fix 310158: Don't null terminate MIDI sequence track name messages Feb 5, 2021
@AndroYD84
Copy link

This problem resurfaced in the current stable and nightly build.
If I export a midi score from MuseScore and run this simple Python script:

import os,sys
import muspy

midi = muspy.read_midi('midi_file.mid')
print([track.name for track in midi.tracks])

It outputs something like ['Piano\x00', 'Strings\x00'] instead of ['Piano', 'Strings'], happens only if the MIDI file was exported from MuseScore.

@Jojo-Schmitz
Copy link
Contributor

Jojo-Schmitz commented Jan 25, 2022

Of course it does, the changes from this PR haven't been merged into 3.x nor been ported/merged to master.

The logs for the failing tests (vtests and mtest) have expired, are no longer available (as have the artifacts, so no way to test using those), so impossible to check what went wrong there, and so equally impossible to tell why they went wrong.

Please squash the commits, git rebase -i HEAD~4 and then git push -f the result.

Also better rebase the entire thing on top of the master branch, as there won't be any further 3.x release.

The mtest failures are most probably due to the *-ref.mid files having been changed only after the 1st commit, which then later got reverted, and that after the 4th commit all text meta events are not null-terminated anymore.

Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Jan 25, 2022
@Jojo-Schmitz
Copy link
Contributor

Jojo-Schmitz commented Jan 25, 2022

OK, I bit the bullet and ported it over to master (see PR #10374), was pretty easy and straight forward, esp. as none of the tests needed to get ported ;-)

vpereverzev pushed a commit that referenced this pull request Jan 25, 2022
@Jojo-Schmitz
Copy link
Contributor

I'd like to include this PR in my PR #9000, but can't seem to get the mtests to work, how would these *-ref.mid files need to get generated?

Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Jan 25, 2022
@Jojo-Schmitz
Copy link
Contributor

Jojo-Schmitz commented Jan 25, 2022

OK, I managed it, it is now part of PR #9000, As no further 3.x is planned and the changes are in master already too, I believe this PR here could/should get closed.

Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Feb 4, 2022
Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Feb 5, 2022
Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Feb 7, 2022
Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Feb 24, 2022
Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request May 12, 2022
@RomanPudashkin
Copy link
Contributor

3.x is closed for any changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants