Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdl: don't return error when reading 0 bytes #784

Merged
merged 2 commits into from
Jan 13, 2023

Conversation

Daft-Freak
Copy link
Collaborator

Here's an easy one. Avoids errors at the end of reading files.

Reaching the end of the file is not an error. This is consistent with stm32/pico.
@ali1234
Copy link
Contributor

ali1234 commented Sep 16, 2022

As Github said, size_t is unsigned. So >= 0 is always true.

@Daft-Freak
Copy link
Collaborator Author

Ah right, and SDL_RWread returns 0 if there's an error or at the end of the file. 🤔

@Daft-Freak
Copy link
Collaborator Author

Ugh

    SDL_ClearError();
    size_t bytes_read = SDL_RWread(file, buffer, 1, length);

    if(!SDL_GetError()[0])
      return (int32_t)bytes_read;

@ali1234
Copy link
Contributor

ali1234 commented Sep 16, 2022

This doesn't seem right either. The doc says "call SDL_GetError() for more information." but the doc for SDL_GetError says "You should not use the results of SDL_GetError() to decide if an error has occurred! Sometimes SDL will set an error string even when reporting success. SDL will not clear the error string for successful API calls. You must check return values for failure cases before you can assume the error string applies."

So that means SDL_GetError might report a different error. I don't see how it is possible to tell the difference between EOF and error with this API.

@Daft-Freak
Copy link
Collaborator Author

Yeah, that was part of the "ugh". I did clear the error before the call, but it's still nasty. Maybe we'll just have to rewrite it to not use rwops (as it's only using files anyway)...

@ali1234
Copy link
Contributor

ali1234 commented Sep 16, 2022

Seems reasonable since the errors are per-thread and nothing else gets called in between.

I asked the question on SDL forums: https://discourse.libsdl.org/t/how-to-tell-whether-an-error-happened-in-sdl-rwread/38828

0 could mean EOF and not an error, clearing and then checking for an error seems to be the only way to check...
@Gadgetoid Gadgetoid merged commit 38cdd5b into 32blit:master Jan 13, 2023
@Gadgetoid
Copy link
Contributor

Thank you both!

@Daft-Freak
Copy link
Collaborator Author

Hah, they fixed the "can't tell if EOF or error" problem... in SDL3.

@Daft-Freak Daft-Freak deleted the patch-1 branch January 13, 2023 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants