Skip to content

Commit b940993

Browse files
committed
admin: share stdin reader across password prompts
readPassword created a fresh bufio.Reader on every call. bufio reads ahead into its buffer, so the first prompt consumed the password line and buffered the confirmation line, which was then discarded with the reader. The second prompt built a new reader on an already-drained pipe and failed with EOF. This stayed hidden as long as the two lines arrived far enough apart that only the first was available at the initial read. Any startup delay that let both lines queue up first exposed it. Keep a single package-level reader so buffered read-ahead survives between prompts. Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent 67beeaa commit b940993

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

‎cmd/pw/admin/users.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,18 @@ func readLine(prompt string) (string, error) {
213213
return strings.TrimSpace(line), err
214214
}
215215

216+
// stdinReader is shared across readPassword calls so that a single reader's
217+
// buffered read-ahead is not discarded between the password and confirmation
218+
// prompts. A fresh bufio.Reader per call would swallow the second line.
219+
var stdinReader = bufio.NewReader(os.Stdin)
220+
216221
func readPassword(prompt string) (string, error) {
217222
fmt.Fprint(os.Stderr, prompt)
218223
if term.IsTerminal(int(os.Stdin.Fd())) {
219224
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
220225
fmt.Fprintln(os.Stderr)
221226
return string(pw), err
222227
}
223-
reader := bufio.NewReader(os.Stdin)
224-
line, err := reader.ReadString('\n')
228+
line, err := stdinReader.ReadString('\n')
225229
return strings.TrimSpace(line), err
226230
}

‎smoke/_init.sh‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ pw() {
183183

184184
set_password() {
185185
printf '%s\n' "$PASSWORD"
186-
sleep 0.1
187186
printf '%s\n' "$PASSWORD"
188187
}
189188

0 commit comments

Comments
 (0)