From 8e4780f8ec0953205c154bb7fac9bc78983963d3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 21 Oct 2026 22:38:43 +0530 Subject: [PATCH] Fix crash with damaged images in set-window-logo/set-background-image image.Decode failures fell through to encoding a nil image, causing a panic. Read the image into memory instead of streaming from an open file, which also fixes file descriptors leaking on the DecodeConfig error path and for PNG images, which were never closed. Include the path in decode errors and drop the wasteful raw RGBA sized buffer preallocation. Closes #10715 --- docs/changelog.rst | 3 + tools/cmd/at/set_window_logo.go | 23 ++---- tools/cmd/at/set_window_logo_test.go | 110 +++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 15 deletions(-) create mode 100644 tools/cmd/at/set_window_logo_test.go diff --git a/docs/changelog.rst b/docs/changelog.rst index e9bb5e306..b177f54a2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -231,6 +231,9 @@ Detailed list of changes 0.50.0 [future] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- Remote control: Fix a crash when using damaged images with + :code:`set-window-logo` or :code:`set-background-image` (:pull:`10715`) + - Transfer kitten: When receiving files, replace existing symbolic and hard links at the destination instead of overwriting the files they point to. Files are now written to a temporary file and renamed into place, so an diff --git a/tools/cmd/at/set_window_logo.go b/tools/cmd/at/set_window_logo.go index a542895e1..047a71f4a 100644 --- a/tools/cmd/at/set_window_logo.go +++ b/tools/cmd/at/set_window_logo.go @@ -28,30 +28,23 @@ func read_window_logo(io_data *rc_io_data, path string) (func(io_data *rc_io_dat }, nil } - f, err := os.Open(path) + data, err := os.ReadFile(path) if err != nil { return nil, err } - var image_data_stream io.Reader - image_data_stream = f - config, format, ierr := image.DecodeConfig(f) - if ierr != nil { + _, format, err := image.DecodeConfig(bytes.NewReader(data)) + if err != nil { return nil, fmt.Errorf("%s is not a supported image format", path) } - f.Seek(0, 0) - + var image_data_stream io.Reader = bytes.NewReader(data) if format != "png" { - f.Seek(0, 0) - img, _, err := image.Decode(f) + img, _, err := image.Decode(bytes.NewReader(data)) if err != nil { - f.Close() + return nil, fmt.Errorf("failed to decode the image %s: %w", path, err) } - f.Close() b := bytes.Buffer{} - b.Grow(config.Height * config.Width * 4) - err = images.Encode(&b, img, "image/png") - if err != nil { - return nil, err + if err = images.Encode(&b, img, "image/png"); err != nil { + return nil, fmt.Errorf("failed to convert the image %s to PNG: %w", path, err) } image_data_stream = &b } diff --git a/tools/cmd/at/set_window_logo_test.go b/tools/cmd/at/set_window_logo_test.go new file mode 100644 index 000000000..040281d35 --- /dev/null +++ b/tools/cmd/at/set_window_logo_test.go @@ -0,0 +1,110 @@ +// License: GPLv3 Copyright: 2026, Kovid Goyal, + +package at + +import ( + "bytes" + "encoding/base64" + "encoding/binary" + "image" + "image/jpeg" + "image/png" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/kovidgoyal/kitty/tools/utils" +) + +// Truncate a JPEG immediately after its SOS segment header so that the +// header (and thus image.DecodeConfig) is valid but there is no image data. +func truncate_jpeg_before_scan_data(t *testing.T, data []byte) []byte { + idx := bytes.Index(data, []byte{0xff, 0xda}) + if idx < 0 || idx+4 > len(data) { + t.Fatal("Could not find SOS marker in JPEG data") + } + end := idx + 2 + int(binary.BigEndian.Uint16(data[idx+2:])) + if end > len(data) { + t.Fatal("Invalid SOS segment length in JPEG data") + } + return data[:end] +} + +func TestReadRemoteImage(t *testing.T) { + type payload struct{ Data string } + src := image.NewRGBA(image.Rect(0, 0, 4, 4)) + var jpeg_data, png_data bytes.Buffer + if err := jpeg.Encode(&jpeg_data, src, nil); err != nil { + t.Fatal(err) + } + if err := png.Encode(&png_data, src); err != nil { + t.Fatal(err) + } + truncated := truncate_jpeg_before_scan_data(t, jpeg_data.Bytes()) + if _, _, err := image.DecodeConfig(bytes.NewReader(truncated)); err != nil { + t.Fatalf("Truncated JPEG does not have a valid header: %s", err) + } + tdir := t.TempDir() + + for _, tc := range []struct { + name string + data []byte + expected_err string + }{ + {"truncated_jpeg", truncated, "failed to decode"}, + {"not_an_image", []byte("not an image"), "not a supported image format"}, + {"jpeg", jpeg_data.Bytes(), ""}, + {"png", png_data.Bytes(), ""}, + } { + path := filepath.Join(tdir, tc.name) + if err := os.WriteFile(path, tc.data, 0o600); err != nil { + t.Fatal(err) + } + for _, reader := range []struct { + name string + read func(*rc_io_data, string) (func(*rc_io_data) (bool, error), error) + }{ + {"window_logo", read_window_logo}, + {"background_image", read_background_image}, + } { + t.Run(reader.name+"/"+tc.name, func(t *testing.T) { + io_data := rc_io_data{rc: &utils.RemoteControlCmd{Stream: true, Payload: payload{}}} + next, err := reader.read(&io_data, path) + if tc.expected_err != "" { + if err == nil || next != nil { + t.Fatalf("Expected an error and no image stream, got error: %v", err) + } + if !strings.Contains(err.Error(), tc.expected_err) || !strings.Contains(err.Error(), path) { + t.Fatalf("Error message %q does not contain %q and the path", err, tc.expected_err) + } + return + } + if err != nil { + t.Fatal(err) + } + var received []byte + for done := false; !done; { + if done, err = next(&io_data); err != nil { + t.Fatal(err) + } + chunk, err := base64.StdEncoding.DecodeString(io_data.rc.Payload.(payload).Data) + if err != nil { + t.Fatal(err) + } + received = append(received, chunk...) + } + img, format, err := image.Decode(bytes.NewReader(received)) + if err != nil { + t.Fatal(err) + } + if format != "png" || img.Bounds() != src.Bounds() { + t.Fatalf("Incorrect image stream: format=%s, bounds=%v", format, img.Bounds()) + } + if tc.name == "png" && !bytes.Equal(received, tc.data) { + t.Fatal("PNG data was not sent unmodified") + } + }) + } + } +}