Skip to content

Commit 76d8d33

Browse files
committed
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>
1 parent 0a3afe4 commit 76d8d33

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/f_midi.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
#include "reverb.h"
3737
#include "sample.h"
3838

39+
/* Smallest float that does NOT fit in a uint32_t (2^32); used to reject an
40+
* out-of-range sample count before the float->uint32 conversion. */
41+
#define WM_TWO_TO_32_F 4294967296.0f
3942

4043
struct _mdi *
4144
_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) {
247250

248251
subtract_delta = smallest_delta;
249252
sample_count_f = (((float) smallest_delta * samples_per_delta_f) + sample_remainder);
250-
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;
253+
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;
251254
sample_remainder = sample_count_f - (float) sample_count;
252255

253256
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) {
345348
subtract_delta = smallest_delta;
346349
sample_count_f = (((float) smallest_delta * samples_per_delta_f)
347350
+ sample_remainder);
348-
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;
351+
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;
349352
sample_remainder = sample_count_f - (float) sample_count;
350353

351354
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) {
418421
}
419422
sample_count_f = (((float) track_delta[i] * samples_per_delta_f)
420423
+ sample_remainder);
421-
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;
424+
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;
422425
sample_remainder = sample_count_f - (float) sample_count;
423426
mdi->events[mdi->event_count - 1].samples_to_next += sample_count;
424427
mdi->extra_info.approx_total_samples += sample_count;

0 commit comments

Comments
 (0)