Skip to content

dd: writes null bytes to device if stdout is redirect to device #3542

@jhscheer

Description

@jhscheer

@jfinkels I took the liberty to open a new issue with a summary for all the findings you published in #3447

gnu/tests/dd/skip-seek-past-dev.sh runs

device=$(df --output=source . | tail -n1)
dev_size=$(blockdev --getsize64 $device)
DEV_OFLOW=$(expr $dev_size + 1)
dd bs=1 seek=$DEV_OFLOW count=0 status=noxfer > "$device"

and expects $?=1 and stderr=dd: 'standard output': cannot seek: Invalid argument.

However, uu_tail does not check if stdout is a redirect to a seekable device and then writes null bytes to $device !

let mut dst = io::stdout();
// The --seek and --oseek flags are additive.
let amt = seek.unwrap_or(0) + oseek.unwrap_or(0);
// stdout is not seekable, so we just write null bytes.
if amt > 0 {
io::copy(&mut io::repeat(0u8).take(amt as u64), &mut dst)
.map_err_context(|| String::from("write error"))?;
}

possible fix

Haven't looked into it much but a possible fix could be to get the redirect for /dev/stdout similar to this:

if let Ok(p) = Path::new("/dev/stdin").canonicalize() {
p.into_os_string()
} else {
OsString::from("/dev/stdin")
}

and then apply the seek checks for the new path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions