after enough complaints by POSIX sh advocates,

• make parsing numbers with leading digit-zero as octal independent of
  mksh/lksh and dependent on set -o posix; adjust manpages to match
• warn about these changes and why mksh uses 32-bit consistent arithmetics
  and point people to lksh for host-long undefined-behaviour arithmetics
• point out, explicitly, that it is *legal* for the operating environment
  to make 'print $((2147483647 + 1))' (on a 32-bit system; adjust for a
  64-bit system) to run 'rm -rf ~ /' instead
This commit is contained in:
tg 2013-04-27 18:50:25 +00:00
parent 496988e27a
commit dc94c3d205
5 changed files with 59 additions and 39 deletions

View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/Makefile,v 1.119 2013/04/27 18:22:47 tg Exp $
# $MirOS: src/bin/mksh/Makefile,v 1.120 2013/04/27 18:50:21 tg Exp $
#-
# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
# 2011, 2012, 2013
@ -104,6 +104,9 @@ test-build-lksh: .PHONY
cd ${.CURDIR} && exec ${MAKE} lksh.cat1 test-build \
_TBF=-L USE_PRINTF_BUILTIN=0
bothmans: .PHONY
cd ${.CURDIR} && exec ${MAKE} MAN='lksh.1 mksh.1' __MANALL
cleandir: clean-extra
clean-extra: .PHONY
@ -132,7 +135,7 @@ CLEANFILES+= ${MAN:S/$/.txt/} ${MAN:S/$/.txt.gz/}
CATS_KW= mksh, ksh, sh
CATS_TITLE_mksh_1=mksh - The MirBSD Korn Shell
cats: ${MANALL} ${MANALL:S/.cat/.ps/}
.if "${MANALL:Nmksh.cat1}" != ""
.if "${MANALL:Nlksh.cat1:Nmksh.cat1}" != ""
. error Adjust here.
.endif
.for _m _n in mksh 1

25
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.607 2013/04/26 21:22:42 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.608 2013/04/27 18:50:21 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -3779,22 +3779,17 @@ expected-stdout:
---
name: integer-base-check-flat
description:
Check behaviour does not match POSuX, because a not type-safe
scripting language has *no* business interpreting "010" as octal
category: shell:legacy-no
Check behaviour does not match POSuX (except if set -o posix),
because a not type-safe scripting language has *no* business
interpreting the string "010" as octal numer eight (dangerous).
stdin:
echo :$((10)).$((010)).$((0x10)).
echo 1 "$("$__progname" -c 'echo :$((10))/$((010)),$((0x10)):')" .
echo 2 "$("$__progname" -o posix -c 'echo :$((10))/$((010)),$((0x10)):')" .
echo 3 "$("$__progname" -o sh -c 'echo :$((10))/$((010)),$((0x10)):')" .
expected-stdout:
:10.10.16.
---
name: integer-base-check-flat-legacy
description:
Check behaviour matches POSuX for LEGACY KSH
category: shell:legacy-yes
stdin:
echo :$((10)).$((010)).$((0x10)).
expected-stdout:
:10.8.16.
1 :10/10,16: .
2 :10/8,16: .
3 :10/10,16: .
---
name: integer-base-check-numeric-from
description:

33
lksh.1
View File

@ -1,7 +1,22 @@
.\" $MirOS: src/bin/mksh/lksh.1,v 1.1 2013/04/27 18:13:58 tg Exp $
.\" $MirOS: src/bin/mksh/lksh.1,v 1.2 2013/04/27 18:50:24 tg Exp $
.\"-
.\" Copyright (c) 2008, 2009, 2010, 2012, 2013
.\" Thorsten “mirabilos” Glaser <tg@mirbsd.org>
.\"
.\" Provided that these terms and disclaimer and all copyright notices
.\" are retained or reproduced in an accompanying document, permission
.\" is granted to deal in this work without restriction, including un
.\" limited rights to use, publicly perform, distribute, sell, modify,
.\" merge, give away, or sublicence.
.\"
.\" This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
.\" the utmost extent permitted by applicable law, neither express nor
.\" implied; without malicious intent or gross negligence. In no event
.\" may a licensor, author or contributor be held liable for indirect,
.\" direct, other damage, loss, or other issues arising in any way out
.\" of dealing in the work, even if advised of the possibility of such
.\" damage or existence of a defect, except proven that it results out
.\" of said persons immediate fault when using the work as intended.
.\"-
.\" Try to make GNU groff and AT&T nroff more compatible
.\" * ` generates in gnroff, so use \`
@ -149,11 +164,15 @@
.Ek
.Sh DESCRIPTION
.Nm
is a command interpreter intended exclusive for running legacy
is a command interpreter intended exclusively for running legacy
shell scripts.
It is built on
.Nm mksh ;
refer to its manual page for details on the scripting language.
It is recommended to port scripts to
.Nm mksh
instead of relying on legacy or idiotic POSIX-mandated behaviour,
since the MirBSD Korn Shell scripting language is much more consistent.
.Sh LEGACY MODE
.Nm
has the following differences from
@ -212,9 +231,6 @@ unlike
.Nm ksh ,
does not keep file descriptors \*(Gt 2 private.
.It
.Nm
parses leading-zero numbers as octal (base 8).
.It
Integers use the host C environment's
.Vt long
type, not
@ -246,13 +262,6 @@ tries to make a cross between a legacy bourne/posix compatibl-ish
shell and a legacy pdksh-alike but
.Dq legacy
is not exactly specified.
Parsing numbers with leading zero digits or
.Dq 0x
is relatively recent in all
.Nm pdksh
derivates, but supported here for completeness.
It might make sense to make this a run-time option, but
that might also be overkill.
.Pp
The
.Ic set

27
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.310 2013/04/26 21:22:47 tg Exp $
.\" $MirOS: src/bin/mksh/mksh.1,v 1.311 2013/04/27 18:50:24 tg Exp $
.\" $OpenBSD: ksh.1,v 1.146 2013/03/18 11:10:52 mpi Exp $
.\"-
.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
@ -74,7 +74,7 @@
.\" with -mandoc, it might implement .Mx itself, but we want to
.\" use our own definition. And .Dd must come *first*, always.
.\"
.Dd $Mdocdate: April 26 2013 $
.Dd $Mdocdate: April 27 2013 $
.\"
.\" Check which macro package we use, and do other -mdoc setup.
.\"
@ -2563,9 +2563,14 @@ Additionally, base-16 integers may be specified by prefixing them with
in all forms of arithmetic expressions, except as numeric arguments to the
.Ic test
built-in command.
It is discouraged to prefix numbers with a sole zero
.Pq Sq 0 ,
because some shells may interpret them as base-8 integers.
Prefixing numbers with a sole digit zero
.Pq Sq 0
leads to the shell interpreting it as base-8 integer in
.Ic posix
mode
.Em only ;
historically, (pd)ksh has never done so either anyway,
and it's unsafe to do that, but POSIX demands it nowadays.
As a special
.Nm mksh
extension, numbers to the base of one are treated as either (8-bit
@ -6334,6 +6339,16 @@ Use co-processes instead.
foo \*(Ba bar \*(Ba read baz # will not change $baz
foo \*(Ba bar \*(Ba& read \-p baz # will, however, do so
.Ed
.Pp
.Nm mksh
provides a consistent set of 32-bit integer arithmetics, both signed
and unsigned, with defined wraparound and sign of the result of a modulo
operation, even (defying POSIX) on 64-bit systems.
If you require 64-bit integer arithmetics, use
.Nm lksh Pq legacy mksh
instead, but be aware that, in POSIX, it's legal for the OS to make
.Li print $((2147483647 + 1))
delete all files on your system, as it's Undefined Behaviour.
.Sh BUGS
Suspending (using \*(haZ) pipelines like the one below will only suspend
the currently running part of the pipeline; in this example,
@ -6345,7 +6360,7 @@ $ /bin/sleep 666 && echo fubar
.Ed
.Pp
This document attempts to describe
.Nm mksh\ R45
.Nm mksh\ R46
and up,
compiled without any options impacting functionality, such as
.Dv MKSH_SMALL ,

6
var.c
View File

@ -27,7 +27,7 @@
#include <sys/sysctl.h>
#endif
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.170 2013/04/07 14:11:54 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.171 2013/04/27 18:50:25 tg Exp $");
/*-
* Variables
@ -495,14 +495,12 @@ getint(struct tbl *vp, mksh_ari_u *nump, bool arith)
base = 16;
have_base = true;
}
#ifdef MKSH_LEGACY_MODE
if (arith && s[0] == '0' && ksh_isdigit(s[1]) &&
if (Flag(FPOSIX) && arith && s[0] == '0' && ksh_isdigit(s[1]) &&
!(vp->flag & ZEROFIL)) {
/* interpret as octal (deprecated) */
base = 8;
have_base = true;
}
#endif
while ((c = *s++)) {
if (c == '-') {
neg = true;