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.
This commit is contained in:
delthas 2020-06-07 01:20:56 +02:00 committed by Simon Ser
parent 17fe033adc
commit ed943f5451

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 {