Skip to content

Commit 629c834

Browse files
authored
fix(bmp): validate scanline file position before reading (#5274)
read_native_scanline computed the scanline's file position as m_bmp_header.offset + y * m_padded_scanline_size and seeked there without checking it against the file size. A corrupt header (bogus pixel-data offset or scanline size) can place that position far outside the file. Assisted-by: Claude Code / Claude Opus 4.8 Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent 5d49c3e commit 629c834

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

src/bmp.imageio/bmpinput.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,19 @@ BmpInput::read_native_scanline(int subimage, int miplevel, int y, int /*z*/,
425425
if (m_dib_header.height >= 0)
426426
y = m_spec.height - y - 1;
427427
const int64_t scanline_off = y * m_padded_scanline_size;
428+
const int64_t scanline_pos = int64_t(m_bmp_header.offset) + scanline_off;
429+
430+
// A corrupt header can place the scanline outside the file. The IOProxy
431+
// will reject such a read, but validate the position explicitly so we
432+
// fail cleanly instead of relying on a bad seek being caught downstream.
433+
if (m_padded_scanline_size < 0 || scanline_pos < 0
434+
|| scanline_pos + m_padded_scanline_size > int64_t(ioproxy()->size())) {
435+
errorfmt("Invalid scanline position in BMP file");
436+
return false;
437+
}
428438

429439
fscanline.resize(m_padded_scanline_size);
430-
ioseek(m_bmp_header.offset + scanline_off);
440+
ioseek(scanline_pos);
431441
if (!ioread(fscanline.data(), m_padded_scanline_size)) {
432442
return false; // Read failed
433443
}

0 commit comments

Comments
 (0)