SDL icon indicating copy to clipboard operation
SDL copied to clipboard

Remove/replace SDL_Read(BE|LE)*

Open icculus opened this issue 3 years ago • 5 comments

These functions...

Uint32
SDL_ReadLE32(SDL_RWops *src)
{
    Uint32 value = 0;

    SDL_RWread(src, &value, sizeof(value));
    return SDL_SwapLE32(value);
}

...do not report i/o errors, so you might get a byteswapped zero that isn't actually the data you thought it would be, and no way to know it failed.

The Write equivalent of these functions do report errors; they return a size_t that's either 1 or 0 depending on whether the write succeeded. I can live with that, but the size_t seems excessive, and also these functions are two lines each, so perhaps not strictly necessary to be in SDL3.

Should we remove or replace these?

icculus avatar Dec 14 '22 17:12 icculus

They've been handy for various projects, though I agree the lack of error checking is a problem. The write functions were written before SDL_bool, and should probably be switched to that.

Maybe the read versions should be something like this?

SDL_bool SDL_ReadLE32(SDL_RWops *src, Uint32 *value)
{
    Uint32 v;
    if (SDL_RWread(src, &v, sizeof(v)) == sizeof(v)) {
        if (value) {
            *value = SDL_SwapLE32(v);
        }
        return SDL_TRUE;
    }
    return SDL_FALSE;
}

slouken avatar Dec 14 '22 19:12 slouken

Or maybe they should return int, for consistency with the rest of the API?

slouken avatar Dec 14 '22 19:12 slouken

In c++ and rustland, I've seen uses of try_ variants to allow functions to fail. So for SDL in c land, have:

SDL_bool SDL_TryReadLE32(SDL_RWops *src, Uint32 *value);  // can fail
Uint32 SDL_ReadLE32(SDL_RWops *src); // cannot fail

These would also translate fine to these safe language bindings.

madebr avatar Dec 14 '22 19:12 madebr

They've been handy for various projects, though I agree the lack of error checking is a problem. The write functions were written before SDL_bool, and should probably be switched to that.

Maybe the read versions should be something like this?

SDL_bool SDL_ReadLE32(SDL_RWops *src, Uint32 *value)
{
    Uint32 v;
    if (SDL_RWread(src, &v, sizeof(v)) == sizeof(v)) {
        if (value) {
            *value = SDL_SwapLE32(v);
        }
        return SDL_TRUE;
    }
    return SDL_FALSE;
}

Looks reasonable to me.

sezero avatar Dec 14 '22 19:12 sezero

Might go with an int return value (success, failure, this-rwops-is-non-blocking-please-retry-later).

icculus avatar Dec 14 '22 22:12 icculus

Sam took care of this in b903ccf945a75266ebcba2df68da3b796a71e75d.

icculus avatar Aug 16 '23 14:08 icculus