stb_image: Improve error reporting if file operations fail within *_from_file functions. #1420
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Within the stb_image
*_from_file()
APIs, several of the calls toftell()
andfseek()
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()
andstbi_load_from_file_16()
, the effect is that the function's guarantee thatbreaks; 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()
, andstbi_is_16_bit_from_file()
, the return value fromftell()
— which is negative ifftell()
produced an error — is passed tofseek(..., SEEK_SET)
. The ensuing behavior offseek()
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!