From ed943f54518a0764fa1d6e86fdf8524bf8136298 Mon Sep 17 00:00:00 2001 From: delthas Date: Sun, 7 Jun 2020 01:20:56 +0200 Subject: [PATCH] Fix: Check the stdin scanner for errors when reading the password Reading from stdin with Scanner.Scan() can either fail because of a read error, or return no bytes because the EOF was reached. This adds support for checking these cases before actually reading the password. --- cmd/sojuctl/main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/sojuctl/main.go b/cmd/sojuctl/main.go index 368d2d8..d7bcdeb 100644 --- a/cmd/sojuctl/main.go +++ b/cmd/sojuctl/main.go @@ -120,7 +120,12 @@ func readPassword() ([]byte, error) { } else { fmt.Fprintf(os.Stderr, "Warning: Reading password from stdin.\n") scanner := bufio.NewScanner(os.Stdin) - scanner.Scan() + if !scanner.Scan() { + if err := scanner.Err(); err != nil { + log.Fatalf("failed to read password from stdin: %v", err) + } + log.Fatalf("failed to read password from stdin: stdin is empty") + } password = scanner.Bytes() if len(password) == 0 {