diff options
author | Bryan Drewery <bryan@shatow.net> | 2022-09-20 00:33:51 +0300 |
---|---|---|
committer | Bryan Drewery <bryan@shatow.net> | 2022-11-04 17:33:23 +0300 |
commit | 3588824845fdab1221291592562f0bd4030c73aa (patch) | |
tree | 9a0704d34f8927f699ad7e79f637967a4fa74a0c | |
parent | 083cafbdb6d8b7338884daab1dcada6886bff49c (diff) |
mapfile_read_loop_redir isn't safe
-rw-r--r-- | src/share/poudriere/include/util.sh | 3 | ||||
-rw-r--r-- | test/mapfile.sh | 57 |
2 files changed, 59 insertions, 1 deletions
diff --git a/src/share/poudriere/include/util.sh b/src/share/poudriere/include/util.sh index e8873acc..3fd31ebd 100644 --- a/src/share/poudriere/include/util.sh +++ b/src/share/poudriere/include/util.sh @@ -798,7 +798,8 @@ mapfile_read_loop() { mapfile_read_loop_redir() { [ $# -ge 1 ] || eargs mapfile_read_loop_redir vars - mapfile_read_loop "/dev/fd/0" "$@" + #mapfile_read_loop "/dev/fd/0" "$@" + read -r "$@" } # Basically an optimized loop of mapfile_read_loop_redir, or read_file diff --git a/test/mapfile.sh b/test/mapfile.sh index 3f8e5599..6b8190e1 100644 --- a/test/mapfile.sh +++ b/test/mapfile.sh @@ -252,6 +252,7 @@ fi echo "${n}" i=$((i + 1)) done + assert 10 "${i}" fds=$(procstat -f $$|wc -l) echo "-" >&2 procstat -f $$ >&2 @@ -272,6 +273,60 @@ fi echo "${n}" i=$((i + 1)) done < "${TMP}" + assert 10 "${i}" + fds=$(procstat -f $$|wc -l) + [ ${JAILED} -eq 0 ] && assert "${expectedfds}" "${fds}" "fd leak 2" +} + +# Test mapfile_read_loop_redir with nested call +{ + rm -f "${TMP}" + TMP=$(mktemp -t mapfile) + + jot 10 0 > "${TMP}" + echo inner > "${TMP}.2" + + expectedfds=$(procstat -f $$|wc -l) + i=0 + while mapfile_read_loop_redir n; do + assert "$i" "$n" "value should match 1 $i" + echo "${n}" + i=$((i + 1)) + mapfile_read_loop_redir n < "${TMP}.2" + assert "$n" "inner" "nested call on stdin" + done < "${TMP}" + assert 10 "$i" + rm -f "${TMP}" "${TMP}.2" + fds=$(procstat -f $$|wc -l) + [ ${JAILED} -eq 0 ] && assert "${expectedfds}" "${fds}" "fd leak 2" +} + +# Test mapfile_read_loop_redir with early return +{ + rm -f "${TMP}" + TMP=$(mktemp -t mapfile) + + jot 10 0 > "${TMP}" + + expectedfds=$(procstat -f $$|wc -l) + i=0 + while mapfile_read_loop_redir n; do + assert "$i" "$n" "value should match 1 $i" + echo "${n}" + i=$((i + 1)) + if [ "${n}" -eq 5 ]; then + break + fi + done < "${TMP}" + assert 6 "${i}" + # This may end up reading 6 next due to reused /dev/stdin + i=0 + while mapfile_read_loop_redir n; do + assert "$i" "$n" "value should match 1 $i" + echo "${n}" + i=$((i + 1)) + done < "${TMP}" + assert 10 "$i" fds=$(procstat -f $$|wc -l) [ ${JAILED} -eq 0 ] && assert "${expectedfds}" "${fds}" "fd leak 2" } @@ -296,6 +351,7 @@ fi i=$((i + 1)) done < "${TMP}" fds=$(procstat -f $$|wc -l) + assert 10 "${i}" [ ${JAILED} -eq 0 ] && assert "${expectedfds}" "${fds}" "fd leak 3" } @@ -318,6 +374,7 @@ fi assert "$((i + 5))" "$y" "value should match 3 $((i + 5))" i=$((i + 1)) done < "${TMP}" + assert 10 "$i" fds=$(procstat -f $$|wc -l) [ ${JAILED} -eq 0 ] && assert "${expectedfds}" "${fds}" "fd leak 4" } |