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


Groups > gnu.bash.bug > #14237 > unrolled thread

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

Started byDavide Brini <dave_br@gmx.com>
First post2018-06-15 15:03 +0200
Last post2018-06-15 15:03 +0200
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug


Contents

  When reading less than wanted characters, "read" does not detect NUL bytes Davide Brini <dave_br@gmx.com> - 2018-06-15 15:03 +0200

#14237 — When reading less than wanted characters, "read" does not detect NUL bytes

FromDavide Brini <dave_br@gmx.com>
Date2018-06-15 15:03 +0200
SubjectWhen reading less than wanted characters, "read" does not detect NUL bytes
Message-ID<mailman.1962.1529067816.1292.bug-bash@gnu.org>
Best explained with an example:

$ printf 'a\x00\x00bc' | { while IFS= read -d '' -n 1 var; do echo "read: $var, length: ${#var}"; done; }
read: a, length: 1
read: , length: 0
read: , length: 0
read: b, length: 1
read: c, length: 1

This is as expected, and allows detecting a NUL in the input when the
length of $var is 0  (let's not talk about EOF and non-newline terminated
inputs here, this is not the issue).

However, if trying to read 2 (or more) characters at a time, it's no longer
true:

$ 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".

Apologies if this has been already discussed and/or if I'm missing
something.

$ bash --version
GNU bash, version 4.4.23(1)-release (x86_64-unknown-linux-gnu)

-- 
D.

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web