commit:     6ce38387d6dfdaf5aad04e3f44f588bfe6a4b586
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Tue Jun 17 00:15:14 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Tue Jun 17 03:00:41 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=6ce38387

Refrain from using RETURN traps

Presently, there are two units that set up a RETURN trap.

- bin/ecompress (by the guess_suffix() function)
- bin/etc-update (by the show_diff() function)

Alas, RETURN traps are difficult to manage in bash because they affect
all functions until otherwise cleared or re-defined - a fact that I had
somehow managed to overlook at the time of introducing them. Whatever
value they would otherwise have conferred over the use of an EXIT trap
is duly decimated.

As concerns the ecompress utility, address the issue by having the
guess_suffix() function declare an EXIT trap instead. To do so is
acceptable, because bash is always made to fork in the course of calling
that particular function. So as to emphasise this point, the body of the
function has been changed to a compound command of the ( … ) form.

As concerns the etc-update utility, simply remove the RETURN trap. The
directories created by the show_diff() function are subdirectories of a
known temporary directory, which is already covered by an EXIT trap.

Fixes: 9b95224283b2ed992c482956df952fada2fd41ef
Fixes: 508a79cedd09079174299239afb4e12e5470b129
Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 bin/ecompress  | 7 +++----
 bin/etc-update | 2 --
 2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/bin/ecompress b/bin/ecompress
index 0cd8211595..6a1d2ab472 100755
--- a/bin/ecompress
+++ b/bin/ecompress
@@ -116,12 +116,11 @@ do_queue() {
        fi
 }
 
-guess_suffix() {
+guess_suffix() (
        local IFS f i tmpdir
        local -a args
 
-       trap 'rm -rf -- "${tmpdir}"' RETURN
-
+       trap 'rm -rf -- "${tmpdir}"' EXIT
        tmpdir=$(mktemp -d -- "${T:-/tmp}/tmp.XXXXXX") \
        && cd -- "${tmpdir}" \
        || return
@@ -143,7 +142,7 @@ guess_suffix() {
                && printf '%s\n' "${f#compressme}" \
                && return
        done
-}
+)
 
 fix_symlinks() {
        local something_changed link target1 target2 i

diff --git a/bin/etc-update b/bin/etc-update
index 567fc6c74f..a73076a6cc 100755
--- a/bin/etc-update
+++ b/bin/etc-update
@@ -469,8 +469,6 @@ show_diff() {
        local file1=$1 file2=$2 files=("$1" "$2") \
                diff_files=() file i tmpdir
 
-       trap '[[ ${tmpdir} ]] && rm -r -- "${tmpdir}"' RETURN
-
        if [[ -L ${file1} && ! -L ${file2} &&
                -f ${file1} && -f ${file2} ]] ; then
                # If a regular file replaces a symlink to a regular file, then

Reply via email to