bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: read fails on null-byte: v4.1.7 FreeBSD 8.0 (amd64)


From: Greg Wooledge
Subject: Re: read fails on null-byte: v4.1.7 FreeBSD 8.0 (amd64)
Date: Wed, 23 Nov 2011 16:41:02 -0500
User-agent: Mutt/1.4.2.3i

On Wed, Nov 23, 2011 at 09:03:33AM -0500, Matthew Story wrote:
>  Description: read terminates reading all records at first null-byte ( chr(0) 
> ) in a stream, null-bytes are valid ascii characters and should not cause 
> read to stop reading
>               a line this behavior is not reproducible using bourne shell.

Actually, read does not stop reading at a NUL byte unless you tell it to
by using -d ''.  It reads until the newline.

> Steps To Reproduce:
> 
> [bash ~]$ printf 'foo\0bar\n' | while read line; do echo "$line"; done
> foo

What happens here is bash reads until the newline, but only the "foo"
part is visible in the variable, because the NUL effective ends the
string.  Whether the "bar" part is copied into memory or not is not
relevant and cannot be determined without poking around inside the bash
process itself.

If bash had actually stopped reading at the NUL, then the "bar" part
would still be sitting there on stdin waiting for the next read.  But
we can see that this is not the case:

$ printf 'foo\0bar\n' | { read line; echo "$line"; read line; echo "$line"; }
foo

$ 

The second read gets nothing.  If we want to see bash stop at the NUL, then
using -d '' will accomplish that:

$ printf 'foo\0bar\n' | { read -d '' line; echo "$line"; read line; echo 
"$line"; }
foo
bar
$ 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]