aboutsummaryrefslogtreecommitdiff
path: root/bin/sh/tests
Commit message (Collapse)AuthorAgeFilesLines
* sh: Fix job pointer invalidation with trapsasyncJilles Tjoelker2025-11-192-0/+10
| | | | | | | | | | | Calling dotrap() can do almost anything, including reallocating the jobtab array. Convert the job pointer to an index before calling dotrap() and then restore a proper job pointer afterwards. PR: 290330 Reported by: bdrewery Reviewed by: bdrewery Differential Revision: https://reviews.freebsd.org/D53793
* sh: Don't assume EINTR means SIGALRMDag-Erling Smørgrav2025-11-193-2/+50
| | | | | | | | | | | | | | | | | | | | | | | | | While waiting for input in the read builtin, if select() is interrupted but there is no pending signal, we act like we timed out, and return the same status as if we had been interrupted by SIGALRM, instead of looping until we actually do time out. * Replace the single select() call with a ppoll() loop. * Improve validation of the timeout value. We now accept things like "1h30m15s", which we used to silently truncate to "1h". The flip side is that we no longer accept things like "1hour" or "5sec". * Modify the existing `read -t 0` test case to verify that read returns immediately when there is input and fails immediately when there isn't. * Add a second test case which performs the same tests with a non-zero timeout value. PR: 290844 MFC after: 1 week Fixes: c4539460e3a4 ("sh: Improve error handling in read builtin:") Reviewed by: jilles, bdrewery Differential Revision: https://reviews.freebsd.org/D53761
* sh: Fix a double free in a rare scenario with pipesJilles Tjoelker2025-11-172-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The command sh -c 'sleep 3 | sleep 2 & sleep 3 & kill %1; wait %1' crashes (with appropriate sanitization such as putting MALLOC_CONF=abort:true,junk:true in the environment or compiling with -fsanitize=address). What happens here is that waitcmdloop() calls dowait() with a NULL job pointer, instructing dowait() to freejob() if it's a non-interactive shell and $! was not and cannot be referenced for it. However, waitcmdloop() then uses fields possibly freed by freejob() and calls freejob() again. This only occurs if the job being waited for is identified via % syntax ($! has never been referenced for it), it is a pipeline with two or more elements and another background job has been started before the wait command. That seems special enough for a bug to remain. Test scripts written by Jilles would almost always use $! and not % syntax. We can instead make waitcmdloop() pass its job pointer to dowait(), fixing up things for that (waitcmdloop() will have to call deljob() if it does not call freejob()). The crash from https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=290330#c2 appears to be the same bug. PR: 290330 Reported by: bdrewery Reviewed by: bdrewery Differential Revision: https://reviews.freebsd.org/D53773
* sh: Implement simple parameter expansion in PS1 and PS2Matthew Phillips2025-11-077-0/+56
| | | | | | | | | | This change follows a localized approach within getprompt() and avoids full parser reentry. While this means we don't support advanced expansions like ${parameter#pattern}, it provides POSIX-compliant basic parameter expansion without the complexity of making the parser reentrant. This is sufficient for the vast majority of use cases. PR: 46441
* sh tests: Fix racy test11.0Bryan Drewery2025-11-071-2/+2
| | | | | | | | | This was sometimes exiting while the child fifo was created resulting in [ENOTEMPTY] from rm. The child fifo isn't needed, just sleep. PR: 290837 Fixes: e31fb97148f ("read builtin: Empty variables on timeout") MFC after: 3 days
* sh(1): Do not interpret chdir to "" as equivalent to chdir with no argumentXin LI2025-07-023-0/+5
| | | | | | | | | | | | | | | | | | | | A script that does the following: cd "${dir}" || exit 1 would incorrectly remain in the current directory when `${dir}` is an empty string under the current implementation. This behavior, while historical, is potentially dangerous, as it is likely not what the script author intended. Change the command to treat an empty string as an error and emit a diagnostic message to standard error, as required by IEEE Std 1003.1-2024. PR: standards/287440 Test Plan: kyua test bin/sh Relnotes: yes Differential Revision: https://reviews.freebsd.org/D50974
* tests: Update for jemalloc's option parsingEd Maste2025-06-171-2/+2
| | | | | | | | | | MALLOC_OPTIONS=J -> MALLOC_CONF=junk:true PR: 287357 Reviewed by: markj Event: Kitchener-Waterloo Hackathon 202506 Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D50910
* sh tests: Fix ktrace usage now that envvars are traced by defaultMark Johnston2024-11-172-2/+2
| | | | | | | | | | Some sh tests use ktrace to see whether a particular file, specified in the environment, was accessed by the shell. After commit 65a4daeaf324, this test matches the ktrace record generated by execve. Use ktrace to only log name lookups, to avoid such false matches. Fixes: 65a4daeaf324 ("ktrace: log execve(2) arguments and environment")
* Remove residual blank line at start of MakefileWarner Losh2024-07-159-9/+0
| | | | | | | This is a residual of the $FreeBSD$ removal. MFC After: 3 days (though I'll just run the command on the branches) Sponsored by: Netflix
* sh tests: Update $LINENO tests after $FreeBSD$ removalMark Johnston2023-08-232-4/+4
| | | | Fixes: d0b2dbfa0ecf ("Remove $FreeBSD$: one-line sh pattern")
* Remove $FreeBSD$: one-line sh patternWarner Losh2023-08-16520-520/+0
| | | | Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
* sh: nullify ENV in testsPiotr Pawel Stefaniak2022-08-203-3/+3
| | | | | | | | This is to avoid loading .shrc which may contain commands that would result in output different than expected. Reviewed by: jilles Differential Revision: https://reviews.freebsd.org/D35876
* sh: accept fc options grouped behind one '-'Piotr Pawel Stefaniak2022-08-204-0/+14
| | | | | | | | | | | | | | | As per Utility Syntax Guidelines, accept both forms: -l -n and -ln. To do that, anticipate the source string for the next option that will be parsed by nextopt(). It's not always *argptr, sometimes it is nextopt_optptr. To simplify the check for not_fcnumber, slightly modify nextopt() to always nullify nextopt_optptr in cases where it would have been set to point to a NUL character. Reviewed by: jilles Differential Revision: https://reviews.freebsd.org/D35836
* sh: Fix heredoc at certain places in case and forJilles Tjoelker2021-10-274-0/+28
| | | | | | | | After an unescaped newline, there may be a here-document. Some places in case and for did not check for one. Reviewed by: bdrewery Differential Revision: https://reviews.freebsd.org/D32628
* read builtin: Empty variables on timeoutBryan Drewery2021-09-243-0/+27
| | | | | | | | This matches how a non-timeout error is handled. Reviewed by: jilles MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D31876
* sh/tests: Add a second kind of binary scripts without #!Jilles Tjoelker2021-01-032-0/+11
| | | | | | | | One of the reasons for git commit e0f5c1387df23c8c4811f5b24a7ef6ecac51a71a was to make "actually portable executables" work. Add a test that is more like those. MFC after: 1 week
* sh: Write absolute path in command -vV and typeJilles Tjoelker2020-09-014-0/+42
| | | | | | | | | | | | POSIX is pretty clear that command -v, command -V and type shall write absolute pathnames. Therefore, we need to prepend the current directory's name to relative pathnames. This can happen either when PATH contains a relative pathname or when the operand contains a slash but is not an absolute pathname. Notes: svn path=/head/; revision=365037
* sh: Keep ignored SIGINT/SIGQUIT after set in a background jobJilles Tjoelker2020-08-282-0/+17
| | | | | | | | | | | | | | | | | | If job control is not enabled, a background job (... &) ignores SIGINT and SIGQUIT, but this can be reverted using the trap builtin in the same shell environment. Using the set builtin to change options would also revert SIGINT and SIGQUIT to their previous dispositions. This broke due to r317298. Calling setsignal() reverts the effect of ignoresig(). Reported by: bdrewery MFC after: 1 week Notes: svn path=/head/; revision=364919
* sh/tests: Re-enable bin.sh.execution.functional_test.bg12.0Jilles Tjoelker2020-06-281-15/+1
| | | | | | | | | | This reverts r362646. PR: 247559 MFC after: 1 week Notes: svn path=/head/; revision=362738
* sh/tests: Fix flaky execution/bg12.0Jilles Tjoelker2020-06-281-1/+1
| | | | | | | | | | | | | | | | | | | | | | When job control is not enabled, the shell ignores SIGINT while waiting for a foreground process unless that process exits on SIGINT. In this case, the foreground process is sleep and it does not exit on SIGINT because the signal is only sent to the shell. Depending on order of events, this could cause the SIGINT to be unexpectedly ignored. On lightly loaded bare metal, the chance of this happening tends to be less than 0.01% but with higher loads and/or virtualization it becomes more likely. Starting the sleep in background and using the wait builtin ensures SIGINT will not be ignored. PR: 247559 Reported by: lwhsu MFC after: 1 week Notes: svn path=/head/; revision=362737
* Temporarily skip flakey bin.sh.execution.functional_test.bg12 in CILi-Wen Hsu2020-06-261-1/+15
| | | | | | | | PR: 238870 Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=362646
* sh/tests: Add tests for SIGINT in non-jobc background commandsJilles Tjoelker2020-06-143-0/+30
| | | | | | | | | | If job control is not enabled, background commands shall ignore SIGINT and SIGQUIT, and it shall be possible to override that ignore in the same shell. MFC after: 1 week Notes: svn path=/head/; revision=362182
* sh: Allow more scripts without #!Jilles Tjoelker2020-05-302-0/+9
| | | | | | | | | | | | | | | | | | | Austin Group bugs #1226 and #1250 changed the requirements for shell scripts without #! (POSIX does not specify #!; this is about the shell execution when execve(2) returns an [ENOEXEC] error). POSIX says we shall allow execution if the initial part intended to be parsed by the shell consists of characters and does not contain the NUL character. This allows concatenating a shell script (ending with exec or exit) and a binary payload. In order to reject common binary files such as PNG images, check that there is a lowercase letter or expansion before the last newline before the NUL character, in addition to the check for the newline character suggested by POSIX. Notes: svn path=/head/; revision=361647
* sh: Fix double INTON with vforkJilles Tjoelker2020-05-162-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | The shell maintains a count of the number of times SIGINT processing has been disabled via INTOFF, so SIGINT processing resumes when all disables have enabled again (INTON). If an error occurs in a vfork() child, the processing of the error enables SIGINT processing again, and the INTON in vforkexecshell() causes the count to become negative. As a result, a later INTOFF may not actually disable SIGINT processing. This might cause memory corruption if a SIGINT arrives at an inopportune time. As of r360452, it causes the shell to abort when it would unsafely allocate or free memory in certain ways. Note that various places such as errors in non-special builtins unconditionally reset the count to 0, so the problem might still not always be visible. PR: 246497 Reported by: jbeich MFC after: 2 weeks Notes: svn path=/head/; revision=361112
* sh/tests: Test some obscure cases with aliasing keywordsJilles Tjoelker2020-05-125-0/+21
| | | | Notes: svn path=/head/; revision=360992
* sh: Test that executing various binary files is rejectedJilles Tjoelker2019-12-305-0/+64
| | | | | | | | | | | | | | | If executing a file fails with an [ENOEXEC] error, the shell executes the file as a shell script, except that this execution may instead result in an error message if the file is binary. Per a recent Austin Group interpretation, we will need to change this to allow a concatenation of a shell script and a binary payload. See Austin Group bugs #1226 and #1250. MFC after: 1 week Notes: svn path=/head/; revision=356208
* sh/tests: Improve failure messages of expansion/arith15.0Jilles Tjoelker2019-03-071-3/+3
| | | | Notes: svn path=/head/; revision=344902
* sh: Add set -o pipefailJilles Tjoelker2019-02-248-0/+39
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The pipefail option allows checking the exit status of all commands in a pipeline more easily, at a limited cost of complexity in sh itself. It works similarly to the option in bash, ksh93 and mksh. Like ksh93 and unlike bash and mksh, the state of the option is saved when a pipeline is started. Therefore, even in the case of commands like A | B & a later change of the option does not change the exit status, the same way (A | B) & works. Since SIGPIPE is not handled specially, more work in the script is required for a proper exit status for pipelines containing commands such as head that may terminate successfully without reading all input. This can be something like ( cmd1 r=$? if [ "$r" -gt 128 ] && [ "$(kill -l "$r")" = PIPE ]; then exit 0 else exit "$r" fi ) | head PR: 224270 Relnotes: yes Notes: svn path=/head/; revision=344502
* sh: Restore $((x)) error checking after fix for $((-9223372036854775808))Jilles Tjoelker2019-02-103-0/+31
| | | | | | | | | | | | | | | | | | SVN r342880 was designed to fix $((-9223372036854775808)) and things like $((0x8000000000000000)) but also broke error detection for values of variables without dollar sign ($((x))). For compatibility, overflow in plain literals continues to be ignored and the value is clamped to the boundary (except 9223372036854775808 which is changed to -9223372036854775808). Reviewed by: se (although he would like error checking to be removed) MFC after: 2 weeks X-MFC-with: r342880 Differential Revision: https://reviews.freebsd.org/D18926 Notes: svn path=/head/; revision=343981
* Fix an edge case when parsing large numbers which resulted in inconsistentDag-Erling Smørgrav2019-01-092-0/+21
| | | | | | | | | | | results between an expression that refers to a variable by name and the same expression that includes the same variable by value. Submitted by: se@ MFC after: 1 week Notes: svn path=/head/; revision=342880
* sh: Add test for exported but unset variablesJilles Tjoelker2019-01-032-0/+6
| | | | | | | PR: 233545 Notes: svn path=/head/; revision=342741
* sh: Fix ${param?} default error messageJilles Tjoelker2018-11-282-0/+12
| | | | | | | | | | | | | | | If word in ${param?word} is missing, the shell shall write a default error message. So expanding ${param?} when param is not set should write an error message like sh: param: parameter not set This was broken by r316417. PR: 233585 Notes: svn path=/head/; revision=341164
* sh: Use 126 and 127 exit status for failures opening a scriptJilles Tjoelker2018-11-272-0/+6
| | | | | | | | | | This affects scripts named on the command line, named with a '.' special builtin and found via the PATH %func autoloading mechanism. PR: 231986 Notes: svn path=/head/; revision=341097
* sh: Don't treat % specially in CDPATHJilles Tjoelker2018-07-152-0/+25
| | | | Notes: svn path=/head/; revision=336320
* sh: Allow unquoted newlines in word in ${param+word} etc.Jilles Tjoelker2018-05-204-0/+25
| | | | | | | | | | | | | | | | | | | POSIX requires accepting unquoted newlines in word in parameter expansions like ${param+word}, ${param#word}, although the Bourne shell did not support it, it is not commonly used and might make it harder to find a missing closing brace. It was also strange that something like foo="${bar# }" was rejected. Reported by: Martijn Dekker via Robert Elz Notes: svn path=/head/; revision=333927
* sh: Test that backslash-newline within single-quotes is not specialJilles Tjoelker2018-05-112-0/+6
| | | | | | | This works correctly, but the test may be helpful when modifying the parser. Notes: svn path=/head/; revision=333507
* sh: Don't have [ match any [[:class:]]Jilles Tjoelker2018-04-292-0/+6
| | | | | | | | Submitted by: Robert Elz MFC after: 3 days Notes: svn path=/head/; revision=333092
* Fix a few speelling errorsEitan Adler2017-12-281-1/+1
| | | | | | | | | | - man pages - bin/sh Reviewed by: jilles Notes: svn path=/head/; revision=327281
* DIRDEPS_BUILD: Connect new directories.Bryan Drewery2017-10-311-0/+11
| | | | | | | Sponsored by: Dell EMC Isilon Notes: svn path=/head/; revision=325187
* sh: Add test for sh -c with missing command string.Jilles Tjoelker2017-08-132-0/+4
| | | | | | | This already works correctly. Notes: svn path=/head/; revision=322455
* sh: Add tests for sh -c that already pass.Jilles Tjoelker2017-08-126-0/+41
| | | | | | | | PR: 220587 Submitted by: Ryan Moeller Notes: svn path=/head/; revision=322438
* sh: Ignore error when cd writes the directory actually switched to.Jilles Tjoelker2017-06-252-0/+7
| | | | | | | | | | | | If CDPATH is used non-trivially or the operand is "-", cd writes the directory actually switched to. (We currently do this only in interactive shells, but POSIX requires this in non-interactive shells as well.) As mentioned in Austin group bug #1045, cd shall not return an error while leaving the current directory changed. Therefore, ignore any write error. Notes: svn path=/head/; revision=320340
* sh: Fix INTOFF leak when a redirection on a compound command fails.Jilles Tjoelker2017-06-042-0/+6
| | | | | | | Reported by: bdrewery Notes: svn path=/head/; revision=319575
* sh: Fix '-' from quoted arithmetic in case/glob pattern range.Jilles Tjoelker2017-05-142-0/+11
| | | | | | | | | | | It does not make much sense to generate the '-' in a pattern bracket expression using arithmetic expansion, but it does not make sense to forbid it either. Try to avoid reprocessing the string if it is unnecessary. Notes: svn path=/head/; revision=318269
* sh: Add test for arithmetic expansion in [x-y] pattern range.Jilles Tjoelker2017-05-132-0/+11
| | | | | | | | | | | It does not make much sense to generate the '-' in a pattern bracket expression using arithmetic expansion, but it does not make sense to forbid it either. This test already passes. Notes: svn path=/head/; revision=318258
* sh: Fix INTOFF leak after a builtin with different locale settings.Jilles Tjoelker2017-05-072-0/+6
| | | | | | | | | | | After executing a builtin with different locale settings such as LC_ALL=C true SIGINT handling was left disabled indefinitely. MFC after: 1 week Notes: svn path=/head/; revision=317912
* sh: Add some tests for command substitution final newline stripping.Jilles Tjoelker2017-04-273-0/+15
| | | | Notes: svn path=/head/; revision=317514
* sh: Add tests for NUL byte in command substitution output.Jilles Tjoelker2017-04-232-0/+25
| | | | Notes: svn path=/head/; revision=317347
* sh: Fix use after free when resetting an in-use alias.Jilles Tjoelker2017-04-162-0/+9
| | | | | | | | | The special case of modifying an existing alias does not work correctly if the alias is currently in use. Instead, handle this case by unaliasing the old alias (if any) and then creating a new alias. Notes: svn path=/head/; revision=317039
* sh: Link the new test to the build.Jilles Tjoelker2017-04-161-0/+1
| | | | Notes: svn path=/head/; revision=317038