[Whonix-devel] [Help-bash] Why read exits non-zero? How to safely use bash's built-in read command?

Greg Wooledge wooledg at eeg.ccf.org
Mon Oct 7 14:54:11 CEST 2019


On Mon, Oct 07, 2019 at 06:47:00AM +0000, Patrick Schleizer wrote:
> I found a file that makes bash's built-in read command exit non-zero.

> set -e

I found your first problem.

> 
> test -f ./advancedsettings.ico
> 
> while read -r line; do
>    echo "$line"
>    read -r first_word _ <<< "$line"
>    echo "$first_word"
> done < "./advancedsettings.ico"

> https://raw.githubusercontent.com/Whonix/anon-connection-wizard/master/usr/share/anon-connection-wizard/advancedsettings.ico

This is not a text file.  It's a binary file.

wooledg:/tmp$ hd advancedsettings.ico  | head
00000000  00 00 01 00 01 00 80 80  00 00 01 00 20 00 28 08  |............ .(.|
00000010  01 00 16 00 00 00 28 00  00 00 80 00 00 00 00 01  |......(.........|
00000020  00 00 01 00 20 00 00 00  00 00 00 00 01 00 12 0b  |.... ...........|
00000030  00 00 12 0b 00 00 00 00  00 00 00 00 00 00 ff ff  |................|
00000040  ff 00 ff ff ff 00 ff ff  ff 00 ff ff ff 00 ff ff  |................|
*
00000070  ff 00 dd dd da 00 db da  dc 00 db db db 00 d9 d9  |................|
00000080  da 00 db da da 00 ff ff  ff 00 da dc de 00 df e2  |................|
00000090  e0 00 e0 e2 e2 00 e4 e4  e4 00 e7 e7 e7 00 e9 ec  |................|
000000a0  eb 00 ed ed ec 00 f2 f3  f2 00 f2 f3 f3 00 f6 f6  |................|

Attempting to process a binary file as if it were text, including
any notion of "lines" or "fields", is doomed.

Most likely, read is throwing a fit because you're feeding it input
that doesn't constitute a valid stream of characters in your locale.
In any event, all those NUL bytes are going to be a problem.  Bash
cannot store NUL bytes in a variable, at all, ever.

This script is simply not suited for this type of input.


More information about the Whonix-devel mailing list