Skip to content

Commit ef98b35

Browse files
radarherewiredfool
andauthored
Fix buffer overflow when saving compressed DDS images (#9041)
Co-authored-by: Eric Soroos <eric-github@soroos.net>
1 parent e783aff commit ef98b35

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Tests/test_file_dds.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,20 @@ def test_save_dx10_bc5(tmp_path: Path) -> None:
511511
im = hopper("L")
512512
with pytest.raises(OSError, match="only RGB mode can be written as BC5"):
513513
im.save(out, pixel_format="BC5")
514+
515+
516+
@pytest.mark.parametrize(
517+
"pixel_format, mode",
518+
(
519+
("DXT1", "RGBA"),
520+
("DXT3", "RGBA"),
521+
("DXT5", "RGBA"),
522+
("BC2", "RGBA"),
523+
("BC3", "RGBA"),
524+
("BC5", "RGB"),
525+
),
526+
)
527+
def test_save_large_file(tmp_path: Path, pixel_format: str, mode: str) -> None:
528+
im = hopper(mode).resize((440, 440))
529+
# should not error in valgrind
530+
im.save(tmp_path / "img.dds", pixel_format=pixel_format)

src/libImaging/BcnEncode.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ ImagingBcnEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
258258
UINT8 *dst = buf;
259259

260260
for (;;) {
261+
// Loop writes a max of 16 bytes per iteration
262+
if (dst + 16 >= bytes + buf) {
263+
break;
264+
}
261265
if (n == 5) {
262266
encode_bc3_alpha(im, state, dst, 0);
263267
dst += 8;

0 commit comments

Comments
 (0)