From 3723c447aaacd542f2f588b1e5e2f08a52c3d754 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 26 Oct 2026 08:48:06 +0530 Subject: [PATCH] clipboard: Reject duplicate stream outputs for the same MIME type Stream outputs write data as it arrives, so with fan-out to all outputs sharing a remote MIME type, two outputs to the same stream would interleave their data chunk by chunk, or encode two images into the stream concurrently. Reject such duplicates with an error instead. Also add a changelog entry for saving all outputs sharing a MIME type. --- docs/changelog.rst | 4 ++++ kittens/clipboard/read.go | 16 ++++++++++++--- kittens/clipboard/read_test.go | 36 +++++++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 2ce2291ca..834e67786 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -389,6 +389,10 @@ Detailed list of changes - Clipboard kitten: Fix saving the list of available MIME types to a file with :code:`kitten clipboard -g -m . formats.txt` silently doing nothing +- Clipboard kitten: Fix only the last of several outputs that use the same + clipboard MIME type being saved, for example with + :code:`kitten clipboard -g picture.png picture.jpg` (:pull:`10744`) + - Kittens: When saving files, such as themes, config files or clipboard output, write to the target of a symlink even when the target does not yet exist, instead of replacing the symlink with a regular file (:pull:`10740`) diff --git a/kittens/clipboard/read.go b/kittens/clipboard/read.go index 2da90f929..ba0a3787b 100644 --- a/kittens/clipboard/read.go +++ b/kittens/clipboard/read.go @@ -281,8 +281,18 @@ func parse_aliases(raw []string) (map[string][]string, error) { type read_outputs map[string][]*Output -func (self read_outputs) add(o *Output) { +func (self read_outputs) add(o *Output) error { + if o.arg_is_stream { + // Stream outputs write data as it arrives, so two of them sharing a + // remote MIME type would interleave their data in the same stream. + for _, x := range self[o.remote_mime_type] { + if x.arg_is_stream && x.arg == o.arg { + return fmt.Errorf("Cannot write the clipboard MIME type %s to %s more than once", o.remote_mime_type, o.arg) + } + } + } self[o.remote_mime_type] = append(self[o.remote_mime_type], o) + return nil } func (self read_outputs) add_data(mime_type string, data []byte) { @@ -382,8 +392,8 @@ func run_get_loop(opts *Options, args []string) (err error) { } if o.remote_mime_type == "." { o.write_mime_list(available_mimes) - } else { - requested_mimes.add(o) + } else if err = requested_mimes.add(o); err != nil { + return err } } if len(requested_mimes) > 0 { diff --git a/kittens/clipboard/read_test.go b/kittens/clipboard/read_test.go index 43d206f63..ab8cbb0e0 100644 --- a/kittens/clipboard/read_test.go +++ b/kittens/clipboard/read_test.go @@ -52,7 +52,9 @@ func TestClipboardReadMultipleOutputs(t *testing.T) { t.Fatal(err) } outputs = append(outputs, o) - requested.add(o) + if err := requested.add(o); err != nil { + t.Fatal(err) + } } if len(requested) != len(tc.available_mimes) { t.Fatalf("Expected one request per remote MIME type, got %d", len(requested)) @@ -117,8 +119,11 @@ func TestClipboardReadOutputFailure(t *testing.T) { defer failed.cleanup() defer succeeded.cleanup() requested := make(read_outputs) - requested.add(failed) - requested.add(succeeded) + for _, o := range []*Output{failed, succeeded} { + if err := requested.add(o); err != nil { + t.Fatal(err) + } + } requested.add_data("text/plain", []byte("clipboard contents")) var wg sync.WaitGroup requested.commit("text/plain", &wg) @@ -134,6 +139,31 @@ func TestClipboardReadOutputFailure(t *testing.T) { } } +func TestClipboardReadDuplicateStreamOutputs(t *testing.T) { + stream := func(arg, remote_mime_type string) *Output { + return &Output{arg: arg, arg_is_stream: true, remote_mime_type: remote_mime_type} + } + requested := make(read_outputs) + for _, o := range []*Output{ + stream("/dev/stdout", "text/plain"), + stream("/dev/stderr", "text/plain"), + stream("/dev/stdout", "text/html"), + {arg: filepath.Join(t.TempDir(), "output"), remote_mime_type: "text/plain"}, + } { + if err := requested.add(o); err != nil { + t.Fatal(err) + } + } + for _, o := range []*Output{stream("/dev/stdout", "text/plain"), stream("/dev/stderr", "text/plain")} { + if err := requested.add(o); err == nil { + t.Fatalf("Duplicate stream output to %s for %s was not rejected", o.arg, o.remote_mime_type) + } + } + if len(requested["text/plain"]) != 3 || len(requested["text/html"]) != 1 { + t.Fatalf("Rejected outputs were added: %d %d", len(requested["text/plain"]), len(requested["text/html"])) + } +} + func TestClipboardOutputCommitAtomic(t *testing.T) { dest := filepath.Join(t.TempDir(), "output") old_data := bytes.Repeat([]byte("old contents\n"), 1024)