Subject: bug#64540: [PATCH] od: exit out on failure to write to stdout

From: Paul Eggert <[email-masked]>
Date: Sun, 9 Jul 2023 12:11:43 -0700

On 2023-07-09 07:11, Pádraig Brady wrote:

> Note the patch looks wrong as it would close the input always.
> We can fix that up easily enough anyway.

If it's easy and doesn't hurt performance in the usual case, that's of 
course fine.

> For my reference a short list of utils to check (that might inf loop) are:
>    cat comm cp cut dd dirname expand factor fmt fold head join ls nl numfmt
>    od paste pr printf ptx readlink realpath seq shuf sort stat tac tail
>    tee tr tsort unexpand uniq wc yes

There's no point to complicating programs that have bounded input for 
other reasons, as they'll report errors soon enough for those other 
reasons. The main problem comes from programs like 'cat' and 'od' that 
can read from "infinite" input like /dev/urandom and can write indefinitely.

So I think we needn't worry about dirname, ptx, readlink, realpath, 
sort, stat, tac, tsort, wc. For example, we needn't worry about 'sort' 
because it needs to read all its input before outputting anything, and 
it cannot read all of /dev/urandom. As long as these programs eventually 
check for write errors, which I expect they all do, that should be good 
enough.

Even for programs like 'od' there is no need to check every output 
function; just check occasionally, when it's convenient.