We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5509

Summary: SDL_RWsize() returns wrong value if start pos != 0 ?
Product: SDL Reporter: Ozkan Sezer <sezeroz>
Component: fileAssignee: Sam Lantinga <slouken>
Status: RESOLVED WONTFIX QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: HG 2.0   
Hardware: All   
OS: All   

Description Ozkan Sezer 2021-01-28 03:07:32 UTC
Suppose that I do:

    FILE *f = fopen("test","rb"); /* 100 bytes file */
    fseek(f, 20, SEEK_SET);
    SDL_RWops *rw = SDL_RWFromFP(f);

SDL_RWsize() would give me 100 instead of 80.  Only if I apply the
following then it gives me 80.  Is this an unsupported scenario or
is it an oversight?

diff --git a/src/file/SDL_rwops.c b/src/file/SDL_rwops.c
--- a/src/file/SDL_rwops.c
+++ b/src/file/SDL_rwops.c
@@ -357,7 +357,7 @@ stdio_size(SDL_RWops * context)
     size = SDL_RWseek(context, 0, RW_SEEK_END);
 
     SDL_RWseek(context, pos, RW_SEEK_SET);
-    return size;
+    return size - pos;
 }
 
 static Sint64 SDLCALL
Comment 1 Ozkan Sezer 2021-01-28 03:30:02 UTC
Another scenario is the test case from bug #4410:

    SDL_RWops *file = SDL_RWFromFile("test.dat", "rb");
    SDL_RWseek(file, 69531, RW_SEEK_SET); /* MP3 is 69531 bytes from start */
    Mix_Music *music = Mix_LoadMUS_RW(file, 0);
    Mix_PlayMusic(music, -1);

And see current hg for this:
https://hg.libsdl.org/SDL_mixer/file/a341f68abf34/src/codecs/mp3utils.c#l29

I can easily fix bug #4410 by doing the following:
diff --git a/src/codecs/music_mpg123.c b/src/codecs/music_mpg123.c
--- a/src/codecs/music_mpg123.c
+++ b/src/codecs/music_mpg123.c
@@ -231,6 +231,7 @@ static void *MPG123_CreateFromRW(SDL_RWo
     music->mp3file.src = src;
     music->volume = MIX_MAX_VOLUME;
 
+    music->mp3file.start  = SDL_RWtell(src);
     music->mp3file.length = SDL_RWsize(src);
     if (mp3_skiptags(&music->mp3file, SDL_TRUE) < 0) {
         SDL_free(music);

... _but only if_ I apply the patch stdio_size() patch to SDL2.

SDL_rwops.c::windows_file_size() has the same issue.
Comment 2 Ozkan Sezer 2021-01-28 14:07:51 UTC
I fixed the bug #4410 issue by not using SDL_RWsize() and fixing
other mistakes in the code there.

However the SDL_RWsize() concerns I noted here still remain. Not
sure if they are to be fixed, though.
Comment 3 Ozkan Sezer 2021-02-05 12:26:27 UTC
Closing this as WONTFIX -- any changes to RWsize() would be too
intrusive and result in other possible bugs.