From bb094dab9ee025c2352bdc458e4e34be4698b2fb Mon Sep 17 00:00:00 2001 From: yory8 <> Date: Sun, 27 Oct 2019 21:53:27 +0100 Subject: [PATCH] fix: stop leaking content of clipboard --- CHANGELOG.md | 4 ++++ main.go | 20 +++++++++++++++++--- storer.go | 5 ++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da0a528..0721ce1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - `--tool-args` argument to pass additional args to dmenu/rofi/etc. - rofi and wofi now display a prompt hint to remind you whether you are picking or clearing +**Notable Bug fixes** + +- we don't leak our clipboard to `ps` anymore + # 1.1 **New features** diff --git a/main.go b/main.go index 3af1862..d7fe05d 100644 --- a/main.go +++ b/main.go @@ -73,7 +73,7 @@ func main() { if selection != "" { // serve selection to the OS - if err := exec.Command("wl-copy", "--", selection).Run(); err != nil { + if err := serveTxt(selection); err != nil { log.Fatal(err) } } @@ -83,7 +83,7 @@ func main() { return } - if err := exec.Command("wl-copy", "--", history[len(history)-1]).Run(); err != nil { + if err := serveTxt(history[len(history)-1]); err != nil { log.Fatal(err) } case "clear": @@ -116,7 +116,7 @@ func main() { if selection == history[len(history)-1] { // wl-copy is still serving the copy, so replace with next latest // note: we alread exited if less than 2 items - if err := exec.Command("wl-copy", "--", history[len(history)-2]).Run(); err != nil { + if err := serveTxt(history[len(history)-2]); err != nil { log.Fatal(err) } } @@ -166,3 +166,17 @@ func getHistory(rawPath string) (string, []string, error) { return histfile, history, nil } + +func serveTxt(s string) error { + bin, err := exec.LookPath("wl-copy") + if err != nil { + return fmt.Errorf("couldn't find wl-copy: %v", err) + } + + cmd := exec.Cmd{Path: bin, Stdin: strings.NewReader(s)} + if err := cmd.Run(); err != nil { + log.Printf("Error running wl-copy: %s", err) // don't abort, minor error + } + + return nil +} diff --git a/storer.go b/storer.go index cb40d94..a01f67d 100644 --- a/storer.go +++ b/storer.go @@ -5,7 +5,6 @@ import ( "fmt" "io/ioutil" "log" - "os/exec" ) func store(text string, history []string, histfile string, max int, persist bool) error { @@ -43,8 +42,8 @@ func store(text string, history []string, histfile string, max int, persist bool // make the copy buffer available to all applications, // even when the source has disappeared if persist { - if err := exec.Command("wl-copy", []string{"--", text}...).Run(); err != nil { - log.Printf("Error running wl-copy: %s", err) // don't abort, minor error + if err := serveTxt(text); err != nil { + log.Print(err) // don't abort, minor error } }