Skip to content
Myr

stb_image: fix assert failing when loading BMP

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This fixes two issues with an assert failing. I tested that the first part fixes #909 and the second fixes #897. 1. Loading 16/24/32-bit BMP from memory caused an assert to fail (excluding 16-bit BMP with hsz 12). img_buffer offset was always compared with the buffer for stbi_load_from_file() but stbi_load_from_memory() uses an external buffer. Resolution: Change s->buffer_start to s->img_buffer_original. 2. Loading BMP with large header from file caused assert to fail. img_buffer points to stbi_uc buffer_start[128] but the largest BMP supported has a 138 byte header (hsz 124) causing img_buffer to wrap around to an offset of 10. The assert fails because 138 (header size) != 10 (offset in temp read buffer). Resolution: Add the previously read bytes to the offset in temp read buffer to get the absolute offset. The issues were introduced by the commit c440a53d06 ("stb_image: fix reading BMP with explicit masks").

authored by Zack Middleton · committed by Zack Middleton · parent f54acd4 · Browse files · 1 file+7 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+7 added−3 removed

  • other:+7 added−3 removed

1 file+7 added−3 removed

modified:stb_image.h+7 added-3 removed
105 unmodified lines
106 Aldo Culquicondor Philipp Wiesemann Dale Weiler github:sammyhw107 Oriol Ferrer Mesia Josh Tobin Matthew Gregan github:phprus108 Julian Raschke Gregory Mullen Baldur Karlsson github:poppolopoppo109 Christian Floisand Kevin Schmidt JR Smith github:darealshinji110 Brad Weinberger Matvey Cherevko github:Michaelangel007110 Brad Weinberger Matvey Cherevko Zack Middleton github:Michaelangel007111 Blazej Dariusz Roszkowski Alexander Veselov112*/113114#ifndef STBI_INCLUDE_STB_IMAGE_H
635 unmodified lines
750751 int read_from_callbacks;752 int buflen;753 stbi_uc buffer_start[128];754 int callback_already_read;755756 stbi_uc *img_buffer, *img_buffer_end;757 stbi_uc *img_buffer_original, *img_buffer_original_end;758} stbi__context;
5 unmodified lines
764static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len)765{766 s->io.read = NULL;767 s->read_from_callbacks = 0;768 s->callback_already_read = 0;769 s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer;770 s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len;771}772
3 unmodified lines
776 s->io = *c;777 s->io_user_data = user;778 s->buflen = sizeof(s->buffer_start);779 s->read_from_callbacks = 1;780 s->callback_already_read = 0;778 s->img_buffer_original = s->buffer_start;781 s->img_buffer = s->img_buffer_original = s->buffer_start;782 stbi__refill_buffer(s);783 s->img_buffer_original_end = s->img_buffer_end;784}785
715 unmodified lines
15011502static void stbi__refill_buffer(stbi__context *s)1503{1504 int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);1505 s->callback_already_read += (int) (s->img_buffer - s->img_buffer_original);1506 if (n == 0) {1507 // at end of file, treat same as if from memory, but need to handle case1508 // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file1509 s->read_from_callbacks = 0;
3817 unmodified lines
5327 if (info.bpp < 16)5328 psize = (info.offset - info.extra_read - info.hsz) >> 2;5329 }5330 if (psize == 0) {5327 STBI_ASSERT(info.offset == (s->img_buffer - s->buffer_start));5331 STBI_ASSERT(info.offset == s->callback_already_read + (int) (s->img_buffer - s->img_buffer_original));5332 }53335334 if (info.bpp == 24 && ma == 0xff000000)5335 s->img_n = 3;
2325 unmodified lines