Configuration Information [Automatically generated, do not change]: Machine: i686 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' -DC$ uname output: Linux lalgec 3.0.0-16-generic-pae #29-Ubuntu SMP Tue Feb 14 13:56:31 UTC 2012 i686 i686 i386 GNU/Linux Machine Type: i686-pc-linux-gnu
Bash Version: 4.2
Patch Level: 10
Release Status: release
Description:
Hi. I think there is a bug when using $RANDOM in combination with here document
or here string. When using here-doc/str one random number is consumed for
generating filename for temporary file used in the redirection. Probably the
bug also shows up always when temporay files are created with sh_mktmpfd().
If random seed is also set (e.g. RANDOM=42) and then different combinations are
repeatedly tried: without redirection, here-doc, ..., different random
sequences are obtained. In the case of here-doc/str the sequence is shifted one
place left. Obviously one random number is consumed.
It think this is more a bug than a feature. However, you decide. See attached
script random.sh which explores different cases.
Fix.
1) In lib/sh/tmpfile.c in function sh_mktmpfd() replace get_random_number()
with C's random(). Probably the same should be done in sh_mktmpname(). This fix
is sufficient to fix the described bug. And also tested on Mac OS an Linux.
2) Another suggestion. Why not use pipes instead of temporay files? In redir.c
a function here_document_to_fd() could be changed to something similar as:
static int
here_document_to_fd (redirectee, ri)
WORD_DESC *redirectee;
enum r_instruction ri;
{
int r, fds[2];
errno = r = 0; /* XXX */
if (pipe(fds) < 0)
return -1;
/* write_here_document returns 0 on success, errno on failure. */
if (redirectee->word)
r = (ri != r_reading_string) ? write_here_document (fds[1], redirectee)
: write_here_string (fds[1], redirectee);
close (fds[1]);
if (r)
{
close (fds[0]);
return (-1);
}
return (fds[0]);
}
This fixes the bug and also does not use temporay files which sometimes are not
cleaned properly (tested on Linux), e.g. cat <<< $(sleep 42) and press Ctrl+C.
Regards,
Jurij Mihelič
random.sh
Description: Binary data
