Ensure the buffer used in `prompt` is zeroized even on error.

Co-authored-by: Jack Grigg <str4d@electriccoin.co>
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
This commit is contained in:
Daira Hopwood 2022-03-11 18:21:11 +00:00
parent 7a98644ff2
commit d5b6e226f0
1 changed files with 9 additions and 5 deletions

View File

@ -496,12 +496,16 @@ fn prompt(input: &mut Stdin) -> anyhow::Result<SecretString> {
let res = input
.read_line(&mut buf)
.with_context(|| "Error reading from stdin");
if !buf.ends_with('\n') {
return Err(WalletToolError::UnexpectedEof.into());
}
// TODO: Ensure the buffer is zeroized even on error.
// Ensure the buffer is zeroized even on error.
let line = SecretString::new(buf);
res.map(|_| line)
res.and_then(|_| {
if line.expose_secret().ends_with('\n') {
Ok(line)
} else {
Err(WalletToolError::UnexpectedEof.into())
}
})
}
fn strip(input: &SecretString) -> &str {