Skip to content

Commit

Permalink
Merge pull request #4128 from lvinken/277230-musicxml-corruption
Browse files Browse the repository at this point in the history
fix #277230 - MuseScore saves a corrupted file after importing a Musi…
  • Loading branch information
anatoly-os committed Nov 22, 2018
2 parents 100d297 + 9b10f30 commit a4e6ad8
Show file tree
Hide file tree
Showing 8 changed files with 1,174 additions and 2 deletions.
6 changes: 4 additions & 2 deletions libmscore/fraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ static int gcd(int a, int b)

static unsigned lcm(int a, int b)
{
return a * b / gcd(a, b);
const auto product = static_cast<int_least64_t>(a) * b;
return static_cast<int>(product / gcd(a, b));
}

//---------------------------------------------------------
Expand Down Expand Up @@ -182,7 +183,8 @@ int Fraction::ticks() const
// MScore::division - ticks per quarter note
// MScore::division * 4 - ticks per whole note
// result: rounded (MScore::division * 4 * _numerator * 1.0 / _denominator) value
return (_numerator * MScore::division * 4 + (_denominator/2)) / _denominator;
const auto result = (static_cast<int_least64_t>(_numerator) * MScore::division * 4 + (_denominator/2)) / _denominator;
return static_cast<int>(result);
}

} // namespace Ms
Expand Down
83 changes: 83 additions & 0 deletions mtest/musicxml/io/testFractionMinus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.1 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
<score-partwise version="3.1">
<work>
<work-number>MuseScore testfile</work-number>
<work-title>Fraction::operator-=()</work-title>
</work>
<identification>
<encoding>
<software>MuseScore 0.7.0</software>
<encoding-date>2007-09-10</encoding-date>
<supports element="accidental" type="yes"/>
<supports element="beam" type="yes"/>
<supports element="print" attribute="new-page" type="no"/>
<supports element="print" attribute="new-system" type="no"/>
<supports element="stem" type="yes"/>
</encoding>
<miscellaneous>
<miscellaneous-field name="description">
When compiled with 32-bit ints, importing this file produces an integer overflow in lcm()
when called by Fraction::operator-=(). Causes undefined behaviour, but apparently current
compilers and/or compiler settings result in ignoring the overflow. End effect is corruption
of the internal data structures and invalid files written.
The divisions value used is similar to the one used by Myriad Harmony Assistant v9.7.1c.
</miscellaneous-field>
</miscellaneous>
</identification>
<part-list>
<score-part id="P1">
<part-name>Music</part-name>
<score-instrument id="P1-I1">
<instrument-name>Music</instrument-name>
</score-instrument>
<midi-device id="P1-I1" port="1"></midi-device>
<midi-instrument id="P1-I1">
<midi-channel>1</midi-channel>
<midi-program>1</midi-program>
<volume>78.7402</volume>
<pan>0</pan>
</midi-instrument>
</score-part>
</part-list>
<part id="P1">
<measure number="1">
<attributes>
<divisions>11586</divisions>
<key>
<fifths>0</fifths>
</key>
<time>
<beats>2</beats>
<beat-type>4</beat-type>
</time>
<clef>
<sign>G</sign>
<line>2</line>
</clef>
</attributes>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>11586</duration>
<type>quarter</type>
</note>
<forward>
<duration>1</duration>
</forward>
<backup>
<duration>1</duration>
</backup>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>11586</duration>
<type>quarter</type>
</note>
</measure>
</part>
</score-partwise>
72 changes: 72 additions & 0 deletions mtest/musicxml/io/testFractionMinus_ref.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.1 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
<score-partwise version="3.1">
<work>
<work-number>MuseScore testfile</work-number>
<work-title>Fraction::operator-=()</work-title>
</work>
<identification>
<encoding>
<software>MuseScore 0.7.0</software>
<encoding-date>2007-09-10</encoding-date>
<supports element="accidental" type="yes"/>
<supports element="beam" type="yes"/>
<supports element="print" attribute="new-page" type="no"/>
<supports element="print" attribute="new-system" type="no"/>
<supports element="stem" type="yes"/>
</encoding>
</identification>
<part-list>
<score-part id="P1">
<part-name>Music</part-name>
<score-instrument id="P1-I1">
<instrument-name>Music</instrument-name>
</score-instrument>
<midi-device id="P1-I1" port="1"></midi-device>
<midi-instrument id="P1-I1">
<midi-channel>1</midi-channel>
<midi-program>1</midi-program>
<volume>78.7402</volume>
<pan>0</pan>
</midi-instrument>
</score-part>
</part-list>
<part id="P1">
<measure number="1">
<attributes>
<divisions>1</divisions>
<key>
<fifths>0</fifths>
</key>
<time>
<beats>2</beats>
<beat-type>4</beat-type>
</time>
<clef>
<sign>G</sign>
<line>2</line>
</clef>
</attributes>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>1</duration>
<voice>1</voice>
<type>quarter</type>
<stem>up</stem>
</note>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>1</duration>
<voice>1</voice>
<type>quarter</type>
<stem>up</stem>
</note>
</measure>
</part>
</score-partwise>
83 changes: 83 additions & 0 deletions mtest/musicxml/io/testFractionPlus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.1 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
<score-partwise version="3.1">
<work>
<work-number>MuseScore testfile</work-number>
<work-title>Fraction::operator+=()</work-title>
</work>
<identification>
<encoding>
<software>MuseScore 0.7.0</software>
<encoding-date>2007-09-10</encoding-date>
<supports element="accidental" type="yes"/>
<supports element="beam" type="yes"/>
<supports element="print" attribute="new-page" type="no"/>
<supports element="print" attribute="new-system" type="no"/>
<supports element="stem" type="yes"/>
</encoding>
<miscellaneous>
<miscellaneous-field name="description">
When compiled with 32-bit ints, importing this file produces an integer overflow in lcm()
when called by Fraction::operator+=(). Causes undefined behaviour, but apparently current
compilers and/or compiler settings result in ignoring the overflow. End effect is corruption
of the internal data structures and invalid files written.
The divisions value used is similar to the one used by Myriad Harmony Assistant v9.7.1c.
</miscellaneous-field>
</miscellaneous>
</identification>
<part-list>
<score-part id="P1">
<part-name>Music</part-name>
<score-instrument id="P1-I1">
<instrument-name>Music</instrument-name>
</score-instrument>
<midi-device id="P1-I1" port="1"></midi-device>
<midi-instrument id="P1-I1">
<midi-channel>1</midi-channel>
<midi-program>1</midi-program>
<volume>78.7402</volume>
<pan>0</pan>
</midi-instrument>
</score-part>
</part-list>
<part id="P1">
<measure number="1">
<attributes>
<divisions>11586</divisions>
<key>
<fifths>0</fifths>
</key>
<time>
<beats>2</beats>
<beat-type>4</beat-type>
</time>
<clef>
<sign>G</sign>
<line>2</line>
</clef>
</attributes>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>11586</duration>
<type>quarter</type>
</note>
<forward>
<duration>1</duration>
</forward>
<forward>
<duration>1</duration>
</forward>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>11586</duration>
<type>quarter</type>
</note>
</measure>
</part>
</score-partwise>
72 changes: 72 additions & 0 deletions mtest/musicxml/io/testFractionPlus_ref.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.1 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
<score-partwise version="3.1">
<work>
<work-number>MuseScore testfile</work-number>
<work-title>Fraction::operator+=()</work-title>
</work>
<identification>
<encoding>
<software>MuseScore 0.7.0</software>
<encoding-date>2007-09-10</encoding-date>
<supports element="accidental" type="yes"/>
<supports element="beam" type="yes"/>
<supports element="print" attribute="new-page" type="no"/>
<supports element="print" attribute="new-system" type="no"/>
<supports element="stem" type="yes"/>
</encoding>
</identification>
<part-list>
<score-part id="P1">
<part-name>Music</part-name>
<score-instrument id="P1-I1">
<instrument-name>Music</instrument-name>
</score-instrument>
<midi-device id="P1-I1" port="1"></midi-device>
<midi-instrument id="P1-I1">
<midi-channel>1</midi-channel>
<midi-program>1</midi-program>
<volume>78.7402</volume>
<pan>0</pan>
</midi-instrument>
</score-part>
</part-list>
<part id="P1">
<measure number="1">
<attributes>
<divisions>1</divisions>
<key>
<fifths>0</fifths>
</key>
<time>
<beats>2</beats>
<beat-type>4</beat-type>
</time>
<clef>
<sign>G</sign>
<line>2</line>
</clef>
</attributes>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>1</duration>
<voice>1</voice>
<type>quarter</type>
<stem>up</stem>
</note>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>1</duration>
<voice>1</voice>
<type>quarter</type>
<stem>up</stem>
</note>
</measure>
</part>
</score-partwise>
Loading

0 comments on commit a4e6ad8

Please sign in to comment.