SDL_mixer: Updated SDL_mixer for SDL read/write size_t change

From 7e8dd2ab8160d2aafd8ca96043d36d0e49f8298c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 8 Aug 2023 17:17:40 -0700
Subject: [PATCH] Updated SDL_mixer for SDL read/write size_t change

---
 external/SDL                                |   2 +-
 src/codecs/load_aiff.c                      |  48 +++++---
 src/codecs/mp3utils.c                       |   7 +-
 src/codecs/music_flac.c                     |  12 +-
 src/codecs/music_ogg.c                      |   7 +-
 src/codecs/music_opus.c                     |   6 +-
 src/codecs/music_wav.c                      | 116 +++++++++-----------
 src/codecs/music_wavpack.c                  |   6 +-
 src/codecs/music_xmp.c                      |   7 +-
 src/codecs/native_midi/native_midi_macosx.c |  15 +--
 src/codecs/stb_vorbis/stb_vorbis.h          |   2 +-
 src/codecs/timidity/TODO                    |   2 -
 src/codecs/timidity/instrum.c               |   2 +-
 src/codecs/timidity/readmidi.c              |  77 +++++++++----
 14 files changed, 161 insertions(+), 148 deletions(-)

diff --git a/external/SDL b/external/SDL
index a509771a..8a1afc9b 160000
--- a/external/SDL
+++ b/external/SDL
@@ -1 +1 @@
-Subproject commit a509771a87559ca040a40b5fef932a8220343f7a
+Subproject commit 8a1afc9b10e9153f764f562008795b415d0575e1
diff --git a/src/codecs/load_aiff.c b/src/codecs/load_aiff.c
index d636f8cf..b243d758 100644
--- a/src/codecs/load_aiff.c
+++ b/src/codecs/load_aiff.c
@@ -91,6 +91,9 @@ SDL_AudioSpec *Mix_LoadAIFF_RW (SDL_RWops *src, SDL_bool freesrc,
     Uint8 sane_freq[10];
     Uint32 frequency = 0;
 
+    /* VHDR chunk */
+    Uint16 frequency16 = 0;
+
     /* Sanity checks */
     if (audio_buf) {
         *audio_buf = NULL;
@@ -112,14 +115,18 @@ SDL_AudioSpec *Mix_LoadAIFF_RW (SDL_RWops *src, SDL_bool freesrc,
         goto done;
     }
 
-    FORMchunk   = SDL_ReadLE32(src);
-    chunk_length    = SDL_ReadBE32(src);
+    if (!SDL_ReadU32LE(src, &FORMchunk) ||
+        !SDL_ReadU32BE(src, &chunk_length)) {
+        goto done;
+    }
     if (chunk_length == AIFF) { /* The FORMchunk has already been read */
         AIFFmagic    = chunk_length;
         chunk_length = FORMchunk;
         FORMchunk    = FORM;
     } else {
-        AIFFmagic    = SDL_ReadLE32(src);
+        if (!SDL_ReadU32LE(src, &AIFFmagic)) {
+            goto done;
+        }
     }
     if ((FORMchunk != FORM) || ((AIFFmagic != AIFF) && (AIFFmagic != _8SVX))) {
         Mix_SetError("Unrecognized file type (not AIFF nor 8SVX)");
@@ -134,8 +141,10 @@ SDL_AudioSpec *Mix_LoadAIFF_RW (SDL_RWops *src, SDL_bool freesrc,
     found_BODY = 0;
 
     do {
-        chunk_type  = SDL_ReadLE32(src);
-        chunk_length = SDL_ReadBE32(src);
+        if (!SDL_ReadU32LE(src, &chunk_type) ||
+            !SDL_ReadU32BE(src, &chunk_length)) {
+            goto done;
+        }
         next_chunk  = SDL_RWtell(src) + chunk_length;
         /* Paranoia to avoid infinite loops */
         if (chunk_length == 0)
@@ -144,22 +153,26 @@ SDL_AudioSpec *Mix_LoadAIFF_RW (SDL_RWops *src, SDL_bool freesrc,
         switch (chunk_type) {
             case SSND:
                 found_SSND  = 1;
-                offset      = SDL_ReadBE32(src);
-                blocksize   = SDL_ReadBE32(src);
-                start       = SDL_RWtell(src) + offset;
+                if (!SDL_ReadU32BE(src, &offset) ||
+                    !SDL_ReadU32BE(src, &blocksize)) {
+                    goto done;
+                }
+                start = SDL_RWtell(src) + offset;
                 (void)blocksize; /* unused. */
                 break;
 
             case COMM:
                 found_COMM  = 1;
-                channels    = SDL_ReadBE16(src);
-                numsamples  = SDL_ReadBE32(src);
-                samplesize  = SDL_ReadBE16(src);
+                if (!SDL_ReadU16BE(src, &channels) ||
+                    !SDL_ReadU32BE(src, &numsamples) ||
+                    !SDL_ReadU16BE(src, &samplesize)) {
+                    goto done;
+                }
                 if (SDL_RWread(src, sane_freq, sizeof(sane_freq)) != sizeof(sane_freq)) {
                     Mix_SetError("Bad AIFF sample frequency");
                     goto done;
                 }
-                frequency   = SANE_to_Uint32(sane_freq);
+                frequency = SANE_to_Uint32(sane_freq);
                 if (frequency == 0) {
                     Mix_SetError("Bad AIFF sample frequency");
                     goto done;
@@ -168,12 +181,15 @@ SDL_AudioSpec *Mix_LoadAIFF_RW (SDL_RWops *src, SDL_bool freesrc,
 
             case VHDR:
                 found_VHDR  = 1;
-                SDL_ReadBE32(src);
-                SDL_ReadBE32(src);
-                SDL_ReadBE32(src);
-                frequency = SDL_ReadBE16(src);
+                if (!SDL_ReadU32BE(src, NULL) ||
+                    !SDL_ReadU32BE(src, NULL) ||
+                    !SDL_ReadU32BE(src, NULL) ||
+                    !SDL_ReadU16BE(src, &frequency16)) {
+                    goto done;
+                }
                 channels = 1;
                 samplesize = 8;
+                frequency = frequency16;
                 break;
 
             case BODY:
diff --git a/src/codecs/mp3utils.c b/src/codecs/mp3utils.c
index 7938dc78..8740734c 100644
--- a/src/codecs/mp3utils.c
+++ b/src/codecs/mp3utils.c
@@ -53,15 +53,12 @@ int MP3_RWinit(struct mp3file_t *fil, SDL_RWops *src) {
 
 size_t MP3_RWread(struct mp3file_t *fil, void *ptr, size_t size, size_t maxnum) {
     size_t remaining = (size_t)(fil->length - fil->pos);
-    Sint64 ret;
+    size_t ret;
     maxnum *= size;
     if (maxnum > remaining) maxnum = remaining;
     ret = SDL_RWread(fil->src, ptr, maxnum);
-    if (ret <= 0) {
-        return 0;
-    }
     fil->pos += ret;
-    return (size_t)ret;
+    return ret;
 }
 
 Sint64 MP3_RWseek(struct mp3file_t *fil, Sint64 offset, int whence) {
diff --git a/src/codecs/music_flac.c b/src/codecs/music_flac.c
index 80fbcb76..44fffb71 100644
--- a/src/codecs/music_flac.c
+++ b/src/codecs/music_flac.c
@@ -171,12 +171,14 @@ static FLAC__StreamDecoderReadStatus flac_read_music_cb(
 
     /* make sure there is something to be reading */
     if (*bytes > 0) {
-        Sint64 amount = SDL_RWread (data->src, buffer, *bytes);
-        if (amount <= 0) { /* error or no data was read (EOF) */
-            return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
-        } else { /* data was read, continue */
-            *bytes = (size_t)amount;
+        *bytes = SDL_RWread(data->src, buffer, *bytes);
+        if (data->src->status == SDL_RWOPS_STATUS_READY ||
+            data->src->status == SDL_RWOPS_STATUS_NOT_READY) {
             return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
+        } else if (data->src->status == SDL_RWOPS_STATUS_EOF) {
+            return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
+        } else {
+            return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
         }
     } else {
         return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
diff --git a/src/codecs/music_ogg.c b/src/codecs/music_ogg.c
index a35085aa..ff2e2b20 100644
--- a/src/codecs/music_ogg.c
+++ b/src/codecs/music_ogg.c
@@ -166,11 +166,10 @@ static int set_ov_error(const char *function, int error)
 
 static size_t sdl_read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
 {
-    Sint64 amount = SDL_RWread((SDL_RWops*)datasource, ptr, size * nmemb);
-    if (amount <= 0) {
-        return 0;
+    if (size > 0 && nmemb > 0) {
+        return SDL_RWread((SDL_RWops*)datasource, ptr, size * nmemb) / size;
     }
-    return (size_t)(amount / size);
+    return 0;
 }
 
 static int sdl_seek_func(void *datasource, ogg_int64_t offset, int whence)
diff --git a/src/codecs/music_opus.c b/src/codecs/music_opus.c
index 3184fbc8..6c01ed6f 100644
--- a/src/codecs/music_opus.c
+++ b/src/codecs/music_opus.c
@@ -146,11 +146,7 @@ static int set_op_error(const char *function, int error)
 
 static int sdl_read_func(void *datasource, unsigned char *ptr, int size)
 {
-    Sint64 amount = SDL_RWread((SDL_RWops*)datasource, ptr, size);
-    if (amount <= 0) {
-        return 0;
-    }
-    return (int)amount;
+    return (int)SDL_RWread((SDL_RWops*)datasource, ptr, (size_t)size);
 }
 
 static int sdl_seek_func(void *datasource, opus_int64 offset, int whence)
diff --git a/src/codecs/music_wav.c b/src/codecs/music_wav.c
index 9729689a..e04d7fe2 100644
--- a/src/codecs/music_wav.c
+++ b/src/codecs/music_wav.c
@@ -239,13 +239,14 @@ static void *WAV_CreateFromRW(SDL_RWops *src, SDL_bool freesrc)
     music->decode = fetch_pcm;
     music->encoding = PCM_CODE;
 
-    magic = SDL_ReadLE32(src);
-    if (magic == RIFF || magic == WAVE) {
-        loaded = LoadWAVMusic(music);
-    } else if (magic == FORM) {
-        loaded = LoadAIFFMusic(music);
-    } else {
-        Mix_SetError("Unknown WAVE format");
+    if (SDL_ReadU32LE(src, &magic)) {
+        if (magic == RIFF || magic == WAVE) {
+            loaded = LoadWAVMusic(music);
+        } else if (magic == FORM) {
+            loaded = LoadAIFFMusic(music);
+        } else {
+            Mix_SetError("Unknown WAVE format");
+        }
     }
     if (!loaded) {
         WAV_Delete(music);
@@ -310,11 +311,7 @@ static void WAV_Stop(void *context)
 static int fetch_pcm(void *context, int length)
 {
     WAV_Music *music = (WAV_Music *)context;
-    Sint64 amount = SDL_RWread(music->src, music->buffer, length);
-    if (amount <= 0) {
-        return 0;
-    }
-    return (int)amount;
+    return (int)SDL_RWread(music->src, music->buffer, (size_t)length);
 }
 
 static Uint32 PCM_S24_to_S32_BE(Uint8 *x) {
@@ -339,12 +336,7 @@ static int fetch_pcm24be(void *context, int length)
 {
     WAV_Music *music = (WAV_Music *)context;
     int i = 0, o = 0;
-    Sint64 amount = SDL_RWread(music->src, music->buffer, ((length / 4) * 3));
-    if (amount <= 0) {
-        length = 0;
-    } else {
-        length = (int)amount;
-    }
+    length = (int)SDL_RWread(music->src, music->buffer, (size_t)((length / 4) * 3));
     if (length % music->samplesize != 0) {
         length -= length % music->samplesize;
     }
@@ -362,12 +354,7 @@ static int fetch_pcm24le(void *context, int length)
 {
     WAV_Music *music = (WAV_Music *)context;
     int i = 0, o = 0;
-    Sint64 amount = SDL_RWread(music->src, music->buffer, ((length / 4) * 3));
-    if (amount <= 0) {
-        length = 0;
-    } else {
-        length = (int)amount;
-    }
+    length = (int)SDL_RWread(music->src, music->buffer, (size_t)((length / 4) * 3));
     if (length % music->samplesize != 0) {
         length -= length % music->samplesize;
     }
@@ -406,12 +393,7 @@ static int fetch_float64be(void *context, int length)
 {
     WAV_Music *music = (WAV_Music *)context;
     int i = 0, o = 0;
-    Sint64 amount = SDL_RWread(music->src, music->buffer, length);
-    if (amount <= 0) {
-        length = 0;
-    } else {
-        length = (int)amount;
-    }
+    length = (int)SDL_RWread(music->src, music->buffer, (size_t)length);
     if (length % music->samplesize != 0) {
         length -= length % music->samplesize;
     }
@@ -434,12 +416,7 @@ static int fetch_float64le(void *context, int length)
 {
     WAV_Music *music = (WAV_Music *)context;
     int i = 0, o = 0;
-    Sint64 amount = SDL_RWread(music->src, music->buffer, length);
-    if (amount <= 0) {
-        length = 0;
-    } else {
-        length = (int)amount;
-    }
+    length = (int)SDL_RWread(music->src, music->buffer, (size_t)length);
     if (length % music->samplesize != 0) {
         length -= length % music->samplesize;
     }
@@ -1026,12 +1003,12 @@ static int fetch_adpcm(void *context, int length, int (*DecodeBlockHeader)(ADPCM
 
     while (left > 0) {
         if (state->output.read == state->output.pos) {
-            Sint64 bytesread = SDL_RWread(music->src, state->block.data, state->blocksize);
-            if (bytesread < 0) {
-                return -1;
+            size_t bytesread = SDL_RWread(music->src, state->block.data, state->blocksize);
+            if (bytesread == 0) {
+                break;
             }
 
-            state->block.size = (size_t)bytesread < state->blocksize ? (size_t)bytesread : state->blocksize;
+            state->block.size = bytesread < state->blocksize ? bytesread : state->blocksize;
             state->block.pos = 0;
             state->output.pos = 0;
             state->output.read = 0;
@@ -1150,12 +1127,7 @@ static int fetch_xlaw(Sint16 (*decode_sample)(Uint8), void *context, int length)
 {
     WAV_Music *music = (WAV_Music *)context;
     int i = 0, o = 0;
-    Sint64 amount = SDL_RWread(music->src, music->buffer, length / 2);
-    if (amount <= 0) {
-        length = 0;
-    } else {
-        length = (int)amount;
-    }
+    length = (int)SDL_RWread(music->src, music->buffer, (size_t)(length / 2));
     if (length % music->samplesize != 0) {
         length -= length % music->samplesize;
     }
@@ -1374,7 +1346,6 @@ static SDL_bool ParseFMT(WAV_Music *wave, Uint32 chunk_length)
     WaveFMTEx fmt;
     size_t size;
     int bits;
-    Sint64 nbread;
     Uint8 *chunk;
 
     if (chunk_length < sizeof(fmt.format)) {
@@ -1388,8 +1359,7 @@ static SDL_bool ParseFMT(WAV_Music *wave, Uint32 chunk_length)
         return SDL_FALSE;
     }
 
-    nbread = SDL_RWread(wave->src, chunk, chunk_length);
-    if (nbread < 0 || (Uint64)nbread != (Uint64)chunk_length) {
+    if (SDL_RWread(wave->src, chunk, chunk_length) != chunk_length) {
         Mix_SetError("Couldn't read %d bytes from WAV file", chunk_length);
         SDL_free(chunk);
         return SDL_FALSE;
@@ -1672,16 +1642,20 @@ static SDL_bool LoadWAVMusic(WAV_Music *wave)
     meta_tags_init(&wave->tags);
 
     /* Check the magic header */
-    wavelen = SDL_ReadLE32(src);
-    WAVEmagic = SDL_ReadLE32(src);
+    if (!SDL_ReadU32LE(src, &wavelen) ||
+        !SDL_ReadU32LE(src, &WAVEmagic)) {
+        return SDL_FALSE;
+    }
 
     (void)wavelen;   /* unused */
     (void)WAVEmagic; /* unused */
 
     /* Read the chunks */
     for (; ;) {
-        chunk_type = SDL_ReadLE32(src);
-        chunk_length = SDL_ReadLE32(src);
+        if (!SDL_ReadU32LE(src, &chunk_type) ||
+            !SDL_ReadU32LE(src, &chunk_length)) {
+            return SDL_FALSE;
+        }
 
         if (chunk_length == 0)
             break;
@@ -1787,8 +1761,10 @@ static SDL_bool LoadAIFFMusic(WAV_Music *wave)
     file_length = SDL_RWsize(src);
 
     /* Check the magic header */
-    chunk_length = SDL_ReadBE32(src);
-    AIFFmagic = SDL_ReadLE32(src);
+    if (!SDL_ReadU32BE(src, &chunk_length) ||
+        !SDL_ReadU32LE(src, &AIFFmagic)) {
+        return SDL_FALSE;
+    }
     if (AIFFmagic != AIFF && AIFFmagic != AIFC) {
         Mix_SetError("Unrecognized file type (not AIFF or AIFC)");
         return SDL_FALSE;
@@ -1804,9 +1780,11 @@ static SDL_bool LoadAIFFMusic(WAV_Music *wave)
      *       contains compressed sound data?
      */
     do {
-        chunk_type      = SDL_ReadLE32(src);
-        chunk_length    = SDL_ReadBE32(src);
-        next_chunk      = SDL_RWtell(src) + chunk_length;
+        if (!SDL_ReadU32LE(src, &chunk_type) ||
+            !SDL_ReadU32BE(src, &chunk_length)) {
+            return SDL_FALSE;
+        }
+        next_chunk = SDL_RWtell(src) + chunk_length;
 
         if (chunk_length % 2) {
             next_chunk++;
@@ -1815,15 +1793,19 @@ static SDL_bool LoadAIFFMusic(WAV_Music *wave)
         switch (chunk_type) {
         case SSND:
             found_SSND = SDL_TRUE;
-            offset = SDL_ReadBE32(src);
-            blocksize = SDL_ReadBE32(src);
+            if (!SDL_ReadU32BE(src, &offset) ||
+                !SDL_ReadU32BE(src, &blocksize)) {
+                return SDL_FALSE;
+            }
             wave->start = SDL_RWtell(src) + offset;
             (void)blocksize; /* unused */
             break;
 
         case FVER:
             found_FVER = SDL_TRUE;
-            AIFCVersion1 = SDL_ReadBE32(src);
+            if (!SDL_ReadU32BE(src, &AIFCVersion1)) {
+                return SDL_FALSE;
+            }
             (void)AIFCVersion1; /* unused */
             break;
 
@@ -1857,15 +1839,17 @@ static SDL_bool LoadAIFFMusic(WAV_Music *wave)
             found_COMM = SDL_TRUE;
 
             /* Read the audio data format chunk */
-            channels = SDL_ReadBE16(src);
-            numsamples = SDL_ReadBE32(src);
-            samplesize = SDL_ReadBE16(src);
-            if (SDL_RWread(src, sane_freq, sizeof(sane_freq)) != sizeof(sane_freq)) {
+            if (!SDL_ReadU16BE(src, &channels) ||
+                !SDL_ReadU32BE(src, &numsamples) ||
+                !SDL_ReadU16BE(src, &samplesize) ||
+                SDL_RWread(src, sane_freq, sizeof(sane_freq)) != sizeof(sane_freq)) {
                 return SDL_FALSE;
             }
             frequency = SANE_to_Uint32(sane_freq);
             if (is_AIFC) {
-                compressionType = SDL_ReadLE32(src);
+                if (!SDL_ReadU32LE(src, &compressionType)) {
+                    return SDL_FALSE;
+                }
                 /* here must be a "compressionName" which is a padded string */
             }
             break;
diff --git a/src/codecs/music_wavpack.c b/src/codecs/music_wavpack.c
index 34e5777f..bd1d1bb8 100644
--- a/src/codecs/music_wavpack.c
+++ b/src/codecs/music_wavpack.c
@@ -198,11 +198,7 @@ typedef struct {
 
 static int32_t sdl_read_bytes(void *id, void *data, int32_t bcount)
 {
-    Sint64 amount = SDL_RWread((SDL_RWops*)id, data, bcount);
-    if (amount <= 0) {
-        return 0;
-    }
-    return (int32_t)amount;
+    return (int32_t)SDL_RWread((SDL_RWops*)id, data, (size_t)bcount);
 }
 
 static uint32_t sdl_get_pos32(void *id)
diff --git a/src/codecs/music_xmp.c b/src/codecs/music_xmp.c
index 75b06239..8a397c26 100644
--- a/src/codecs/music_xmp.c
+++ b/src/codecs/music_xmp.c
@@ -177,11 +177,10 @@ static void libxmp_set_error(int e)
 }
 
 static unsigned long xmp_fread(void *dst, unsigned long len, unsigned long nmemb, void *src) {
-    Sint64 amount = SDL_RWread((SDL_RWops*)src, dst, len * nmemb);
-    if (amount <= 0) {
-        return 0;
+    if (len > 0 && nmemb > 0) {
+        return SDL_RWread((SDL_RWops*)src, dst, len * nmemb) / len;
     }
-    return (unsigned long)(amount / len);
+    return 0;
 }
 static int xmp_fseek(void *src, long offset, int whence) {
     return (SDL_RWseek((SDL_RWops*)src, offset, whence) < 0)? -1 : 0;
diff --git a/src/codecs/native_midi/native_midi_macosx.c b/src/codecs/native_midi/native_midi_macosx.c
index 5f18ded1..44fd1444 100644
--- a/src/codecs/native_midi/native_midi_macosx.c
+++ b/src/codecs/native_midi/native_midi_macosx.c
@@ -200,24 +200,13 @@ NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *src, SDL_bool freesrc)
 {
     NativeMidiSong *retval = NULL;
     void *buf = NULL;
-    Sint64 len = 0;
+    size_t len = 0;
     CFDataRef data = NULL;
 
-    if (SDL_RWseek(src, 0, SDL_RW_SEEK_END) < 0)
-        goto fail;
-    len = SDL_RWtell(src);
-    if (len < 0)
-        goto fail;
-    if (SDL_RWseek(src, 0, SDL_RW_SEEK_SET) < 0)
-        goto fail;
-
-    buf = SDL_malloc(len);
+    buf = SDL_LoadFile_RW(src, &len, SDL_FALSE);
     if (buf == NULL)
         goto fail;
 
-    if (SDL_RWread(src, buf, len) != len)
-        goto fail;
-
     retval = SDL_malloc(sizeof(NativeMidiSong));
     if (retval == NULL)
         goto fail;
diff --git a/src/codecs/stb_vorbis/stb_vorbis.h b/src/codecs/stb_vorbis/stb_vorbis.h
index 554620df..77685c0b 100644
--- a/src/codecs/stb_vorbis/stb_vorbis.h
+++ b/src/codecs/stb_vorbis/stb_vorbis.h
@@ -1417,7 +1417,7 @@ static uint32 get32(vorb *f)
 static int getn(vorb *z, uint8 *data, int n)
 {
    #ifdef STB_VORBIS_SDL
-   if (SDL_RWread(z->rwops, data, n) == n) return 1;
+   if (SDL_RWread(z->rwops, data, n) == (size_t)n) return 1;
    z->eof = 1;
    return 0;
 
diff --git a/src/codecs/timidity/TODO b/src/codecs/timidity/TODO
index 69b37ee2..caeadde9 100644
--- a/src/codecs/timidity/TODO
+++ b/src/codecs/timidity/TODO
@@ -3,8 +3,6 @@
 
 * Much of the code looks ugly to me.
 
-* The return value from SDL_RWread() is checked inconsistenly.
-
 * Group the members of MidiSong into logical units, i.e. structs?
 
 * The debug messages are probably a bit too noisy. I've removed one
diff --git a/src/codecs/timidity/instrum.c b/src/codecs/timidity/instrum.c
index 2a6be628..875039b1 100644
--- a/src/codecs/timidity/instrum.c
+++ b/src/codecs/timidity/instrum.c
@@ -367,7 +367,7 @@ static void load_instrument(MidiSong *song, const char *name,
       sp->data = (sample_t *) SDL_malloc(sp->data_length+4);
       if (!sp->data) goto nomem;
 
-      if (sp->data_length != SDL_RWread(rw, sp->data, sp->data_length))
+      if (SDL_RWread(rw, sp->data, sp->data_length) != (size_t)sp->data_length)
 	goto badread;
 
       if (!(sp->modes & MODES_16BIT)) /* convert to 16-bit data */
diff --git a/src/codecs/timidity/readmidi.c b/src/codecs/timidity/readmidi.c
index 3d7ac2f3..a189b017 100644
--- a/src/codecs/timidity/readmidi.c
+++ b/src/codecs/timidity/readmidi.c
@@ -37,7 +37,7 @@ static Sint32 getvl(SDL_RWops *rw)
   Uint8 c;
   for (;;)
     {
-      if (SDL_RWread(rw, &c, 1) != 1) return l;
+      if (!SDL_ReadU8(rw, &c)) return l;
       l += (c & 0x7f);
       if (!(c & 0x80)) return l;
       l<<=7;
@@ -58,7 +58,7 @@ static int dumpstring(SDL_RWops *rw, Sint32 len, Uint8 type)
       SDL_RWseek(rw, len, SDL_RW_SEEK_CUR);/* should I ? */
       return -1;
     }
-  if (len != (Sint32) SDL_RWread(rw, s, len))
+  if (SDL_RWread(rw, s, len) != (size_t)len)
     {
       SDL_free(s);
       return -1;
@@ -102,7 +102,7 @@ static MidiEventList *read_midi_event(MidiSong *song)
   for (;;)
     {
       song->at += getvl(song->rw);
-      if (SDL_RWread(song->rw, &me, 1) != 1)
+      if (!SDL_ReadU8(song->rw, &me))
 	{
 	  SNDDBG(("read_midi_event: SDL_RWread() failure\n"));
 	  return NULL;
@@ -115,7 +115,11 @@ static MidiEventList *read_midi_event(MidiSong *song)
 	}
       else if(me==0xFF) /* Meta event */
 	{
-	  SDL_RWread(song->rw, &type, 1);
+	  if (!SDL_ReadU8(song->rw, &type))
+	  {
+	    SNDDBG(("read_midi_event: SDL_RWread() failure\n"));
+	    return NULL;
+	  }
 	  len=getvl(song->rw);
 	  if (type>0 && type<16)
 	    {
@@ -132,14 +136,22 @@ static MidiEventList *read_midi_event(MidiSong *song)
 		return MAGIC_EOT;
 
 	      case 0x51: /* Tempo */
-		SDL_RWread(song->rw, &a, 1);
-		SDL_RWread(song->rw, &b, 1);
-		SDL_RWread(song->rw, &c, 1);
+                if (!SDL_ReadU8(song->rw, &a) ||
+                    !SDL_ReadU8(song->rw, &b) ||
+                    !SDL_ReadU8(song->rw, &c))
+        	  {
+        	    SNDDBG(("read_midi_event: SDL_RWread() failure\n"));
+        	    return NULL;
+        	  }
 		MIDIEVENT(song->at, ME_TEMPO, c, a, b);
 
 	      default:
 		SNDDBG(("(Meta event type 0x%02x, length %d)\n", type, len));
-		SDL_RWseek(song->rw, len, SDL_RW_SEEK_CUR);
+                if (SDL_RWseek(song->rw, len, SDL_RW_SEEK_CUR) < 0)
+        	  {
+        	    SNDDBG(("read_midi_event: SDL_RWseek() failure\n"));
+        	    return NULL;
+        	  }
 		break;
 	      }
 	}
@@ -150,28 +162,48 @@ static MidiEventList *read_midi_event(MidiSong *song)
 	    {
 	      lastchan=a & 0x0F;
 	      laststatus=(a>>4) & 0x07;
-	      SDL_RWread(song->rw, &a, 1);
+              if (!SDL_ReadU8(song->rw, &a))
+                {
+        	  SNDDBG(("read_midi_event: SDL_RWread() failure\n"));
+                  return NULL;
+                }
 	      a &= 0x7F;
 	    }
 	  switch(laststatus)
 	    {
 	    case 0: /* Note off */
-	      SDL_RWread(song->rw, &b, 1);
+              if (!SDL_ReadU8(song->rw, &b))
+                {
+        	  SNDDBG(("read_midi_event: SDL_RWread() failure\n"));
+                  return NULL;
+                }
 	      b &= 0x7F;
 	      MIDIEVENT(song->at, ME_NOTEOFF, lastchan, a,b);
 
 	    case 1: /* Note on */
-	      SDL_RWread(song->rw, &b, 1);
+              if (!SDL_ReadU8(song->rw, &b))
+                {
+        	  SNDDBG(("read_midi_event: SDL_RWread() failure\n"));
+                  return NULL;
+                }
 	      b &= 0x7F;
 	      MIDIEVENT(song->at, ME_NOTEON, lastchan, a,b);
 
 	    case 2: /* Key Pressure */
-	      SDL_RWread(song->rw, &b, 1);
+              if (!SDL_ReadU8(song->rw, &b))
+                {
+        	  SNDDBG(("read_midi_event: SDL_RWread() failure\n"));
+                  return NULL;
+                }
 	      b &= 0x7F;
 	      MIDIEVENT(song->at, ME_KEYPRESSURE, lastchan, a, b);
 
 	    case 3: /* Control change */
-	      SDL_RWread(song->rw, &b, 1);
+              if (!SDL_ReadU8(song->rw, &b))
+                {
+        	  SNDDBG(("read_midi_event: SDL_RWread() failure\n"));
+                  return NULL;
+                }
 	      b &= 0x7F;
 	      {
 		int control=255;
@@ -251,7 +283,11 @@ static MidiEventList *read_midi_event(MidiSong *song)
 	      break;
 
 	    case 6: /* Pitch wheel */
-	      SDL_RWread(song->rw, &b, 1);
+              if (!SDL_ReadU8(song->rw, &b))
+                {
+        	  SNDDBG(("read_midi_event: SDL_RWread() failure\n"));
+                  return NULL;
+                }
 	      b &= 0x7F;
 	      MIDIEVENT(song->at, ME_PITCHWHEEL, lastchan, a, b);
 
@@ -554,12 +590,13 @@ MidiEvent *read_midi_file(MidiSong *song, Sint32 *count, Sint32 *sp)
     }
 
   format=tracks=divisions_tmp = -1;
-  SDL_RWread(song->rw, &format, 2);
-  SDL_RWread(song->rw, &tracks, 2);
-  SDL_RWread(song->rw, &divisions_tmp, 2);
-  format=SDL_SwapBE16(format);
-  tracks=SDL_SwapBE16(tracks);
-  divisions_tmp=SDL_SwapBE16(divisions_tmp);
+  if (!SDL_ReadS16BE(song->rw, &format) ||
+      !SDL_ReadS16BE(song->rw, &tracks) ||
+      !SDL_ReadS16BE(song->rw, &divisions_tmp))
+    {
+      SNDDBG(("Not a MIDI file!\n"));
+      return NULL;
+    }
 
   if (divisions_tmp<0)
     {