mcookie - fix providing /dev/random or /dev/urandom as file leads to infinite loop (#274)

* Update mcookie.rs

* edit doc

* fmt

* fix for unix and non-unix build

* add test case

* refactor

* fmt

* fix
This commit is contained in:
Quang
2025-03-31 00:54:28 -07:00
committed by GitHub
parent 36b3af044c
commit 0113e301e8
2 changed files with 43 additions and 1 deletions

View File

@@ -3,6 +3,8 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;
use std::{fs::File, io::Read};
use clap::{crate_version, Arg, ArgAction, Command};
@@ -57,7 +59,22 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let mut handle = f.take(*max_bytes);
handle.read_to_end(&mut buffer)?;
} else {
f.read_to_end(&mut buffer)?;
#[cfg(unix)]
{
const DEFAULT_SEED_READ_BYTES: u64 = 1024;
let metadata = f.metadata()?;
if metadata.file_type().is_char_device() {
let mut handle = f.take(DEFAULT_SEED_READ_BYTES);
handle.read_to_end(&mut buffer)?;
} else {
f.read_to_end(&mut buffer)?;
}
}
#[cfg(not(unix))]
{
f.read_to_end(&mut buffer)?;
}
}
if verbose {