diff --git a/tests/unit/test_jpeg.py b/tests/unit/test_jpeg.py index 06bf29a..227e638 100644 --- a/tests/unit/test_jpeg.py +++ b/tests/unit/test_jpeg.py @@ -66,8 +66,21 @@ def raster(width: int = 64, height: int = 64, stride: int | None = None) -> tupl return bytes(out), stride -def address_of(pixels: bytes) -> int: - return ctypes.addressof(ctypes.create_string_buffer(pixels, len(pixels))) +class Frame: + """Pixels in a ctypes buffer that outlives the address taken from it. + + `ctypes.addressof(ctypes.create_string_buffer(...))` is the obvious one-liner + and it is a use-after-free: the buffer is temporary, so the address is + dangling the moment the call returns. Locally the freed memory still happened + to hold the pixels and the tests passed; inside the build sandbox the + allocator reused it and the encoder was handed whatever was there -- which is + the same class of bug this file's docstring warns about, so it should not be + the way these tests fail either. + """ + + def __init__(self, pixels: bytes) -> None: + self.buffer = ctypes.create_string_buffer(pixels, len(pixels)) + self.address = ctypes.addressof(self.buffer) def centre_colour(pixels: bytes, stride: int, x: int, y: int) -> tuple[int, int, int]: @@ -80,7 +93,8 @@ def test_a_frame_round_trips_with_its_channels_in_the_right_place(): enc = TurboJPEG() try: pixels, stride = raster() - encoded = enc.compress(address_of(pixels), 64, 64, stride) + frame = Frame(pixels) + encoded = enc.compress(frame.address, 64, 64, stride) width, height, decoded = enc.decompress(encoded) assert (width, height) == (64, 64) for column, row in ((0, 0), (1, 0), (2, 0), (0, 1), (3, 3)): @@ -105,7 +119,8 @@ def test_a_padded_stride_is_honoured(): try: pixels, stride = raster(64, 64, stride=64 * 4 + 32) assert stride != 64 * 4 - encoded = enc.compress(address_of(pixels), 64, 64, stride) + frame = Frame(pixels) + encoded = enc.compress(frame.address, 64, 64, stride) _, _, decoded = enc.decompress(encoded) got = centre_colour(decoded, 64 * 4, 8, 8) assert got == pytest.approx(as_rgb(colour_at(8, 8, 64)), abs=12), got @@ -117,8 +132,9 @@ def test_quality_trades_bytes_for_fidelity(): enc = TurboJPEG() try: pixels, stride = raster() - small = enc.compress(address_of(pixels), 64, 64, stride, quality=20) - large = enc.compress(address_of(pixels), 64, 64, stride, quality=95) + frame = Frame(pixels) + small = enc.compress(frame.address, 64, 64, stride, quality=20) + large = enc.compress(frame.address, 64, 64, stride, quality=95) assert len(small) < len(large) finally: enc.close() @@ -128,7 +144,8 @@ def test_the_output_is_really_a_jpeg(): enc = TurboJPEG() try: pixels, stride = raster() - encoded = enc.compress(address_of(pixels), 64, 64, stride) + frame = Frame(pixels) + encoded = enc.compress(frame.address, 64, 64, stride) assert encoded.startswith(b"\xff\xd8"), "missing SOI marker" assert b"\xff\xc0" in encoded, "missing SOF0 marker" assert encoded.endswith(b"\xff\xd9"), "missing EOI marker" @@ -147,8 +164,9 @@ def test_an_impossible_quality_is_refused(quality): enc = TurboJPEG() try: pixels, stride = raster() + frame = Frame(pixels) with pytest.raises(ValueError, match="1..100"): - enc.compress(address_of(pixels), 64, 64, stride, quality=quality) + enc.compress(frame.address, 64, 64, stride, quality=quality) finally: enc.close()