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


Groups > gnu.bash.bug > #16382

hash -l with empty hash table prints to stdout

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From beluro@web.de
Newsgroups gnu.bash.bug
Subject hash -l with empty hash table prints to stdout
Date Mon, 15 Jun 2020 22:47:17 +0200
Lines 96
Approved bug-bash@gnu.org
Message-ID <mailman.1946.1592255765.2541.bug-bash@gnu.org> (permalink)
References <20200615204717.GB28118@jar>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding quoted-printable
X-Trace usenet.stanford.edu 1592255766 10178 209.51.188.17 (15 Jun 2020 21:16:06 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1592254039; bh=lHYzG0LSkDV+HjM5nl/798K9RlKdHK3gn2sFiZQh8JU=; h=X-UI-Sender-Class:Date:From:To:Subject; b=QAfPEvmsXCipb2MVwVHmijhTT1GvmjmR8x5eWXoJD1bQI53LzO7wxkJs8FewW7m6C eS9i9RNQSX5H9DQXvN6Mf61eank2hljKr5jqFgKkE9zbJprusdJioTMt1kE7HXlLIf fx+K2Fr7xmd/fsoFKNXoU8J+nf9mYfxZN79/AiGE=
X-UI-Sender-Class c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9
Content-Disposition inline
User-Agent Mutt/1.10.1 (2018-07-13)
X-Provags-ID V03:K1:7m3lw9lmKsAJxReAH6PwH+80CJENNMBoBeCXjM2JBJJy23WSGPk jqxJk0aLDcRvIwLJK37NwzS/p+/q19SWnSx7uBzSS8NSvWe4C0ptYKQSAC1HzpXuSL5X7Xu BaYIwHpHteR+TTDDlXiZrpCh/bUqj1YE8Y7qU4cr2SZgMHTxU3By6FbtqqpC6i5py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hRrJqTUzzezVJogA3H1k487k7aFg/O1lNy6i5/mtwC99tTS4DjLN08P ssenrd/GqJkvFdY1LJGky7T/SDMA26d0HkJBd+rYCLHhHG60q8cKxO+aXkMOZ5h8FbltSGSNn JdJaH3yopypAe9/lQEGG3OUrELB35sFlDYV8eSkFSZqPyAkwicAP8Fh2sdOZfHYcAeAfTdOB6 +emw+I/mS/c0Wb0Mtu/3ToFIHQcWw2KJ0Gstq7Y/XVPePrDI8akLonmtkYj+i63r8hAPo=
Received-SPF pass client-ip=212.227.17.11; envelope-from=beluro@web.de; helo=mout.web.de
X-detected-operating-system by eggs.gnu.org: First seen = 2020/06/15 16:47:19
X-ACL-Warn Detected OS = Linux 2.2.x-3.x [generic]
X-Spam_score_int -27
X-Spam_score -2.8
X-Spam_bar --
X-Spam_report (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN
X-Spam_action no action
X-Mailman-Approved-At Mon, 15 Jun 2020 17:16:04 -0400
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
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 <https://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>
X-Mailman-Original-Message-ID <20200615204717.GB28118@jar>
Xref csiph.com gnu.bash.bug:16382

Show key headers only | View raw


Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2 -fdebug-prefix-map=/build/bash-2bxm7h/bash-5.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wall -Wno-parentheses -Wno-format-security
uname output: Linux XXX 4.19.0-9-amd64 #1 SMP Debian 4.19.118-2+deb10u1 (2020-06-07) x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.0
Patch Level: 3
Release Status: release

Description:
	(I used `bashbug' from my distributor's pre-compiled package, but,
	looking at the current bash source code, I strongly believe this
	bug still exists, and you can completely ignore the "Configuration
	Information" section above.)

	`hash -l' is supposed to generate output that is reusable as input
	(`help hash' says so, at least). In case the hash table is empty, a
	string not re-usable as input is output to stdout. The exit code
	indicates success in this case. The string being output is locale-
	dependant (and therefore is hard to filter out in a work-around).

	$ hash -r
	$ hash -l
	hash: hash table empty
	$ echo $?
	0
	$ hash -l > /dev/null # no output, the message is printed to stdout
	$ hash -l 2> /dev/null # the message is not printed to stderr
	hash: hash table empty

	A work-around bash function to save the current hash table (in a re-
	usable format) would need to do something along these lines:

	saved_hash_table() {
		local hash_table="$( LANG=C hash -l )"
		# Let's hope, the string for an empty hash table never changes...
		[[ "$hash_table" =~ "hash table empty"$ ]] && hash_table=
		printf %s "$hash_table"
	}

Repeat-By:
	$ LANG=C bash
	$ hash -r
	$ $( hash -l ) # should always succeed, I think
	bash: hash:: command not found
	$ echo $?
	127

	Even weirder after creating an actual script named "hash:" (note
	trailing colon):

	$ cat /usr/local/bin/hash:
	#!/bin/sh
	echo do harm
	$ hash -r
	$ $( hash -l )
	do harm

Fix:
	I've never looked at the bash sources before, but the conundrum
	appears to be in line 138 in "bash-5.0/builtins/hash.def":

		printf (_("%s: hash table empty\n"), this_command_name);

	A bash script should be able to get a really re-usable output from
	`hash -l', at least if the return code of `hash' indicates success.

	So, either of the following changes should be applied:
	1. (preferable, in my opinion) Output the message about the empty hash
	table to stderr instead of stdout, that is, change the afore-mentioned
	line to
		fprintf (stderr, _("%s: hash table empty\n"), this_command_name);
	Saving the hash table in a script would be something along
		saved_hash_table="$( hash -l 2> /dev/null )" || handle_error
	(not sure if any errors (needed to be handled in some cases) can
	happen at all here).
	2. Don't output anything (or a single newline-character only) in case
	the hash table is empty. Saving the hash table would be simply:
		saved_hash_table="$( hash -l )" || handle_error
	with the draw-back of less informative output for interactive users.
	3. Consider an empty hash table an error so at least something along
	the following would work:
		saved_hash_table="$( hash -l )" || saved_hash_table=
	This possibly makes other errors hard to detect, though
	`$saved_hash_table' would result in a loss of efficiency only, not in
	malfunctioning software (or a security risk, my "do harm"-example is a
	little lame, I confess, I currently don't quite see a real-life
	exploitable security bug here), so ignoring a possible error (whatever
	this should be) is probably a minor thing.

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


Thread

hash -l with empty hash table prints to stdout beluro@web.de - 2020-06-15 22:47 +0200

csiph-web