Description
I tried this code:
use std::io::{stdin, Read};
fn main() {
let mut input = Vec::new();
stdin().read_to_end(&mut input).unwrap();
// Or:
/*
let mut input = String::new();
stdin().read_to_string(&mut input).unwrap();
*/
println!("{:?}", input);
}
This code can result in the error:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: InvalidInput, message: "Windows stdin in console mode does not support a buffer too small to guarantee holding one arbitrary UTF-8 character (4 bytes)" }', main.rs:5:37
This error is likely to happen when input is read in small chunks (e.g. a few characters per line, when line buffering is used), to slowly fill the string/vec close to its capacity. Currently, it is easily reproduceable by pressing return 15 times on the terminal input.
This error is uncontrollable by me; when I call read_to_end, the actual read calls and the buffer sizes used are entirely controlled by code in std
. I'd expect it to just do the right thing in this case and provide the correct size buffer, and not raise an error that the end user can't easily recover from.
Meta
rustc --version --verbose
:
rustc 1.57.0 (f1edd0429 2021-11-29)
binary: rustc
commit-hash: f1edd0429582dd29cccacaf50fd134b05593bd9c
commit-date: 2021-11-29
host: x86_64-pc-windows-msvc
release: 1.57.0
LLVM version: 13.0.0
Also reproducible on nightly:
rustc 1.59.0-nightly (efec54529 2021-12-04)
binary: rustc
commit-hash: efec545293b9263be9edfb283a7aa66350b3acbf
commit-date: 2021-12-04
host: x86_64-pc-windows-msvc
release: 1.59.0-nightly
LLVM version: 13.0.0
Backtrace
Pretty sure this isn't relevant, as the panic is from an `unwrap()` in my code because of an error value created in `std`. But here you go:
stack backtrace:
0: std::panicking::begin_panic_handler
at /rustc/efec545293b9263be9edfb283a7aa66350b3acbf\/library\std\src\panicking.rs:498
1: core::panicking::panic_fmt
at /rustc/efec545293b9263be9edfb283a7aa66350b3acbf\/library\core\src\panicking.rs:107
2: core::result::unwrap_failed
at /rustc/efec545293b9263be9edfb283a7aa66350b3acbf\/library\core\src\result.rs:1660
3: core::result::Result<T,E>::unwrap
4: core::ptr::const_ptr::<impl *const T>::is_null
5: core::ops::function::FnOnce::call_once