Re: [sr #111211] `pipefail` leak/disablement

2025-03-24 Thread Martin D Kealey
function safe_source() {
> local saved_opts=$-
> local pipefail_on=0
>
> # Check if pipefail is set using 'shopt'
> if (set -o | grep -q 'pipefail *on'); then
>

'on' or 'off' is preceded by a tab character, plus arbitrary padding
spaces, so that won't work.

But it's a suboptimal approach to begin with.

To check a shell option such as pipefail, pick one of these:

[[ -o pipefail ]]
shopt -qo pipefail
[[ :$SHELLOPTS: = *:pipefail:* ]]

-Martin


Re: [sr #111211] `pipefail` leak/disablement

2025-03-24 Thread Emanuele Torre
On Mon, Mar 24, 2025 at 03:47:20AM -0400, anonymous wrote:
> # Check if pipefail is set using 'shopt'
> if (set -o | grep -q 'pipefail *on'); then
> pipefail_on=1
> fi

To check if an option is set you use  [[ -o pipefail ]], not that thing.

Also if you run   local -   in a function, al subsequent option changes
are retored when the function returns.

o/
 emanuele6



[sr #111211] `pipefail` leak/disablement

2025-03-24 Thread anonymous
URL:
  

 Summary: `pipefail` leak/disablement
   Group: The GNU Bourne-Again SHell
   Submitter: None
   Submitted: Mon 24 Mar 2025 07:47:14 AM UTC
Category: None
Priority: 5 - Normal
Severity: 3 - Normal
  Status: None
 Privacy: Private
 Assigned to: None
Originator Email: wod...@gmail.com
 Open/Closed: Open
 Discussion Lock: Any
Operating System: GNU/Linux


___

Follow-up Comments:


---
Date: Mon 24 Mar 2025 07:47:14 AM UTC By: Anonymous
= empty.sh =
#!/usr/bin/bash

set -x
echo '---' &>/dev/null
echo empty
echo '---' &>/dev/null
set +x

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
exit 0
else
return 0
fi

= menus.sh =
#!/usr/bin/bash

set -x
echo '---' &>/dev/null
echo menu
echo '---' &>/dev/null
set +x

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
exit 0
else
return 0
fi

= test_modules.sh =
#!/usr/bin/bash

set -o errexit
set -o nounset
set -o pipefail
set -o xtrace

export SHELLOPTS

function safe_source() {
local saved_opts=$-
local pipefail_on=0

# Check if pipefail is set using 'shopt'
if (set -o | grep -q 'pipefail *on'); then
pipefail_on=1
fi

source "$1"

# Restore flags
[[ $saved_opts == *e* ]] && set -o errexit || set +o errexit
[[ $saved_opts == *u* ]] && set -o nounset || set +o nounset
[[ $saved_opts == *x* ]] && set -o xtrace || set +o xtrace

# Restore pipefail
if [[ $pipefail_on -eq 1 ]]; then
set -o pipefail
else
set +o pipefail
fi
}

bash -c 'set -o errexit -o nounset -o pipefail; bash -x './menus.sh'
>'./exec_menus.sh.log' 2>&1'

function source_menus() {
echo "Before source: \$-=$-, SHELLOPTS=$SHELLOPTS"
safe_source './menus.sh'
echo "After source: \$-=$-, SHELLOPTS=$SHELLOPTS"
}

source_menus >'./src_menus.sh.log' 2>&1

bash -c 'set -o errexit -o nounset -o pipefail; bash -x './empty.sh'
>'./exec_empty.sh.log' 2>&1'

function source_empty() {
echo "Before source: \$-=$-, SHELLOPTS=$SHELLOPTS"
safe_source './empty.sh'
echo "After source: \$-=$-, SHELLOPTS=$SHELLOPTS"
}

source_empty >'./src_empty.sh.log' 2>&1

exit 0

= src_menus.sh.log =
+ echo 'Before source: $-=ehuxB,
SHELLOPTS=braceexpand:errexit:hashall:interactive-comments:nounset:pipefail:xtrace'
Before source: $-=ehuxB,
SHELLOPTS=braceexpand:errexit:hashall:interactive-comments:nounset:pipefail:xtrace
+ safe_source ./menus.sh
+ local saved_opts=ehuxB
+ local pipefail_on=0
+ set -o
+ grep -q 'pipefail *on'
+ source ./menus.sh
++ set -x
++ echo ---
++ echo menu
menu
++ echo ---
++ set +x
+ [[ 0 -eq 1 ]]
+ set +o pipefail
+ echo 'After source: $-=ehuxB,
SHELLOPTS=braceexpand:errexit:hashall:interactive-comments:nounset:xtrace'
After source: $-=ehuxB,
SHELLOPTS=braceexpand:errexit:hashall:interactive-comments:nounset:xtrace








___

Reply to this item at:

  

___
Message sent via Savannah
https://savannah.gnu.org/


signature.asc
Description: PGP signature


Re: foo=bar baz=${|...;} cmd segfauls after running ...

2025-03-24 Thread Chet Ramey

On 3/23/25 7:37 PM, Emanuele Torre wrote:

If you run a simple command that runs something (not just assignments)
and contains an assignment word with ${|;} that is preceeded by at least
one other assignment word, bash segfaults.


Thanks for the report. The fix was a simple matter of swapping two lines
of code when setting up the funsub.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/


OpenPGP_signature.asc
Description: OpenPGP digital signature