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.

git-svn-id: file:///srv/svn/repo/suika/trunk@326 f0ae65fe-ee39-954e-97ec-027ff2717ef4
This commit is contained in:
delthas 2020-06-08 20:30:09 +00:00
parent e853516870
commit 7f49fa7971

View File

@ -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 {