bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bernhard Voelker <mail@bernhard-voelker.de>
To: Kamil Dudka <kdudka@redhat.com>, Paul Eggert <eggert@cs.ucla.edu>
Cc: bug-gnulib <bug-gnulib@gnu.org>
Subject: Re: tests: dis/allow '.' in PATH?
Date: Thu, 25 Nov 2021 00:03:53 +0100	[thread overview]
Message-ID: <2f575bd5-8259-1e27-10eb-428060a2e34e@bernhard-voelker.de> (raw)
In-Reply-To: <5769878.lOV4Wx5bFT@nbkamil>

[-- Attachment #1: Type: text/plain, Size: 290 bytes --]

On 11/24/21 08:24, Kamil Dudka wrote:
> Note that having an "empty item" in PATH will have the same effect as
> having '.' in PATH, according to POSIX [1]:

Sure, the attached is an approach with shell tools, i.e., avoiding maybe
non-portable shell extensions.
WDYT?

Have a nice day,
Berny

[-- Attachment #2: 0001-test-framework-sh-remove-.-and-empty-entries-from-PA.patch --]
[-- Type: text/x-patch, Size: 2344 bytes --]

From a864d77c8b723fb3aede775c545320cfcedcc8bf Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <mail@bernhard-voelker.de>
Date: Wed, 24 Nov 2021 23:59:00 +0100
Subject: [PATCH] test-framework-sh: remove '.' and empty entries from PATH

Running tests with '.' in the PATH may yield unspecified results,
and is deemed unsafe per se.  This includes empty entries as well
which are treated like a '.' entry as per POSIX.

* tests/init.sh (setup_): Add snippet to remove '.' and empty entries
from the PATH environment variable.
---
 ChangeLog     |  9 +++++++++
 tests/init.sh | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 3e752b238..f858fee07 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2021-11-24  Bernhard Voelker  <mail@bernhard-voelker.de>
+
+	test-framework-sh: remove '.' and empty entries from PATH
+	Running tests with '.' in the PATH may yield unspecified results,
+	and is deemed unsafe per se.  This includes empty entries as well
+	which are treated like a '.' entry as per POSIX.
+	* tests/init.sh (setup_): Add snippet to remove '.' and empty entries
+	from the PATH environment variable.
+
 2021-11-24  Paul Eggert  <eggert@cs.ucla.edu>
 
 	regex: merge from glibc
diff --git a/tests/init.sh b/tests/init.sh
index 9ef834888..1f4d29e8b 100644
--- a/tests/init.sh
+++ b/tests/init.sh
@@ -426,6 +426,24 @@ setup_ ()
   for sig_ in 1 2 3 13 15; do
     eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
   done
+
+  # Remove '.' from PATH, and also avoid "empty entries" which are treated as
+  # '.' as per POSIX, see PATH in:
+  # https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
+  # Strategy:
+  # printf: wrap PATH with additional separator characters.
+  # tr: change all separators to newline.
+  # sed: remove dot entries; remove empty entries (note: trailing newline).
+  # tr: change newlines back to separators
+  # head: remove last trailing separator.
+  path_=`printf '%s' "${PATH_SEPARATOR}${PATH}${PATH_SEPARATOR}" \
+           | tr "${PATH_SEPARATOR}" "$gl_init_sh_nl_" \
+           | sed -e '/^\.$/d' -e '/^$/d'\
+           | tr "$gl_init_sh_nl_" "${PATH_SEPARATOR}" \
+           | head -c -1
+           `
+  PATH="$path_"
+  export PATH
 }
 
 # This is a stub function that is run upon trap (upon regular exit and
-- 
2.33.1


  reply	other threads:[~2021-11-24 23:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-23 22:19 tests: dis/allow '.' in PATH? Bernhard Voelker
2021-11-24  0:10 ` Paul Eggert
2021-11-24  7:24 ` Kamil Dudka
2021-11-24 23:03   ` Bernhard Voelker [this message]
2021-11-24 23:54     ` Paul Eggert
2021-11-26  0:10       ` Bernhard Voelker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2f575bd5-8259-1e27-10eb-428060a2e34e@bernhard-voelker.de \
    --to=mail@bernhard-voelker.de \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    --cc=kdudka@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).