Commit Graph

1110 Commits

Author SHA1 Message Date
Jilles Tjoelker
55c2cd6f48 sh: Enable interrupts before executing EXIT trap and doing final flush. 2017-06-11 16:54:04 +00:00
Jilles Tjoelker
79fb1e455e sh: Call fc -e editor with interrupts enabled.
Starting the fc -e editor can execute arbitrary script, and executing
arbitrary script with INTOFF in effect may cause unexpected results.

This change (together with other changes) serves mainly to allow asserting
that INTOFF is not in effect when starting the evaluation of a node.
2017-06-06 21:08:05 +00:00
Jilles Tjoelker
8d4cde8e6d sh: Make sure to process SIGINT if SETINTON re-enables processing.
If INTON re-enables interrupts, it processes any interrupt that occurred
while interrupts were disabled. Make SETINTON do the same.
2017-06-04 21:58:02 +00:00
Bryan Drewery
3ecb77f014 Allow defining nofork builtins from builtins.def and move always-safe ones there.
The generated code remains the same.

Reviewed by:	jilles
Differential Revision:	https://reviews.freebsd.org/D11042
2017-06-04 21:02:48 +00:00
Jilles Tjoelker
eab4998278 sh: Fix INTOFF leak when a redirection on a compound command fails.
Reported by:	bdrewery
2017-06-04 20:52:55 +00:00
Jilles Tjoelker
29717eb029 sh: Keep output buffer across builtins.
Allocating and deallocating repeatedly the 1024-byte buffer for stdout from
builtins costs CPU time for little or no benefit.

A simple loop containing builtins that write to a file descriptor, such as
  i=0; while [ "$i" -lt 1000000 ]; do printf .; i=$((i+1)); done >/dev/null
is over 10% faster in a simple benchmark on an amd64 virtual machine.
2017-05-18 22:10:04 +00:00
Jilles Tjoelker
de29cd0869 sh: Ensure memout.bufsize matches allocated buffer, if it exists. 2017-05-18 21:44:14 +00:00
Jilles Tjoelker
5183ddf2ed sh: Simplify output buffering.
Similarly to how STPUTC was changed, change struct output to store the
pointer just past the end of the available space instead of the size of the
available space, so after writing a character it is only necessary to
increment a pointer and not to decrement a counter.
2017-05-16 21:54:51 +00:00
Jilles Tjoelker
1b21b7fa89 sh: Fix '-' from quoted arithmetic in case/glob pattern range.
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.
2017-05-14 13:14:19 +00:00
Jilles Tjoelker
73a73f7bd2 sh: Add test for arithmetic expansion in [x-y] pattern range.
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.
2017-05-13 20:28:32 +00:00
Jilles Tjoelker
3f2da875f7 sh: Fix INTOFF leak after a builtin with different locale settings.
After executing a builtin with different locale settings such as
  LC_ALL=C true
SIGINT handling was left disabled indefinitely.

MFC after:	1 week
2017-05-07 19:49:46 +00:00
Jilles Tjoelker
b98072777f sh: Update TOUR and comments for some code changes, some of them old.
Also, improve some terminology in TOUR and comments.
2017-05-06 13:28:42 +00:00
Jilles Tjoelker
d4993b6db2 sh: Simplify handling of newlines in command substitution.
Unless we need to split on newlines, just append them as normal and remove
them at the end.
2017-04-28 16:16:22 +00:00
Jilles Tjoelker
24b55fb76d sh: Add some tests for command substitution final newline stripping. 2017-04-27 18:52:18 +00:00
Jilles Tjoelker
53f6052862 sh: Add tests for NUL byte in command substitution output. 2017-04-23 21:58:17 +00:00
Jilles Tjoelker
3be4e97d2b sh: Simplify setinteractive().
setsignal() does nothing if the signal disposition is already set correctly.
2017-04-22 21:31:37 +00:00
Jilles Tjoelker
026dfd4aef sh: Fix use after free when resetting an in-use alias.
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.
2017-04-16 22:10:02 +00:00
Jilles Tjoelker
683bf56cf6 sh: Link the new test to the build. 2017-04-16 21:57:25 +00:00
Jilles Tjoelker
773e27aeee sh: Fix unalias -a while an alias is currently in use.
It is a rare situation to modify aliases while an alias is currently in use,
but this is handled for plain unalias. Handle it for unalias -a as well.
2017-04-16 21:42:43 +00:00
Jilles Tjoelker
a9250603fd sh: Reduce size of limits table. 2017-04-12 21:15:55 +00:00
Jilles Tjoelker
5850a75952 sh: Add test for unaliasing an alias that is currently in use.
This already works correctly.
2017-04-08 21:57:59 +00:00
Jilles Tjoelker
05e626c3cf sh: Remove an XXX comment: it is normal for builtins to use argptr. 2017-04-02 15:53:11 +00:00
Jilles Tjoelker
c39d3320ab sh: Remove global state from collate_range_cmp().
The global state is not used across invocations of collate_range_cmp().
2017-04-02 14:02:10 +00:00
Jilles Tjoelker
e59833ccfe sh: Don't scan word twice in ${param=word}. 2017-04-02 13:43:45 +00:00
Jilles Tjoelker
e2708b1624 sh: Don't scan word twice in ${param#%##%%word}.
If word is expanded, use the found end instead of iterating over the data
again.
2017-04-02 13:29:27 +00:00
Jilles Tjoelker
9e1bb30e23 sh: Don't scan word twice in ${param+-word}.
There is no longer a case where argbackq has already been advanced but the
string pointer needs to be advanced.
2017-04-02 12:37:43 +00:00
Jilles Tjoelker
a54caffda0 sh: Fix build with -DDEBUG=2.
With the new expansion code (word splitting during instead of after other
expansion processing), tracing the result of command substitution is no
longer possible, so stop trying.
2017-03-19 21:18:53 +00:00
Jilles Tjoelker
0620d81993 sh: Remove unused function declaration for arith_lex_reset().
Reported by:	Robert Elz
2017-03-18 20:41:07 +00:00
Jilles Tjoelker
56bf1d616f sh: Remove unused return value of subevalvar_misc(). 2017-03-18 16:09:30 +00:00
Jilles Tjoelker
439948cdf6 sh: Get rid of global variable argbackq. 2017-03-16 21:53:55 +00:00
Jilles Tjoelker
8999a290ab sh: Fix executing wrong command with ${unsetvar#$(cmdsubst)}$(cmdsubst).
The parsed internal representation of words consists of a byte string with a
list of nodes (commands in command substitution). Each unescaped CTLBACKQ or
CTLBACKQ | CTLQUOTE byte corresponds to an entry in the list.

If param in ${param#%##%%word} is not set, the word is not expanded (in a
deviation of POSIX shared with other ash variants and ksh93). Erroneously,
the pointer in the list of commands (argbackq) was not advanced. This caused
the wrong command to be executed later if the outer word contained another
command substitution.

Example:
  echo "${unsetvar#$(echo a)}$(echo b)"
wrote "a" but should write "b".

MFC after:	1 week
2017-03-10 16:04:00 +00:00
Jilles Tjoelker
586fd24857 sh: Fix crash if a -T trap is taken during command substitution.
Code like  t=$(stat -f %m "$file")  segfaulted if -T was active and a trap
was taken while the shell was waiting for the child process to finish.

What happened was that the dotrap() call in waitforjob() was hit. This
re-entered command execution (including expand.c) at a point not expected by
expbackq(), and global state (unallocated stack string and argbackq) was
corrupted.

To fix this, change expbackq() to prepare for command execution to be
re-entered.

Reported by:	bdrewery
MFC after:	1 week
2017-03-04 22:58:34 +00:00
Enji Cooper
caf42d8126 bin: normalize paths using SRCTOP-relative paths or :H when possible
This simplifies make logic/output

MFC after:	1 month
Sponsored by:	Dell EMC Isilon
2017-03-04 11:31:08 +00:00
Jilles Tjoelker
549bbb4fa1 sh: Add some already working tests that exercise new code paths. 2017-03-03 22:46:20 +00:00
Warner Losh
fbbd9655e5 Renumber copyright clause 4
Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by:	Jan Schaumann <jschauma@stevens.edu>
Pull Request:	https://github.com/freebsd/freebsd/pull/96
2017-02-28 23:42:47 +00:00
Baptiste Daroussin
33bf6c45fb Remove empty Li
Reported by:	make manlint
MFC after:	2 days
2017-02-11 23:09:50 +00:00
Jilles Tjoelker
3c47cee5c8 sh: Add simple test for 'set -C' (noclobber).
To ensure fast test runs, race conditions are not tested.
2016-11-02 22:33:37 +00:00
Jilles Tjoelker
7cca93e61a sh: Do not import IFS's value from the environment.
Per Austin group issue #884, always set IFS to $' \t\n'. As before, IFS will
be exported iff it was in the environment.

Most shells (e.g. bash, ksh93 and mksh) already did this. This change
improves predictability, in that scripts can simply rely on the default
value.

However, the effect on security is little, since applications should not be
calling the shell with attacker-controlled environment variable names in the
first place and other security-sensitive variables such as PATH should be
and are imported by the shell.

When using a new sh with an old (before 10.2) libc wordexp(), IFS is no
longer passed on. Otherwise, wordexp() continues to pass along IFS from the
environment per its documentation.

Discussed with:	pfg
Relnotes:	yes
2016-10-08 13:40:12 +00:00
Jilles Tjoelker
681e94f4d0 sh: Add some tests for non-standard features of the echo builtin.
MFC after:	1 week
2016-09-02 21:13:46 +00:00
Jilles Tjoelker
3bb6ada261 sh: Fix a clang warning.
Submitted by:	bdrewery
2016-07-31 13:11:34 +00:00
Andrey A. Chernov
143d321a30 Path generation was not according to collate
Approved by:    jilles
2016-07-16 13:26:18 +00:00
Andrey A. Chernov
fa93fc659f Back out non-collating [a-z] ranges.
Instead of changing the whole course to another POSIX-permitted way
for consistency and uniformity I decide to completely ignore missing
regex fucntionality and focus on fixing bugs in what we have now,
too many small obstacles we have choicing other way, counting ports.
Corresponding libc changes are backed out in r302824.
2016-07-14 09:34:42 +00:00
Andrey A. Chernov
3ea37deb6c After removing collation for [a-z] ranges in r302512, do it here too.
Approved by:    jilles
2016-07-13 08:13:09 +00:00
Jilles Tjoelker
535c8d9372 sh: Improve descriptions in 'ulimit -a' output.
The format limits descriptions to 18 characters and is not changed, so
the descriptions do not describe the limits exactly.
2016-06-05 16:09:31 +00:00
Bryan Drewery
92edc96667 WITH_META_MODE: Don't expect meta files for side-effect generated files.
The first file in these lists will generate everything else so only
it should be getting a .meta file.  With bmake's missing=yes meta
feature these would otherwise cause a rebuild without the
.NOMETA hint.

Sponsored by:	EMC / Isilon Storage Division
2016-06-03 19:25:41 +00:00
Don Lewis
eaf2e1e6f7 The (i < PROMPTLEN - 1) test added by r300442 in the code for the default
case of \c in the prompt format string is a no-op.  We already passed
this test at the top of the loop, and i has not yet been incremented in
this path.  Change this test to (i < PROMPTLEN - 2).

Reported by:	Coverity
CID:		1008328
Reviewed by:	cem
MFC after:	1 week
2016-06-01 16:56:29 +00:00
Warren Block
2770ce607c Clarify the explanations for the hostname and FQDN entries.
MFC after:	1 week
2016-05-31 00:23:29 +00:00
Don Lewis
ae100660d4 Match the descriptions of the \H and \h prompt string sequences to reality.
They were swapped.

X-Confirmed by:	jilles
MFC after:	1 week
2016-05-25 02:10:30 +00:00
Don Lewis
63a4675d89 Hopefully fix Coverity CID 1008328 (Out-of-bounds write) in /bin/sh.
Replace the magic constant 127 in the loop interation count with
"PROMPTLEN - 1".

gethostname() is not guaranteed to NUL terminate the destination
string if it is too short. Decrease the length passed to gethostname()
by one, and add a NUL at the end of the buffer to make sure the
following loop to find the end of the name properly terminates.

The default: case is the likely cause of Coverity CID 1008328.  If
i is 126 at the top of the loop interation where the default case
is triggered, i will be incremented to 127 by the default case,
then incremented to 128 at the top of the loop before being compared
to 127 (PROMPTLENT - 1) and terminating the loop. Then the NUL
termination code after the loop will write to ps[128].  Fix by
checking for overflow before incrementing the index and storing the
second character in the buffer.

These fixes are not guaranteed to satisfy Coverity. The code that
increments i in the 'h'/'H' and 'w'/'W' cases may be beyond its
capability to analyze, but the code appears to be safe.

Reported by:	Coverity
CID:		1008328
Reviewed by:	jilles, cem
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D6482
2016-05-23 01:01:23 +00:00
Bryan Drewery
29df9f6b75 DIRDEPS_BUILD: Run the staged bootstrap-tools version of build-tools.
This avoids running target binaries.

Sponsored by:	EMC / Isilon Storage Division
2016-05-09 22:21:09 +00:00