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

stb_image: Improve error reporting if file operations fail within *_from_file functions. #1420

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

NBickford-NV
Copy link
Contributor

@NBickford-NV NBickford-NV commented Dec 10, 2022

Within the stb_image *_from_file() APIs, several of the calls to ftell() and fseek() responsible for resetting the file position don't check for returned error codes. This merge request adds error checks to these calls.

The main goal of this pull request for me is to fix a few warnings from our static analysis tool. It's difficult but possible to get these calls to fail even if the file's been successfully opened: for instance, imagine another process deletes the file or obtains an exclusive lock to the file - or say the file's on a flash drive that's unplugged at just the right time.

In stbi_load_from_file() and stbi_load_from_file_16(), the effect is that the function's guarantee that

// for stbi_load_from_file, file pointer is left pointing immediately after image

breaks; the file could still be readable, but not returning an error when the file position is unknown could lead to trouble if an application uses the FILE pointer afterwards.

In stbi_is_hdr_from_file(), stbi_info_from_file(), and stbi_is_16_bit_from_file(), the return value from ftell() — which is negative if ftell() produced an error — is passed to fseek(..., SEEK_SET). The ensuing behavior of fseek() when called to set a negative seek position probably depends on the C library implementation; Watcom's version of the specification, for instance, prohibits this.

(This does not adjust the credits to avoid merge conflicts with PR #1223).

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants