diff --git a/CHANGELOG.md b/CHANGELOG.md index a0adecac4af..8597de88693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel ## Unreleased +- Images: a damaged JPEG whose decode stops before every pixel is written is refused as undecodable, where it could be accepted with rows the decoder never wrote; clean JPEGs decode to the same bytes as before (#2759). Thanks to a community tester who reported it privately. + - CUDA: LightOnOCR-3 0.8B and 4B serve image chat on a GB10 from the publisher's BF16 weights. A plain BF16 Qwen3.5 checkpoint with a tied head now loads, and one with an image tower and no Clef joint head opens as chat instead of the decision-only refusal. The flags it is not qualified on (`--drafter`, `--slide`, `--kv-cache fp8`, `--prompt-precision int8`) and towers the CUDA tower cannot read are refused by name before the weights load. MLX, EXL3, ModelOpt and Clef checkpoints load and answer as before (#PR, part of #1927). Thanks to @laudney. - CUDA: LightOnOCR-3 0.8B and 4B serve image chat on a GB10 from the publisher's BF16 weights. A plain BF16 Qwen3.5 checkpoint with a tied head now loads, and one with an image tower and no Clef joint head opens as chat instead of the decision-only refusal. The flags it is not qualified on (`--drafter`, `--slide`, `--kv-cache fp8`, `--prompt-precision int8`) and towers the CUDA tower cannot read are refused by name before the weights load. MLX, EXL3, ModelOpt and Clef checkpoints load and answer as before (#2597, part of #1927). Thanks to @laudney. - GLM-5.3-Flash on CUDA: the router's eight K slices run on the shared mma tiles (`common/router_mma.cuh`), so Flash Next and GLM share one router template; every partial keeps its bytes, and replies, prompt and decode speed at TP=2 are unchanged. The unaligned copy (`tf_glm_router_part`) is gone, and `glue.router` refuses unaligned rows, which no caller passes (#2557, #2535). Thanks to @laudney. diff --git a/zig/src/core/picture/jpeg.zig b/zig/src/core/picture/jpeg.zig index 604ad573e72..d4771030605 100644 --- a/zig/src/core/picture/jpeg.zig +++ b/zig/src/core/picture/jpeg.zig @@ -171,8 +171,13 @@ pub fn decodeWithLimits(io: std.Io, a: Allocator, data: []const u8, limits: pict const rgb = try a.alloc(u8, w * ht * 3); errdefer a.free(rgb); const gray = m.components == 1; - const rc = f.tj3Decompress8(h, data.ptr, data.len, rgb.ptr, 0, if (gray) pf_gray else pf_rgb); - if (rc != 0 and f.tj3GetErrorCode(h) != err_warning) return error.Invalid; + const pf: c_int = if (gray) pf_gray else pf_rgb; + const out = rgb[0 .. w * ht * @as(usize, if (gray) 1 else 3)]; + @memset(out, 0x00); + if (f.tj3Decompress8(h, data.ptr, data.len, out.ptr, 0, pf) != 0) { + if (f.tj3GetErrorCode(h) != err_warning) return error.Invalid; + try wroteAll(f, h, a, data, out, pf); + } if (gray) { // Pillow's L to RGB: each value three times, widened in place from the end var i = w * ht; while (i > 0) { @@ -185,6 +190,15 @@ pub fn decodeWithLimits(io: std.Io, a: Allocator, data: []const u8, limits: pict return .{ .width = w, .height = ht, .channels = 3, .data = rgb, .orientation = o }; } +/// A warned decode (TurboJPEG calls a fatal error after a warning a warning) is refused if it left any byte unwritten. +fn wroteAll(f: Fns, h: ?*anyopaque, a: Allocator, data: []const u8, out: []const u8, pf: c_int) picture.Error!void { + const again = try a.alloc(u8, out.len); + defer a.free(again); + @memset(again, 0xFF); + if (f.tj3Decompress8(h, data.ptr, data.len, again.ptr, 0, pf) != 0 and f.tj3GetErrorCode(h) != err_warning) return error.Invalid; + if (!std.mem.eql(u8, out, again)) return error.Invalid; +} + /// Pillow's open: markers up to the first scan (EXIF, XMP, JFIF density, the frame), then on to the end marker. fn walk(a: Allocator, data: []const u8, m: *Markers) picture.Error!void { var at: usize = 2; diff --git a/zig/src/core/picture/picture_test.zig b/zig/src/core/picture/picture_test.zig index 5d66de94e52..47fe7bcde1d 100644 --- a/zig/src/core/picture/picture_test.zig +++ b/zig/src/core/picture/picture_test.zig @@ -120,6 +120,69 @@ test "edge pictures (zig/tests/fixtures/picture/edges.json) get 0.6.6's outcome, if (!jpeg) return error.SkipZigTest; } +/// Small JPEGs built by hand: 8-bit, one quantization table of ones, one-code Huffman tables (DC 0, then end of block). +const crafted = struct { + const soi = "\xff\xd8"; + const eoi = "\xff\xd9"; + const junk = "\x00"; // a stray byte before a marker: libjpeg's "extraneous bytes" warning + const dqt = "\xff\xdb\x00\x43\x00" ++ @as([64]u8, @splat(1)); + const dht = "\xff\xc4\x00\x14\x00\x01" ++ @as([15]u8, @splat(0)) ++ "\x00" ++ "\xff\xc4\x00\x14\x10\x01" ++ @as([15]u8, @splat(0)) ++ "\x00"; + const gray8 = "\xff\xc0\x00\x0b\x08\x00\x08\x00\x08\x01\x01\x11\x00"; + const gray16 = "\xff\xc0\x00\x0b\x08\x00\x10\x00\x10\x01\x01\x11\x00"; + const progressive16 = "\xff\xc2\x00\x0b\x08\x00\x10\x00\x10\x01\x01\x11\x00"; + const rgb8 = "\xff\xc0\x00\x11\x08\x00\x08\x00\x08\x03\x01\x11\x00\x02\x11\x00\x03\x11\x00"; + const scan_gray = "\xff\xda\x00\x08\x01\x01\x00\x00\x3f\x00"; + const scan_dc = "\xff\xda\x00\x08\x01\x01\x00\x00\x00\x00"; // a progressive first scan: DC only + const scan_rgb = "\xff\xda\x00\x0c\x03\x01\x00\x02\x00\x03\x00\x00\x3f\x00"; + + /// libjpeg stops on a fatal error after a warning, and TurboJPEG reports it as a warning: no pixel is written. + const unfinished = [_][]const u8{ + soi ++ gray8 ++ junk ++ scan_gray ++ eoi, // no quantization table, refused only as the pixels start + soi ++ rgb8 ++ junk ++ scan_rgb ++ eoi, + soi ++ dqt ++ progressive16 ++ dht ++ scan_dc ++ gray16 ++ eoi, // the scan ends at once at a second frame + soi ++ dqt ++ junk ++ progressive16 ++ dht ++ scan_dc ++ "\x0f" ++ gray16 ++ eoi, + }; + + /// Warned, but libjpeg finishes every row: these still decode, all mid-gray (each block's DC is 0). + const finished = [_][]const u8{ + soi ++ dqt ++ junk ++ gray16 ++ dht ++ scan_gray ++ "\x00" ++ eoi, + soi ++ dqt ++ gray16 ++ dht ++ scan_gray ++ eoi, // the scan is empty: its blocks read as zeros + soi ++ dqt ++ gray16 ++ dht ++ scan_gray ++ gray16 ++ eoi, // a second frame only after the last row + soi ++ dqt ++ gray16 ++ dht ++ "\xff\xdd\x00\x04\x00\x01" ++ scan_gray ++ "\x00\xff\xd3\x00" ++ eoi, // a wrong restart + }; +}; + +test "a JPEG libjpeg gives up on after a warning is refused, never returned with pixels it did not write" { + const a = std.testing.allocator; + if (!try jpegReady(a)) return error.SkipZigTest; + var decoded: usize = 0; + for (crafted.unfinished, 0..) |bytes, i| { + var rgb = picture.decode(std.testing.io, a, bytes) catch |err| { + try std.testing.expectEqual(error.Invalid, err); + continue; + }; + defer rgb.deinit(a); + // a safe build paints new memory 0xaa: the bytes the decoder left as it found them + const painted = std.mem.count(u8, rgb.pixels, "\xaa"); + std.debug.print("case {d}: {d}x{d} decoded, {d} of {d} bytes unwritten\n", .{ i, rgb.width, rgb.height, painted, rgb.pixels.len }); + decoded += 1; + } + try std.testing.expectEqual(@as(usize, 0), decoded); +} + +test "warned JPEGs libjpeg finishes still decode, every pixel written" { + const a = std.testing.allocator; + if (!try jpegReady(a)) return error.SkipZigTest; + for (crafted.finished, 0..) |bytes, i| { + errdefer std.debug.print("case {d}\n", .{i}); + var rgb = try picture.decode(std.testing.io, a, bytes); + defer rgb.deinit(a); + try std.testing.expectEqual(@as(usize, 16), rgb.width); + try std.testing.expectEqual(@as(usize, 16), rgb.height); + for (rgb.pixels) |v| try std.testing.expectEqual(@as(u8, 128), v); + } +} + test "PNG tRNS keys compare their low bytes, except mode 1 keeps the whole key" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit();