Skip to content

Commit

Permalink
read builtin: Empty variables on timeout
Browse files Browse the repository at this point in the history
This matches how a non-timeout error is handled.

Reviewed by:	jilles
MFC after:	2 weeks
Differential Revision: https://reviews.freebsd.org/D31876
  • Loading branch information
bdrewery committed Sep 24, 2021
1 parent 0389e9b commit e31fb97
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bin/sh/miscbltin.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ readcmd(int argc __unused, char **argv __unused)
* If there's nothing ready, return an error.
*/
if (status <= 0) {
while (*ap != NULL)
setvar(*ap++, "", 0);
sig = pendingsig;
return (128 + (sig != 0 ? sig : SIGALRM));
}
Expand Down
2 changes: 2 additions & 0 deletions bin/sh/tests/builtins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ ${PACKAGE}FILES+= read6.0
${PACKAGE}FILES+= read7.0
${PACKAGE}FILES+= read8.0
${PACKAGE}FILES+= read9.0
${PACKAGE}FILES+= read10.0
${PACKAGE}FILES+= read11.0
${PACKAGE}FILES+= return1.0
${PACKAGE}FILES+= return2.1
${PACKAGE}FILES+= return3.1
Expand Down
8 changes: 8 additions & 0 deletions bin/sh/tests/builtins/read10.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

set -e

v=original_value
r=0
read v < /dev/null || r=$?
[ "$r" -eq 1 ]
[ -z "$v" ]
17 changes: 17 additions & 0 deletions bin/sh/tests/builtins/read11.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

set -e

T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX)
trap 'rm -rf "$T"' 0
cd $T
mkfifo fifo1
# Open fifo1 for writing and then read block on a dummy fifo
{ mkfifo fifo2; read dummy <fifo2; } >fifo1 &
# Wait for the child to open fifo1 for writing
exec 3<fifo1
v=original_value
r=0
read -t 0 v <&3 || r=$?
kill -TERM "$!" || :
{ [ "$r" -gt 128 ] && [ "$(kill -l "$r")" = ALRM ]; } || exit
[ -z "$v" ]

0 comments on commit e31fb97

Please sign in to comment.