From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5912 invoked by alias); 10 Dec 2015 03:56:46 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 37369 Received: (qmail 21224 invoked from network); 10 Dec 2015 03:56:45 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1449719800; bh=i0ga/kc0TxpkCamexl/htYFRYguCx+IDrhx/6khEmvY=; h=From:To:In-Reply-To:References:Subject:Date; b=gJtnNb5hJpFT6Yj3VHMdJnQFJyZFQEaOPWtOyvQf+gt/jcCoCDZUUc+O9ZeCi6O3a j0TFn1LP62/NKKcWmCWC2up9KOt6s6GxG+4dQF3MXXRHZr8h29V4pL/utaTxKcGMns IB/d8qZb5cN/diuTs/I3R3pL7wHu56xyBTeNN9TM= From: "Nikolay Aleksandrovich Pavlov (ZyX)" To: D Gowers , "zsh-workers@zsh.org" In-Reply-To: References: Subject: Re: expr length "$val" returns the wrong length for values containing NULL (\\0) MIME-Version: 1.0 Message-Id: <2007121449719799@web8h.yandex.ru> X-Mailer: Yamail [ http://yandex.ru ] 5.0 Date: Thu, 10 Dec 2015 06:56:39 +0300 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=utf-8 10.12.2015, 04:52, "D Gowers" : > 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)'