Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14238

Re: When reading less than wanted characters, "read" does not detect NUL bytes

Path csiph.com!goblin2!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail
From Greg Wooledge <wooledg@eeg.ccf.org>
Newsgroups gnu.bash.bug
Subject Re: When reading less than wanted characters, "read" does not detect NUL bytes
Date Fri, 15 Jun 2018 09:07:46 -0400
Lines 18
Approved bug-bash@gnu.org
Message-ID <mailman.1963.1529068105.1292.bug-bash@gnu.org> (permalink)
References <0Mfn88-1fqxyA1P40-00NBdT@mail.gmx.com>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace usenet.stanford.edu 1529068106 7780 208.118.235.17 (15 Jun 2018 13:08:26 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
Mail-Followup-To bug-bash@gnu.org
Content-Disposition inline
In-Reply-To <0Mfn88-1fqxyA1P40-00NBdT@mail.gmx.com>
User-Agent NeoMutt/20170113 (1.7.2)
X-detected-operating-system by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy]
X-Received-From 139.137.100.1
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.21
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash/>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:14238

Show key headers only | View raw


On Fri, Jun 15, 2018 at 03:03:21PM +0200, Davide Brini wrote:
> $ printf 'a\x00\x00bc' | { while IFS= read -d '' -n 2 var; do echo "read: $var, length: ${#var}"; done; }
> read: a, length: 1
> read: , length: 0
> read: bc, length: 2
> 
> I would expect there to be another read of length 0 between the "a" and the
> "bc".

Seems correct to me.  You asked it to stop reading when it finds a NUL
or when it has read 2 characters.  The first time, they both happen at
the same time, and you end up with "a", and two bytes have been consumed.

The second time, it happens after reading the NUL byte, so you get "" and
a total of three bytes have been consumed.

The third time, you read "bc", and all the bytes have been consumed.

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: When reading less than wanted characters, "read" does not detect NUL bytes Greg Wooledge <wooledg@eeg.ccf.org> - 2018-06-15 09:07 -0400

csiph-web