@@ -229,12 +229,23 @@ class IOCache
229229
230230 BOOL isNull () { return _begin == NULL ;}
231231
232+ inline
233+ void refill () {
234+ // NB: at EOF read_proc may return fewer bytes than asked for; the
235+ // tail is zeroed so callers never see stale heap/file bytes in
236+ // decoded pixels (CVE-2024-28581 read-side hardening).
237+ unsigned got = _io->read_proc (_begin, sizeof (BYTE ), (unsigned )_size, _handle);
238+ if (got < _size) {
239+ memset (_begin + got, 0 , _size - got);
240+ }
241+ _ptr = _begin;
242+ }
243+
232244 inline
233245 BYTE getByte () {
234246 if (_ptr >= _end) {
235247 // need refill
236- _ptr = _begin;
237- _io->read_proc (_ptr, sizeof (BYTE ), (unsigned )_size, _handle); // ### EOF - no problem?
248+ refill ();
238249 }
239250
240251 BYTE result = *_ptr;
@@ -243,11 +254,11 @@ class IOCache
243254
244255 return result;
245256 }
246-
257+
247258 inline
248259 BYTE * getBytes (size_t count /* must be < _size!*/ ) {
249260 if (_ptr + count >= _end) {
250-
261+
251262 // need refill
252263
253264 // 'count' bytes might span two cache bounds,
@@ -258,8 +269,7 @@ class IOCache
258269
259270 _io->seek_proc (_handle, -remaining, SEEK_CUR );
260271
261- _ptr = _begin;
262- _io->read_proc (_ptr, sizeof (BYTE ), (unsigned )_size, _handle); // ### EOF - no problem?
272+ refill ();
263273 }
264274
265275 BYTE *result = _ptr;
@@ -524,9 +534,13 @@ _assignPixel<8>(BYTE* bits, BYTE* val, BOOL as24bit) {
524534}
525535
526536template <>
527- inline void
537+ inline void
528538_assignPixel<16 >(BYTE * bits, BYTE * val, BOOL as24bit) {
529- WORD value (*reinterpret_cast <WORD *>(val));
539+ // NB: val points into the RLE byte cache at a file-driven offset and is
540+ // not necessarily 2-byte aligned, so go through memcpy instead of a
541+ // WORD load (CVE-2024-28581: UBSan misaligned-load on crafted files).
542+ WORD value (0 );
543+ memcpy (&value, val, sizeof (value));
530544
531545#ifdef FREEIMAGE_BIGENDIAN
532546 SwapShort (&value);
@@ -558,7 +572,12 @@ _assignPixel<32>(BYTE* bits, BYTE* val, BOOL as24bit) {
558572
559573 } else {
560574#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
561- *(reinterpret_cast <unsigned *>(bits)) = *(reinterpret_cast <unsigned *> (val));
575+ // NB: val is cache-sourced at a file-driven offset and may be
576+ // misaligned (see _assignPixel<16> above); bits stays 4-aligned
577+ // because x always advances by pixel_size == 4 here.
578+ unsigned value (0 );
579+ memcpy (&value, val, sizeof (value));
580+ memcpy (bits, &value, sizeof (value));
562581#else // NOTE This is faster then doing reinterpret_cast to int + INPLACESWAP !
563582 bits[FI_RGBA_BLUE ] = val[0 ];
564583 bits[FI_RGBA_GREEN ] = val[1 ];
@@ -640,6 +659,18 @@ loadRLE(FIBITMAP*& dib, int width, int height, FreeImageIO* io, fi_handle handle
640659 // ...and fill packet_count pixels with it
641660
642661 for (int ix = 0 ; ix < packet_count; ix++) {
662+ // NB: the per-packet dib_end check above assumes a flat
663+ // address model, but rows wrap at line_size while the image
664+ // only has height rows: an over-long packet can wrap past
665+ // the last row (CVE-2024-28581 heap overflow in
666+ // _assignPixel), so stop on row exhaustion here. The check
667+ // sits at the top (not after the wrap) so that a packet
668+ // ending exactly on the last pixel still exits cleanly.
669+ if (y >= height) {
670+ FreeImage_OutputMessageProc (s_format_id, FI_MSG_ERROR_CORRUPTED );
671+ // return what is left from the bitmap
672+ return ;
673+ }
643674 _assignPixel<bPP>((line_bits+x), val, as24bit);
644675 x += pixel_size;
645676
@@ -655,6 +686,12 @@ loadRLE(FIBITMAP*& dib, int width, int height, FreeImageIO* io, fi_handle handle
655686
656687 // copy packet_count pixels from file to dib
657688 for (int ix = 0 ; ix < packet_count; ix++) {
689+ // (same row-exhaustion guard as above)
690+ if (y >= height) {
691+ FreeImage_OutputMessageProc (s_format_id, FI_MSG_ERROR_CORRUPTED );
692+ // return what is left from the bitmap
693+ return ;
694+ }
658695 BYTE *val = cache.getBytes (file_pixel_size);
659696 _assignPixel<bPP>((line_bits+x), val, as24bit);
660697 x += pixel_size;
0 commit comments