zsh-workers
 help / color / mirror / code / Atom feed
From: D Gowers <finticemo@gmail.com>
To: "Nikolay Aleksandrovich Pavlov (ZyX)" <kp-pav@yandex.ru>
Cc: "zsh-workers@zsh.org" <zsh-workers@zsh.org>
Subject: Re: expr length "$val" returns the wrong length for values containing NULL (\\0)
Date: Thu, 10 Dec 2015 14:48:51 +1030	[thread overview]
Message-ID: <CAMf8R04CsWtB39TVW-0VHhu9dcfPNYyi6gLXi-QhrF8Vp7nRLQ@mail.gmail.com> (raw)
In-Reply-To: <2007121449719799@web8h.yandex.ru>

[-- Attachment #1: Type: text/plain, Size: 2178 bytes --]

Ah, okay. That (commandline arguments not being able to contain NUL)
seems.. a bit anachronistic. But I guess it's never been enough of a
problem to warrant the considerable bother to fix it. Fair enough.

On Thu, Dec 10, 2015 at 2:26 PM, Nikolay Aleksandrovich Pavlov (ZyX) <
kp-pav@yandex.ru> wrote:

>
> 10.12.2015, 04:52, "D Gowers" <finticemo@gmail.com>:
> > Test case:
> >
> > v=$(printf foo\\0bar);expr length "$v";expr length $v
> >
> > alternatively:
> >
> > v=foo$'\0'bar;expr length "$v";expr length $v
> >
> > In zsh, the values returned are 3 and 3.
> > In dash and zsh, the values returned are 6 and 6.
> >
> > Both of those results are wrong, AFAICS (foo$'0'bar is 7 characters
> long).
> > But the zsh result is more severely wrong. I could understand the
> bash/dash
> > result, at least, as 'NULL characters are not counted towards length'.
>
> Both results are *right*. In both cases you ask the length of the string
> and you get it.
>
> In dash (also posh, bash and busybox ash) zero byte is skipped when
> storing. So length of the $v *is* six. You may question whether it is right
> storing without zero byte, but the fact that all four shells have exactly
> the same behaviour makes me think this is part of the POSIX standard. In
> any case non-C strings are not on the list of features of these shells
> unlike zsh (it also internally uses C NUL-terminated strings, but zero
> bytes and some other characters are “metafied” (i.e. escaped) and
> unmetafied when passed to the outer world e.g. by doing `echo $v` to pass
> string to terminal).
>
> As I said in zsh zero byte is stored. But C strings which are the only
> ones that can be arguments to any program are **NUL-terminated**. So what
> you do is passing string "foo" because NUL terminates the string. You
> cannot possibly get the answer you think is right here thus, unless you
> reimplement `expr` as a zsh function.
>
> >
> > In any case, it is easily demonstrated that the string is not 3
> characters
> > long, by running 'echo "$V"' or 'print "$v"' or 'echo ${#v}'
> >
> > `zsh --version` = 'zsh 5.2 (x86_64-unknown-linux-gnu)'
>

  reply	other threads:[~2015-12-10  4:18 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-10  1:52 D Gowers
2015-12-10  3:56 ` Nikolay Aleksandrovich Pavlov (ZyX)
2015-12-10  4:18   ` D Gowers [this message]
2015-12-10  4:29     ` Nikolay Aleksandrovich Pavlov (ZyX)
2015-12-10  5:00       ` D Gowers
2015-12-10  9:37         ` Peter Stephenson
2015-12-10 17:47           ` Nikolay Aleksandrovich Pavlov (ZyX)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMf8R04CsWtB39TVW-0VHhu9dcfPNYyi6gLXi-QhrF8Vp7nRLQ@mail.gmail.com \
    --to=finticemo@gmail.com \
    --cc=kp-pav@yandex.ru \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).