Subject: Re: bug#8103: NUL terminated lines

From: Eric Blake <[email-masked]>
Date: Fri, 25 Feb 2011 13:45:28 -0700

On 02/25/2011 01:09 PM, Jim Meyering wrote:
>> Actually the shell does.  $'\0' and '' are indistinguishable once passed
>> as argument.
> 
> I thought so, too.  Then I tried this:
> 
>     $ zsh  -c "printf \$'\0'|wc -c"
>     1
>     $ bash -c "printf \$'\0'|wc -c"
>     0
> 
> The former prints a NUL byte.
> The latter prints nothing.
> 
> So it depends on your shell.

zsh does some magic to preserve NUL bytes _when passed as arguments to
shell builtins_.  In this case, printf is a zsh shell builtin,
therefore, zsh's $'\0' is able to preserve the fact that you passed in a
NUL byte, then printf outputs that NUL byte again, such that wc sees it
on stdin.

But zsh is special in that regards, and it violates the upcoming POSIX
wording that will be standardized regarding $'' behavior (which means
zsh will have to add yet another shell option soon, if it hasn't
already, to choose between posix and zsh behavior regarding NUL):

http://austingroupbugs.net/view.php?id=249

        If a \xXX or \XXX escape sequence yields a byte whose value
        is 0, that byte and any following regular characters and
        escape sequences up to the terminating unescaped single-quote
        shall be evaluated and discarded.

Bash, on the other hand, strips NUL bytes everywhere.  And zsh _can't_
preserve NUL bytes to non-builtins.

[As a side note, printf '\0' portably prints a NUL byte, but that's
because you're passing two bytes on input which get interpreted as an
escape sequence, and not passing a literal NUL byte as in zsh printf $'\0']

-- 
Eric Blake   [email-masked]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org