Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #14242
| Path | csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail |
|---|---|
| From | Davide Brini <dave_br@gmx.com> |
| Newsgroups | gnu.bash.bug |
| Subject | Re: When reading less than wanted characters, "read" does not detect NUL bytes |
| Date | Fri, 15 Jun 2018 16:03:54 +0200 |
| Lines | 68 |
| Approved | bug-bash@gnu.org |
| Message-ID | <mailman.1967.1529071444.1292.bug-bash@gnu.org> (permalink) |
| References | <0Mfn88-1fqxyA1P40-00NBdT@mail.gmx.com> <20180615130746.zrpcsmyuizrrls4t@eeg.ccf.org> <0Lrek1-1gEj9F3TAb-013MGJ@mail.gmx.com> |
| NNTP-Posting-Host | lists.gnu.org |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=US-ASCII |
| Content-Transfer-Encoding | 7bit |
| X-Trace | usenet.stanford.edu 1529071445 9556 208.118.235.17 (15 Jun 2018 14:04:05 GMT) |
| X-Complaints-To | action@cs.stanford.edu |
| To | bug-bash@gnu.org |
| Envelope-to | bug-bash@gnu.org |
| In-Reply-To | <0Lrek1-1gEj9F3TAb-013MGJ@mail.gmx.com> |
| X-Mailer | Claws Mail 3.16.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) |
| X-Provags-ID | V03:K1:OpHBW5dzSDvg3dcP5Ce+Dbiqk/9NhqXPGkO3rlAd6DycQTto+ct iJUFCdo71CHG7gosTk8T+EazwUNi/2GHxI9ueky4EuptIw1gXJl1pwPpB65EsblvSyneIGj fQSlx8P32r+LLc6vkJ8zibX6JZc9dqB4Tu8uZzmms7O467g0G+qhVQG1zg2x0kxRnORUJXO 6ktos9F+A4BcFp3DXRPPA== |
| X-UI-Out-Filterresults | notjunk:1;V01:K0:TvLUkGZwEsY=:7q4N7G6YdOwj+GTXFRbkhl 8qd1cQYQcIW4kwrgrp6evq2NH4B/sSJ1hHxdGfggj79eXzdnSOin6DaC83AygeryD0RIRSzyV ZJy+WArVunbe9Jg+pOoO6azUenXNLZGl34XW1wVkEFaDlAydseqgxXzGGcxIQ9uFisImRfLaV 4PiMtzMqWvpMh7wU2pZ34OCQnEj99Abu5oWUJgyEcSOuB4gR78y9/H7wGQDtc5kDltsVIrSX7 sjVttnWrG/6MDThlKDviqCO/ueMSy9fDMNT6PdfYHt2ytqQAtZAc0WZI56C3FxKKFp3k4tkYs eH5xff0qCm6bMC4TDrvLCjnI3zLdHUtA0l/BTN4LRO5uGSwwjtH7oNJrUxIhPeF8v4S8sqjIT OyqvvTwbdOu9/Bpx5UBGFJd3spaAZRb0dJuJX7inv8fMDoEPnCDPlbPn7kEaSrhSG0Y2QZM6O 7TXOalUPJTNbe/p5mrrcJe3UleE4qQ3DPNbJWBLjT0MlZ1+9Pf0T035ljybGGwXUbM2dOc5B0 t7Jy5f7+2q8R0bhqDuggOTuog6TXzc/UF9AbEgzM2+BTmjneXeSCZ2f7iFNZDoQLO/W+mKFTR GwNiI9EEdG4Hgi85GoJtriRcmItdbUUu6QhERnjYTFfn5/V1Bdh2GT+WyOaoCxrqTNkm5vK+E lDDufCnur6UKMCQUVBR1cKkgbHG1k4iWJWwQavxRLHs/engj2IyZwT7UAsnaTHOSS/NtNjfQJ Vnd7rQ+jt09TxQfBy8Bceefc1gWuhBzQ+iHLGa+c1ow6tvp3qllsUyFqvUkzQvut15R1MrvjM 1WqsfOB |
| X-detected-operating-system | by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] |
| X-Received-From | 212.227.15.19 |
| 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:14242 |
Show key headers only | View raw
On Fri, 15 Jun 2018 15:15:23 +0200, Davide Brini <dave_br@gmx.com> wrote:
> So it looks like the only "reliable" way to detect NULs in the input is to
> read one character at a time.
Your explanation got me thinking, and I've come up with the following code
that seems to be slightly more efficient than reading one byte at a time,
as it allows reading bigger chunks of data in the parts where there are no
delimiters (NULs). I'm posting it in case it helps someone. Works For Me
(TM).
----------------------------------
to_read=512 # how many bytes to read at a time
while true; do
IFS= read -d '' -r -n $to_read data
status=$?
length=${#data}
# do whatever with what we got
for ((i=0; i < length; i++)); do
printf "Read character %02x\n" "'${data:i:1}"
done
# if we read less than we wanted, and it's not EOF, it means we also have
# a delimiter
if [ $length -lt $to_read ] && [ $status -eq 0 ]; then
printf "Read a NUL\n"
fi
# exit if EOF
if [ $status -ne 0 ]; then
break
fi
done
----------------------------------
Tests:
$ printf '\x00' | ./test.sh
Read a NUL
$ printf '\x00\x00' | ./test.sh
Read a NUL
Read a NUL
$ printf '\x00a\x00' | ./test.sh
Read a NUL
Read character 61
Read a NUL
$ printf '' | ./test.sh
$ printf 'abcd' | ./test.sh
Read character 61
Read character 62
Read character 63
Read character 64
$ printf '\x001\x00\n' | ./test.sh
Read a NUL
Read character 31
Read a NUL
Read character 0a
--
D.
Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread
Re: When reading less than wanted characters, "read" does not detect NUL bytes Davide Brini <dave_br@gmx.com> - 2018-06-15 16:03 +0200
csiph-web