kitty/tools/cmd/at/set_window_logo.go
Kovid Goyal 8e4780f8ec
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
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
2026-10-21 22:38:43 +05:30

102 lines
2.3 KiB
Go

// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package at
import (
"bytes"
"fmt"
"image"
"io"
"os"
"strings"
"github.com/emmansun/base64"
"github.com/kovidgoyal/kitty/tools/utils/images"
)
func set_payload_data(io_data *rc_io_data, data string) {
set_payload_string_field(io_data, "Data", data)
}
func read_window_logo(io_data *rc_io_data, path string) (func(io_data *rc_io_data) (bool, error), error) {
if strings.ToLower(path) == "none" {
io_data.rc.Stream = false
return func(io_data *rc_io_data) (bool, error) {
set_payload_data(io_data, "-")
return true, nil
}, nil
}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
_, format, err := image.DecodeConfig(bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("%s is not a supported image format", path)
}
var image_data_stream io.Reader = bytes.NewReader(data)
if format != "png" {
img, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("failed to decode the image %s: %w", path, err)
}
b := bytes.Buffer{}
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
}
is_first_call := true
buf := make([]byte, 2048)
return func(io_data *rc_io_data) (bool, error) {
if is_first_call {
is_first_call = false
} else {
io_data.rc.Stream = false
}
buf = buf[:cap(buf)]
n, err := image_data_stream.Read(buf)
if err != nil && err != io.EOF {
return false, err
}
buf = buf[:n]
set_payload_data(io_data, base64.StdEncoding.EncodeToString(buf))
if err == io.EOF {
return true, nil
}
return false, nil
}, nil
}
func is_index_arg(s string) bool {
if len(s) == 0 {
return false
}
start := 0
if s[0] == '+' || s[0] == '-' {
start = 1
}
if start >= len(s) {
return false
}
for _, c := range s[start:] {
if c < '0' || c > '9' {
return false
}
}
return true
}
func read_background_image(io_data *rc_io_data, path string) (func(io_data *rc_io_data) (bool, error), error) {
if is_index_arg(path) {
io_data.rc.Stream = false
return func(io_data *rc_io_data) (bool, error) {
set_payload_data(io_data, "index:"+path)
return true, nil
}, nil
}
return read_window_logo(io_data, path)
}