mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 06:35:15 +00:00
Fix crash with damaged images in set-window-logo/set-background-image
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run
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
This commit is contained in:
parent
aef2bc2306
commit
8e4780f8ec
3 changed files with 121 additions and 15 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
110
tools/cmd/at/set_window_logo_test.go
Normal file
110
tools/cmd/at/set_window_logo_test.go
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
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")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue