public inbox for git-commits@fedoraproject.org
help / color / mirror / Atom feed
* [rpms/wildmidi] f45: Backport some upstream patches
@ 2026-10-24 9:26 Artur Frenszek-Iwicki
0 siblings, 0 replies; only message in thread
From: Artur Frenszek-Iwicki @ 2026-10-24 9:26 UTC (permalink / raw)
To: git-commits
A new commit has been pushed.
Repo : rpms/wildmidi
Branch : f45
Commit : ae1509e2486e1f389debc2c4064c6e7c0eb6c569
Author : Artur Frenszek-Iwicki <fedora@svgames.pl>
Date : 2026-10-24T11:07:04+02:00
Stats : +607/-1 in 3 file(s)
URL : https://src.fedoraproject.org/rpms/wildmidi/c/ae1509e2486e1f389debc2c4064c6e7c0eb6c569?branch=f45
Log:
Backport some upstream patches
This includes two PRs accepted upstream:
- #293: Fix signed-integer-overflow UB in big-endian 32-bit reads
across format parsers
- $294: Bound variable-length shift and clamp float->uint casts
in parsers
---
diff --git a/293.patch b/293.patch
new file mode 100644
index 0000000..208f6c6
--- /dev/null
+++ b/293.patch
@@ -0,0 +1,288 @@
+From 9622015bd0c44b6e2a2c923bd70b711d3f54d6d1 Mon Sep 17 00:00:00 2001
+From: glitchfox <glitchfox@benjaminali.com>
+Date: Thu, 30 Jul 2026 06:49:53 +0000
+Subject: [PATCH 1/2] Fix signed-integer-overflow UB in big-endian 32-bit reads
+ (parsers)
+
+The format parsers assemble 32-bit big-endian values a byte at a time, e.g.
+`tmp_val = *midi_data++ << 24;`. The uint8_t operand promotes to int, so when
+the top byte's high bit is set, `value << 24` shifts into/past the int sign
+bit -- undefined behavior (UBSan: "left shift of 255 by 24 places cannot be
+represented in type 'int'"). This fires on the normal MIDI/XMI/MUS/HMP/HMI
+loading paths and when reading GUS patch headers; found with AFL++ + UBSan.
+
+Cast the shifted byte to uint32_t so the shift is well-defined unsigned
+arithmetic. The computed value is unchanged on two's-complement targets; only
+the `<< 24` term can overflow (`<< 16` / `<< 8` on a byte stay within int), so
+those are left as-is. 25 sites across f_midi, f_xmidi, f_hmi, f_hmp, gus_pat,
+and mus2mid.
+
+Signed-off-by: glitchfox <glitchfox@benjaminali.com>
+---
+ src/f_hmi.c | 4 ++--
+ src/f_hmp.c | 16 ++++++++--------
+ src/f_midi.c | 4 ++--
+ src/f_xmidi.c | 12 ++++++------
+ src/gus_pat.c | 12 ++++++------
+ src/mus2mid.c | 2 +-
+ 6 files changed, 25 insertions(+), 25 deletions(-)
+
+diff --git a/src/f_hmi.c b/src/f_hmi.c
+index 14ca8c51..85833a88 100644
+--- a/src/f_hmi.c
++++ b/src/f_hmi.c
+@@ -143,7 +143,7 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
+ hmi_track_offset[i] = *hmi_data++;
+ hmi_track_offset[i] += (*hmi_data++ << 8);
+ hmi_track_offset[i] += (*hmi_data++ << 16);
+- hmi_track_offset[i] += (*hmi_data++ << 24);
++ hmi_track_offset[i] += ((uint32_t)*hmi_data++ << 24);
+
+ if (hmi_size < (hmi_track_offset[i] + 0x5a + 4)) {
+ _WM_GLOBAL_ERROR(WM_ERR_NOT_HMI, "file too short", 0);
+@@ -160,7 +160,7 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
+ hmi_track_header_length[i] = hmi_addr[0x57];
+ hmi_track_header_length[i] += (hmi_addr[0x58] << 8);
+ hmi_track_header_length[i] += (hmi_addr[0x59] << 16);
+- hmi_track_header_length[i] += (hmi_addr[0x5a] << 24);
++ hmi_track_header_length[i] += ((uint32_t)hmi_addr[0x5a] << 24);
+
+ /* GHSA-f2xg-2rq9-xvhm: header length is file-controlled; bound it
+ * against the remaining buffer so the pointer advance below cannot
+diff --git a/src/f_hmp.c b/src/f_hmp.c
+index 93ae8247..dd020ac0 100644
+--- a/src/f_hmp.c
++++ b/src/f_hmp.c
+@@ -111,7 +111,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ hmp_file_length = *hmp_data++;
+ hmp_file_length += (*hmp_data++ << 8);
+ hmp_file_length += (*hmp_data++ << 16);
+- hmp_file_length += (*hmp_data++ << 24);
++ hmp_file_length += ((uint32_t)*hmp_data++ << 24);
+ hmp_size -= 4;
+
+ WMIDI_UNUSED(hmp_file_length);
+@@ -123,7 +123,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ hmp_chunks = *hmp_data++;
+ hmp_chunks += (*hmp_data++ << 8);
+ hmp_chunks += (*hmp_data++ << 16);
+- hmp_chunks += (*hmp_data++ << 24);
++ hmp_chunks += ((uint32_t)*hmp_data++ << 24);
+ hmp_size -= 4;
+
+ if (!hmp_chunks) {
+@@ -135,7 +135,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ hmp_unknown = *hmp_data++;
+ hmp_unknown += (*hmp_data++ << 8);
+ hmp_unknown += (*hmp_data++ << 16);
+- hmp_unknown += (*hmp_data++ << 24);
++ hmp_unknown += ((uint32_t)*hmp_data++ << 24);
+ hmp_size -= 4;
+
+ WMIDI_UNUSED(hmp_unknown);
+@@ -147,7 +147,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ hmp_bpm = *hmp_data++;
+ hmp_bpm += (*hmp_data++ << 8);
+ hmp_bpm += (*hmp_data++ << 16);
+- hmp_bpm += (*hmp_data++ << 24);
++ hmp_bpm += ((uint32_t)*hmp_data++ << 24);
+ hmp_size -= 4;
+
+ if (!hmp_bpm) {
+@@ -171,7 +171,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ hmp_song_time = *hmp_data++;
+ hmp_song_time += (*hmp_data++ << 8);
+ hmp_song_time += (*hmp_data++ << 16);
+- hmp_song_time += (*hmp_data++ << 24);
++ hmp_song_time += ((uint32_t)*hmp_data++ << 24);
+ hmp_size -= 4;
+
+ /* DEBUG */
+@@ -211,7 +211,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ chunk_num = *hmp_data++;
+ chunk_num += (*hmp_data++ << 8);
+ chunk_num += (*hmp_data++ << 16);
+- chunk_num += (*hmp_data++ << 24);
++ chunk_num += ((uint32_t)*hmp_data++ << 24);
+ chunk_ofs[i] += 4;
+
+ WMIDI_UNUSED(chunk_num);
+@@ -219,7 +219,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ chunk_length[i] = *hmp_data++;
+ chunk_length[i] += (*hmp_data++ << 8);
+ chunk_length[i] += (*hmp_data++ << 16);
+- chunk_length[i] += (*hmp_data++ << 24);
++ chunk_length[i] += ((uint32_t)*hmp_data++ << 24);
+ chunk_ofs[i] += 4;
+
+ if (chunk_length[i] > hmp_size) {
+@@ -232,7 +232,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ hmp_track = *hmp_data++;
+ hmp_track += (*hmp_data++ << 8);
+ hmp_track += (*hmp_data++ << 16);
+- hmp_track += (*hmp_data++ << 24);
++ hmp_track += ((uint32_t)*hmp_data++ << 24);
+ chunk_ofs[i] += 4;
+
+ WMIDI_UNUSED(hmp_track);
+diff --git a/src/f_midi.c b/src/f_midi.c
+index 4963d4f4..f302606c 100644
+--- a/src/f_midi.c
++++ b/src/f_midi.c
+@@ -88,7 +88,7 @@ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+ /*
+ * Get Midi Header Size - must always be 6
+ */
+- tmp_val = *midi_data++ << 24;
++ tmp_val = (uint32_t)*midi_data++ << 24;
+ tmp_val |= *midi_data++ << 16;
+ tmp_val |= *midi_data++ << 8;
+ tmp_val |= *midi_data++;
+@@ -166,7 +166,7 @@ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+ midi_size -= 4;
+
+ /* track size */
+- tmp_val = *midi_data++ << 24;
++ tmp_val = (uint32_t)*midi_data++ << 24;
+ tmp_val |= *midi_data++ << 16;
+ tmp_val |= *midi_data++ << 8;
+ tmp_val |= *midi_data++;
+diff --git a/src/f_xmidi.c b/src/f_xmidi.c
+index 6e4af3a3..73657d0d 100644
+--- a/src/f_xmidi.c
++++ b/src/f_xmidi.c
+@@ -73,7 +73,7 @@ struct _mdi *_WM_ParseNewXmi(const uint8_t *xmi_data, uint32_t xmi_size) {
+ xmi_size -= 4;
+
+ /* bytes until next entry */
+- xmi_tmpdata = *xmi_data++ << 24;
++ xmi_tmpdata = (uint32_t)*xmi_data++ << 24;
+ xmi_tmpdata |= *xmi_data++ << 16;
+ xmi_tmpdata |= *xmi_data++ << 8;
+ xmi_tmpdata |= *xmi_data++;
+@@ -125,7 +125,7 @@ struct _mdi *_WM_ParseNewXmi(const uint8_t *xmi_data, uint32_t xmi_size) {
+ xmi_data += 4;
+ xmi_size -= 4;
+
+- xmi_catlen = *xmi_data++ << 24;
++ xmi_catlen = (uint32_t)*xmi_data++ << 24;
+ xmi_catlen |= *xmi_data++ << 16;
+ xmi_catlen |= *xmi_data++ << 8;
+ xmi_catlen |= *xmi_data++;
+@@ -164,7 +164,7 @@ struct _mdi *_WM_ParseNewXmi(const uint8_t *xmi_data, uint32_t xmi_size) {
+ xmi_data += 4;
+ xmi_size -= 4;
+
+- xmi_subformlen = *xmi_data++ << 24;
++ xmi_subformlen = (uint32_t)*xmi_data++ << 24;
+ xmi_subformlen |= *xmi_data++ << 16;
+ xmi_subformlen |= *xmi_data++ << 8;
+ xmi_subformlen |= *xmi_data++;
+@@ -196,7 +196,7 @@ struct _mdi *_WM_ParseNewXmi(const uint8_t *xmi_data, uint32_t xmi_size) {
+ /* hold patch events */
+ xmi_data += 4;
+
+- xmi_tmpdata = *xmi_data++ << 24;
++ xmi_tmpdata = (uint32_t)*xmi_data++ << 24;
+ xmi_tmpdata |= *xmi_data++ << 16;
+ xmi_tmpdata |= *xmi_data++ << 8;
+ xmi_tmpdata |= *xmi_data++;
+@@ -216,7 +216,7 @@ struct _mdi *_WM_ParseNewXmi(const uint8_t *xmi_data, uint32_t xmi_size) {
+ /* FIXME: May not be needed for playback */
+ xmi_data += 4;
+
+- xmi_tmpdata = *xmi_data++ << 24;
++ xmi_tmpdata = (uint32_t)*xmi_data++ << 24;
+ xmi_tmpdata |= *xmi_data++ << 16;
+ xmi_tmpdata |= *xmi_data++ << 8;
+ xmi_tmpdata |= *xmi_data++;
+@@ -234,7 +234,7 @@ struct _mdi *_WM_ParseNewXmi(const uint8_t *xmi_data, uint32_t xmi_size) {
+
+ xmi_evnt_cnt++;
+
+- xmi_evntlen = *xmi_data++ << 24;
++ xmi_evntlen = (uint32_t)*xmi_data++ << 24;
+ xmi_evntlen |= *xmi_data++ << 16;
+ xmi_evntlen |= *xmi_data++ << 8;
+ xmi_evntlen |= *xmi_data++;
+diff --git a/src/gus_pat.c b/src/gus_pat.c
+index 697c31f2..a34a9c8c 100644
+--- a/src/gus_pat.c
++++ b/src/gus_pat.c
+@@ -784,29 +784,29 @@ struct _sample * _WM_load_gus_pat(const char *filename, int fix_release) {
+ }
+
+ gus_sample->loop_fraction = gus_patch[gus_ptr + 7];
+- gus_sample->data_length = (gus_patch[gus_ptr + 11] << 24)
++ gus_sample->data_length = ((uint32_t)gus_patch[gus_ptr + 11] << 24)
+ | (gus_patch[gus_ptr + 10] << 16)
+ | (gus_patch[gus_ptr + 9] << 8)
+ | gus_patch[gus_ptr + 8];
+- gus_sample->loop_start = (gus_patch[gus_ptr + 15] << 24)
++ gus_sample->loop_start = ((uint32_t)gus_patch[gus_ptr + 15] << 24)
+ | (gus_patch[gus_ptr + 14] << 16)
+ | (gus_patch[gus_ptr + 13] << 8)
+ | gus_patch[gus_ptr + 12];
+- gus_sample->loop_end = (gus_patch[gus_ptr + 19] << 24)
++ gus_sample->loop_end = ((uint32_t)gus_patch[gus_ptr + 19] << 24)
+ | (gus_patch[gus_ptr + 18] << 16)
+ | (gus_patch[gus_ptr + 17] << 8)
+ | gus_patch[gus_ptr + 16];
+ gus_sample->rate = (gus_patch[gus_ptr + 21] << 8)
+ | gus_patch[gus_ptr + 20];
+- gus_sample->freq_low = (gus_patch[gus_ptr + 25] << 24)
++ gus_sample->freq_low = ((uint32_t)gus_patch[gus_ptr + 25] << 24)
+ | (gus_patch[gus_ptr + 24] << 16)
+ | (gus_patch[gus_ptr + 23] << 8)
+ | gus_patch[gus_ptr + 22];
+- gus_sample->freq_high = (gus_patch[gus_ptr + 29] << 24)
++ gus_sample->freq_high = ((uint32_t)gus_patch[gus_ptr + 29] << 24)
+ | (gus_patch[gus_ptr + 28] << 16)
+ | (gus_patch[gus_ptr + 27] << 8)
+ | gus_patch[gus_ptr + 26];
+- gus_sample->freq_root = (gus_patch[gus_ptr + 33] << 24)
++ gus_sample->freq_root = ((uint32_t)gus_patch[gus_ptr + 33] << 24)
+ | (gus_patch[gus_ptr + 32] << 16)
+ | (gus_patch[gus_ptr + 31] << 8)
+ | gus_patch[gus_ptr + 30];
+diff --git a/src/mus2mid.c b/src/mus2mid.c
+index 86cfffd8..d7ab212b 100644
+--- a/src/mus2mid.c
++++ b/src/mus2mid.c
+@@ -211,7 +211,7 @@ static int32_t writevarlen(int32_t value, uint8_t *out)
+ }
+
+ #define READ_INT16(b) ((b)[0] | ((b)[1] << 8))
+-#define READ_INT32(b) ((b)[0] | ((b)[1] << 8) | ((b)[2] << 16) | ((b)[3] << 24))
++#define READ_INT32(b) ((b)[0] | ((b)[1] << 8) | ((b)[2] << 16) | ((uint32_t)(b)[3] << 24))
+
+ int _WM_mus2midi(const uint8_t *in, uint32_t insize,
+ uint8_t **out, uint32_t *outsize,
+
+From ea903841963bafbcc064657c6c5ac958514de0d2 Mon Sep 17 00:00:00 2001
+From: glitchfox <glitchfox@benjaminali.com>
+Date: Thu, 30 Jul 2026 10:46:36 +0000
+Subject: [PATCH 2/2] mus2mid: cast all READ_INT32 operands to uint32_t
+ (consistency)
+
+Per review (@sezero): match hmi2mid.c's macro style by casting every byte
+rather than only the <<24 term. No behavioural change.
+
+Signed-off-by: glitchfox <glitchfox@benjaminali.com>
+---
+ src/mus2mid.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/src/mus2mid.c b/src/mus2mid.c
+index d7ab212b..fcce478e 100644
+--- a/src/mus2mid.c
++++ b/src/mus2mid.c
+@@ -211,7 +211,7 @@ static int32_t writevarlen(int32_t value, uint8_t *out)
+ }
+
+ #define READ_INT16(b) ((b)[0] | ((b)[1] << 8))
+-#define READ_INT32(b) ((b)[0] | ((b)[1] << 8) | ((b)[2] << 16) | ((uint32_t)(b)[3] << 24))
++#define READ_INT32(b) ((uint32_t)(b)[0] | ((uint32_t)(b)[1] << 8) | ((uint32_t)(b)[2] << 16) | ((uint32_t)(b)[3] << 24))
+
+ int _WM_mus2midi(const uint8_t *in, uint32_t insize,
+ uint8_t **out, uint32_t *outsize,
diff --git a/294.patch b/294.patch
new file mode 100644
index 0000000..91c78fd
--- /dev/null
+++ b/294.patch
@@ -0,0 +1,305 @@
+From bbff48133b0f0a17ec1297c7fe142ec15a239c4c Mon Sep 17 00:00:00 2001
+From: glitchfox <glitchfox@benjaminali.com>
+Date: Thu, 30 Jul 2026 07:35:28 +0000
+Subject: [PATCH 1/4] Bound variable-length shift and clamp float->uint casts
+ in the parsers
+
+Two more UndefinedBehaviorSanitizer findings from AFL++ fuzzing, both on the
+untrusted-input parse paths:
+
+f_hmp.c -- the HMP variable-length delta decoders accumulate 7 bits per byte via
+`(*p & 0x7F) << var_len_shift`, bumping var_len_shift by 7 with no upper bound.
+A long run of continuation bytes drives the shift past 31, which is undefined
+(UBSan: "shift exponent is too large" and, at shift 28, "left shift of N cannot
+be represented in type 'int'"). Accumulate only while var_len_shift < 32 and
+shift as unsigned; bytes are still consumed so the parse stays aligned. Applies
+to both var-len loops.
+
+f_midi.c -- `sample_count = (uint32_t) sample_count_f;` casts a float that a
+crafted tempo/delta can push above UINT32_MAX, an undefined float-to-integer
+conversion. Clamp to [0, UINT32_MAX] before the cast. Three sites.
+
+Value-preserving for well-formed input; only malformed input (which was already
+UB) is affected. Found with AFL++ + UBSan. Independent of #293 (different lines).
+
+Signed-off-by: glitchfox <glitchfox@benjaminali.com>
+---
+ src/f_hmp.c | 21 +++++++++++++++++----
+ src/f_midi.c | 6 +++---
+ 2 files changed, 20 insertions(+), 7 deletions(-)
+
+diff --git a/src/f_hmp.c b/src/f_hmp.c
+index 93ae8247..2b3fbf46 100644
+--- a/src/f_hmp.c
++++ b/src/f_hmp.c
+@@ -242,13 +242,21 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ var_len_shift = 0;
+ if (*hmp_data < 0x80) {
+ do {
+- chunk_delta[i] = chunk_delta[i] | ((*hmp_data++ & 0x7F) << var_len_shift);
++ /* var_len_shift is unbounded on malformed input; only accumulate
++ * while the shift is in range for the 32-bit chunk_delta, and
++ * shift as unsigned. Bytes are still consumed so parsing stays
++ * aligned. */
++ if (var_len_shift < 32)
++ chunk_delta[i] |= ((uint32_t)(*hmp_data & 0x7F) << var_len_shift);
++ hmp_data++;
+ var_len_shift += 7;
+ chunk_ofs[i]++;
+ } while (hmp_data < data_end && *hmp_data < 0x80);
+ }
+ if (hmp_data == data_end) goto tooshort;
+- chunk_delta[i] = chunk_delta[i] | ((*hmp_data++ & 0x7F) << var_len_shift);
++ if (var_len_shift < 32)
++ chunk_delta[i] |= ((uint32_t)(*hmp_data & 0x7F) << var_len_shift);
++ hmp_data++;
+ chunk_ofs[i]++;
+
+ if (chunk_delta[i] < smallest_delta) {
+@@ -343,7 +351,11 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ if (chunk_length[i] && *hmp_chunk[i] < 0x80) {
+ do {
+ if (! chunk_length[i]) break;
+- chunk_delta[i] = chunk_delta[i] + ((*hmp_chunk[i] & 0x7F) << var_len_shift);
++ /* Bound the shift (see the other var-len loop above): a
++ * long run of continuation bytes would otherwise drive
++ * var_len_shift past 31 -> undefined shift. */
++ if (var_len_shift < 32)
++ chunk_delta[i] += ((uint32_t)(*hmp_chunk[i] & 0x7F) << var_len_shift);
+ var_len_shift += 7;
+ hmp_chunk[i]++;
+ chunk_length[i]--;
+@@ -353,7 +365,8 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ _WM_GLOBAL_ERROR(WM_ERR_NOT_HMP, "file too short", 0);
+ goto _hmp_end;
+ }
+- chunk_delta[i] = chunk_delta[i] + ((*hmp_chunk[i] & 0x7F) << var_len_shift);
++ if (var_len_shift < 32)
++ chunk_delta[i] += ((uint32_t)(*hmp_chunk[i] & 0x7F) << var_len_shift);
+ hmp_chunk[i]++;
+ chunk_length[i]--;
+ } while (!chunk_delta[i]);
+diff --git a/src/f_midi.c b/src/f_midi.c
+index 4963d4f4..21cd3161 100644
+--- a/src/f_midi.c
++++ b/src/f_midi.c
+@@ -247,7 +247,7 @@ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+
+ subtract_delta = smallest_delta;
+ sample_count_f = (((float) smallest_delta * samples_per_delta_f) + sample_remainder);
+- sample_count = (uint32_t) sample_count_f;
++ sample_count = (sample_count_f >= 4294967296.0f) ? 0xFFFFFFFFu : (sample_count_f > 0.0f) ? (uint32_t) sample_count_f : 0u;
+ sample_remainder = sample_count_f - (float) sample_count;
+
+ mdi->events[mdi->event_count - 1].samples_to_next += sample_count;
+@@ -345,7 +345,7 @@ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+ subtract_delta = smallest_delta;
+ sample_count_f = (((float) smallest_delta * samples_per_delta_f)
+ + sample_remainder);
+- sample_count = (uint32_t) sample_count_f;
++ sample_count = (sample_count_f >= 4294967296.0f) ? 0xFFFFFFFFu : (sample_count_f > 0.0f) ? (uint32_t) sample_count_f : 0u;
+ sample_remainder = sample_count_f - (float) sample_count;
+
+ mdi->events[mdi->event_count - 1].samples_to_next += sample_count;
+@@ -418,7 +418,7 @@ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+ }
+ sample_count_f = (((float) track_delta[i] * samples_per_delta_f)
+ + sample_remainder);
+- sample_count = (uint32_t) sample_count_f;
++ sample_count = (sample_count_f >= 4294967296.0f) ? 0xFFFFFFFFu : (sample_count_f > 0.0f) ? (uint32_t) sample_count_f : 0u;
+ sample_remainder = sample_count_f - (float) sample_count;
+ mdi->events[mdi->event_count - 1].samples_to_next += sample_count;
+ mdi->extra_info.approx_total_samples += sample_count;
+
+From 6fd1b39ba5a585840e9b998be2f98cbbe32152f5 Mon Sep 17 00:00:00 2001
+From: glitchfox <glitchfox@benjaminali.com>
+Date: Thu, 30 Jul 2026 10:09:28 +0000
+Subject: [PATCH 2/4] f_hmp: fail on over-long var-length quantity instead of
+ capping
+
+Per review (@sezero): a var-length quantity that needs var_len_shift >= 32
+cannot fit the 32-bit chunk_delta and only occurs on malformed input, so reject
+it as WM_ERR_CORUPT (goto _hmp_end) rather than silently skipping the shift.
+Applies to both var-len loops. A valid 32-bit delta only reaches shift 28, so
+no well-formed file is rejected.
+
+Signed-off-by: glitchfox <glitchfox@benjaminali.com>
+---
+ src/f_hmp.c | 41 ++++++++++++++++++++++++++---------------
+ 1 file changed, 26 insertions(+), 15 deletions(-)
+
+diff --git a/src/f_hmp.c b/src/f_hmp.c
+index 2b3fbf46..5dfc38d7 100644
+--- a/src/f_hmp.c
++++ b/src/f_hmp.c
+@@ -242,20 +242,25 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ var_len_shift = 0;
+ if (*hmp_data < 0x80) {
+ do {
+- /* var_len_shift is unbounded on malformed input; only accumulate
+- * while the shift is in range for the 32-bit chunk_delta, and
+- * shift as unsigned. Bytes are still consumed so parsing stays
+- * aligned. */
+- if (var_len_shift < 32)
+- chunk_delta[i] |= ((uint32_t)(*hmp_data & 0x7F) << var_len_shift);
++ /* A var-length quantity that needs a shift of 32+ cannot fit the
++ * 32-bit chunk_delta -- reject it as corrupt rather than perform
++ * an out-of-range (undefined) shift. */
++ if (var_len_shift >= 32) {
++ _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "var-length quantity too large", 0);
++ goto _hmp_end;
++ }
++ chunk_delta[i] |= ((uint32_t)(*hmp_data & 0x7F) << var_len_shift);
+ hmp_data++;
+ var_len_shift += 7;
+ chunk_ofs[i]++;
+ } while (hmp_data < data_end && *hmp_data < 0x80);
+ }
+ if (hmp_data == data_end) goto tooshort;
+- if (var_len_shift < 32)
+- chunk_delta[i] |= ((uint32_t)(*hmp_data & 0x7F) << var_len_shift);
++ if (var_len_shift >= 32) {
++ _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "var-length quantity too large", 0);
++ goto _hmp_end;
++ }
++ chunk_delta[i] |= ((uint32_t)(*hmp_data & 0x7F) << var_len_shift);
+ hmp_data++;
+ chunk_ofs[i]++;
+
+@@ -351,11 +356,14 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ if (chunk_length[i] && *hmp_chunk[i] < 0x80) {
+ do {
+ if (! chunk_length[i]) break;
+- /* Bound the shift (see the other var-len loop above): a
+- * long run of continuation bytes would otherwise drive
+- * var_len_shift past 31 -> undefined shift. */
+- if (var_len_shift < 32)
+- chunk_delta[i] += ((uint32_t)(*hmp_chunk[i] & 0x7F) << var_len_shift);
++ /* See the other var-len loop above: a shift of 32+ cannot
++ * fit chunk_delta, so fail closed rather than perform an
++ * out-of-range (undefined) shift. */
++ if (var_len_shift >= 32) {
++ _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "var-length quantity too large", 0);
++ goto _hmp_end;
++ }
++ chunk_delta[i] += ((uint32_t)(*hmp_chunk[i] & 0x7F) << var_len_shift);
+ var_len_shift += 7;
+ hmp_chunk[i]++;
+ chunk_length[i]--;
+@@ -365,8 +373,11 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
+ _WM_GLOBAL_ERROR(WM_ERR_NOT_HMP, "file too short", 0);
+ goto _hmp_end;
+ }
+- if (var_len_shift < 32)
+- chunk_delta[i] += ((uint32_t)(*hmp_chunk[i] & 0x7F) << var_len_shift);
++ if (var_len_shift >= 32) {
++ _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "var-length quantity too large", 0);
++ goto _hmp_end;
++ }
++ chunk_delta[i] += ((uint32_t)(*hmp_chunk[i] & 0x7F) << var_len_shift);
+ hmp_chunk[i]++;
+ chunk_length[i]--;
+ } while (!chunk_delta[i]);
+
+From 0a3afe4082054508b1956d67489b01c1915f935d Mon Sep 17 00:00:00 2001
+From: glitchfox <glitchfox@benjaminali.com>
+Date: Thu, 30 Jul 2026 10:27:56 +0000
+Subject: [PATCH 3/4] f_midi: fail on out-of-range sample count instead of
+ clamping
+
+Per review (@sezero): treat an out-of-range sample_count_f as WM_ERR_CORUPT
+(goto _end) rather than clamping to UINT32_MAX, matching the f_hmp change.
+Three sites in _WM_ParseNewMidi; well-formed files stay in range.
+
+Signed-off-by: glitchfox <glitchfox@benjaminali.com>
+---
+ src/f_midi.c | 6 +++---
+ 1 file changed, 3 insertions(+), 3 deletions(-)
+
+diff --git a/src/f_midi.c b/src/f_midi.c
+index 21cd3161..0ccf4ae8 100644
+--- a/src/f_midi.c
++++ b/src/f_midi.c
+@@ -247,7 +247,7 @@ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+
+ subtract_delta = smallest_delta;
+ sample_count_f = (((float) smallest_delta * samples_per_delta_f) + sample_remainder);
+- sample_count = (sample_count_f >= 4294967296.0f) ? 0xFFFFFFFFu : (sample_count_f > 0.0f) ? (uint32_t) sample_count_f : 0u;
++ if (sample_count_f < 0.0f || sample_count_f >= 4294967296.0f) { _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "sample count out of range", 0); goto _end; } sample_count = (uint32_t) sample_count_f;
+ sample_remainder = sample_count_f - (float) sample_count;
+
+ mdi->events[mdi->event_count - 1].samples_to_next += sample_count;
+@@ -345,7 +345,7 @@ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+ subtract_delta = smallest_delta;
+ sample_count_f = (((float) smallest_delta * samples_per_delta_f)
+ + sample_remainder);
+- sample_count = (sample_count_f >= 4294967296.0f) ? 0xFFFFFFFFu : (sample_count_f > 0.0f) ? (uint32_t) sample_count_f : 0u;
++ if (sample_count_f < 0.0f || sample_count_f >= 4294967296.0f) { _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "sample count out of range", 0); goto _end; } sample_count = (uint32_t) sample_count_f;
+ sample_remainder = sample_count_f - (float) sample_count;
+
+ mdi->events[mdi->event_count - 1].samples_to_next += sample_count;
+@@ -418,7 +418,7 @@ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+ }
+ sample_count_f = (((float) track_delta[i] * samples_per_delta_f)
+ + sample_remainder);
+- sample_count = (sample_count_f >= 4294967296.0f) ? 0xFFFFFFFFu : (sample_count_f > 0.0f) ? (uint32_t) sample_count_f : 0u;
++ if (sample_count_f < 0.0f || sample_count_f >= 4294967296.0f) { _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "sample count out of range", 0); goto _end; } sample_count = (uint32_t) sample_count_f;
+ sample_remainder = sample_count_f - (float) sample_count;
+ mdi->events[mdi->event_count - 1].samples_to_next += sample_count;
+ mdi->extra_info.approx_total_samples += sample_count;
+
+From 76d8d334fc04646efb9b9f88581ae4618e079a2c Mon Sep 17 00:00:00 2001
+From: glitchfox <glitchfox@benjaminali.com>
+Date: Thu, 30 Jul 2026 11:48:06 +0000
+Subject: [PATCH 4/4] f_midi: name the 2^32 bound (WM_TWO_TO_32_F) instead of a
+ magic float
+
+Per review (@psi29a): replace the bare 4294967296.0f literal used to bound
+sample_count_f before the float->uint32 conversion with a named define.
+
+Signed-off-by: glitchfox <glitchfox@benjaminali.com>
+---
+ src/f_midi.c | 9 ++++++---
+ 1 file changed, 6 insertions(+), 3 deletions(-)
+
+diff --git a/src/f_midi.c b/src/f_midi.c
+index 0ccf4ae8..0e69b947 100644
+--- a/src/f_midi.c
++++ b/src/f_midi.c
+@@ -36,6 +36,9 @@
+ #include "reverb.h"
+ #include "sample.h"
+
++/* Smallest float that does NOT fit in a uint32_t (2^32); used to reject an
++ * out-of-range sample count before the float->uint32 conversion. */
++#define WM_TWO_TO_32_F 4294967296.0f
+
+ struct _mdi *
+ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+@@ -247,7 +250,7 @@ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+
+ subtract_delta = smallest_delta;
+ sample_count_f = (((float) smallest_delta * samples_per_delta_f) + sample_remainder);
+- if (sample_count_f < 0.0f || sample_count_f >= 4294967296.0f) { _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "sample count out of range", 0); goto _end; } sample_count = (uint32_t) sample_count_f;
++ if (sample_count_f < 0.0f || sample_count_f >= WM_TWO_TO_32_F) { _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "sample count out of range", 0); goto _end; } sample_count = (uint32_t) sample_count_f;
+ sample_remainder = sample_count_f - (float) sample_count;
+
+ mdi->events[mdi->event_count - 1].samples_to_next += sample_count;
+@@ -345,7 +348,7 @@ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+ subtract_delta = smallest_delta;
+ sample_count_f = (((float) smallest_delta * samples_per_delta_f)
+ + sample_remainder);
+- if (sample_count_f < 0.0f || sample_count_f >= 4294967296.0f) { _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "sample count out of range", 0); goto _end; } sample_count = (uint32_t) sample_count_f;
++ if (sample_count_f < 0.0f || sample_count_f >= WM_TWO_TO_32_F) { _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "sample count out of range", 0); goto _end; } sample_count = (uint32_t) sample_count_f;
+ sample_remainder = sample_count_f - (float) sample_count;
+
+ mdi->events[mdi->event_count - 1].samples_to_next += sample_count;
+@@ -418,7 +421,7 @@ _WM_ParseNewMidi(const uint8_t *midi_data, uint32_t midi_size) {
+ }
+ sample_count_f = (((float) track_delta[i] * samples_per_delta_f)
+ + sample_remainder);
+- if (sample_count_f < 0.0f || sample_count_f >= 4294967296.0f) { _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "sample count out of range", 0); goto _end; } sample_count = (uint32_t) sample_count_f;
++ if (sample_count_f < 0.0f || sample_count_f >= WM_TWO_TO_32_F) { _WM_GLOBAL_ERROR(WM_ERR_CORUPT, "sample count out of range", 0); goto _end; } sample_count = (uint32_t) sample_count_f;
+ sample_remainder = sample_count_f - (float) sample_count;
+ mdi->events[mdi->event_count - 1].samples_to_next += sample_count;
+ mdi->extra_info.approx_total_samples += sample_count;
diff --git a/wildmidi.spec b/wildmidi.spec
index 1134ec7..1458a8c 100644
--- a/wildmidi.spec
+++ b/wildmidi.spec
@@ -1,12 +1,22 @@
Name: wildmidi
Version: 0.5.0
-Release: 1%{?dist}
+Release: 2%{?dist}
Summary: Softsynth midi player
License: GPL-3.0-or-later
URL: https://github.com/Mindwerks/wildmidi
Source0: https://github.com/Mindwerks/%{name}/archive/%{name}-%{version}.tar.gz
+
+# Fix signed-integer-overflow UB in big-endian 32-bit reads across format parsers
+# https://github.com/Mindwerks/wildmidi/pull/293
+Patch0: 293.patch
+
+# Bound variable-length shift and clamp float->uint casts in parsers
+# https://github.com/Mindwerks/wildmidi/pull/294
+Patch1: 294.patch
+
BuildRequires: gcc
BuildRequires: alsa-lib-devel cmake
+
Requires: %{name}-libs%{?_isa} = %{version}-%{release}
%description
@@ -73,6 +83,9 @@ ln -s ../timidity.cfg $RPM_BUILD_ROOT%{_sysconfdir}/%{name}/%{name}.cfg
%changelog
+* Sat Oct 24 2026 Artur Frenszek-Iwicki <fedora@svgames.pl> - 0.5.0-2
+- Backport some upstream patches adding better bounds checking and integer overflow protection
+
* Sun Aug 30 2026 Artur Frenszek-Iwicki <fedora@svgames.pl> - 0.5.0-1
- Update to v0.5.0 (rhbz#2509263, rhbz#2520292)
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-10-24 9:26 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-24 9:26 [rpms/wildmidi] f45: Backport some upstream patches Artur Frenszek-Iwicki
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox