From 9ce2c9b1872b44cebe1ce54c2a7398fbbc31fc6a Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Mon, 22 Dec 2014 09:04:53 -0500 Subject: makepkg: make run_function_safe more robust Use shopt to set/reset errexit and errtrace, which lets us: 1) be more vigilant, resetting anything the user might do to us in PKGBUILD functions. 2) use human-readable words (errexit vs. -e) On top of this, introduce a new save/restore for the shell's other shopts. A user should not have any expectations that what happens in one function is available in another function, if it isn't explicitly defined in the PKGBUILD. While this change does not make that assertion, it gets us closer. We also replace a variable which comes from out of nowhere (pkgfunc) with the positional parameter containing the same value. Quoting is adjusted to make the expansion happen at the time the trap is set, rather than later on. --- scripts/makepkg.sh.in | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index a1385c17..a97cdc28 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -398,20 +398,23 @@ prepare_buildenv() { } run_function_safe() { - local restoretrap + local restoretrap restoreset restoreshopt - set -e - set -E + # we don't set any special shopts of our own, but we don't want the user to + # muck with our environment. + restoreshopt=$(shopt -p) + + restoreset=$(shopt -o -p) + shopt -o -s errexit errtrace restoretrap=$(trap -p ERR) - trap 'error_function $pkgfunc' ERR + trap "error_function '$1'" ERR run_function "$1" - eval $restoretrap - - set +E - set +e + eval "$restoretrap" + eval "$restoreset" + eval "$restoreshopt" } run_function() { -- cgit v1.2.3-70-g09d2