• finally, the code and manual page text to deprecate, and code to not

handle any more, octal 010 style constants, as promised
• overhaul the manpage re. arithmetic expressions, make the guarantees
  mksh code has explicitly, precisely, clear
• to reduce burden of the compiler, getint() now operates on mksh_uari_t
  internally; it already applied the sign after operation, anyway (C99
  guarantees wraparound on unsigned types, but for signed types we need
  specific compiler support; apparently, this comes from hardware limits)
• use const and shuffle order of locals around while here
This commit is contained in:
tg 2011-12-10 13:34:19 +00:00
parent f8098a7f48
commit 3b87d173d4
4 changed files with 35 additions and 40 deletions

View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.500 2011/12/09 20:40:02 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.501 2011/12/10 13:34:14 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 $
@ -28,7 +28,7 @@
# http://www.freebsd.org/cgi/cvsweb.cgi/src/tools/regression/bin/test/regress.sh?rev=HEAD
expected-stdout:
@(#)MIRBSD KSH R40 2011/12/09
@(#)MIRBSD KSH R40 2011/12/10
description:
Check version of shell.
stdin:
@ -3603,6 +3603,7 @@ expected-stdout:
name: integer-base-check-flat-posix
description:
Check behaviour of POSuX bases
category: !nodeprecated
stdin:
echo :$((10)).$((010)).$((0x10)).
expected-stdout:
@ -3612,6 +3613,7 @@ name: integer-base-check-flat-right
description:
Check behaviour does not match POSuX, because a not type-safe
scripting language has *no* business interpreting "010" as octal
category: nodeprecated
stdin:
echo :$((10)).$((010)).$((0x10)).
expected-stdout:

37
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.280 2011/12/03 00:09:15 tg Exp $
.\" $MirOS: src/bin/mksh/mksh.1,v 1.281 2011/12/10 13:34:16 tg Exp $
.\" $OpenBSD: ksh.1,v 1.141 2011/09/03 22:59:08 jmc Exp $
.\"-
.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
@ -72,7 +72,7 @@
.\" with -mandoc, it might implement .Mx itself, but we want to
.\" use our own definition. And .Dd must come *first*, always.
.\"
.Dd $Mdocdate: December 3 2011 $
.Dd $Mdocdate: December 10 2011 $
.\"
.\" Check which macro package we use
.\"
@ -2465,14 +2465,8 @@ Grouping operators:
( )
.Ed
.Pp
Integer constants and expressions are calculated using the
.Vt mksh_ari_t
.Po if signed Pc
or
.Vt mksh_uari_t
.Po if unsigned Pc
type, and are limited to 32 bits.
Overflows wrap silently.
Integer constants and expressions are calculated using an exactly 32-bit
wide, signed or unsigned, type with silent wraparound on integer overflow.
Integer constants may be specified with arbitrary bases using the notation
.Ar base Ns # Ns Ar number ,
where
@ -2480,22 +2474,15 @@ where
is a decimal integer specifying the base, and
.Ar number
is a number in the specified base.
Additionally,
integers may be prefixed with
.Sq 0X
or
.Sq 0x
(specifying base 16), similar to
.At
.Nm ksh ,
or
.Sq 0
(base 8), as an
.Nm
extension, in all forms of arithmetic expressions,
except as numeric arguments to the
Additionally, base-16 integers may be specified by prefixing them with
.Sq 0x Pq case-insensitive
in all forms of arithmetic expressions, except as numeric arguments to the
.Ic test
command.
built-in command.
Base-8 integers may be specified by prefixing them with
.Sq 0 ,
but this is deprecated and will be removed in the next version of
.Nm .
As a special
.Nm mksh
extension, numbers to the base of one are treated as either (8-bit

4
sh.h
View File

@ -151,9 +151,9 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.509 2011/12/09 20:40:26 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.510 2011/12/10 13:34:18 tg Exp $");
#endif
#define MKSH_VERSION "R40 2011/12/09"
#define MKSH_VERSION "R40 2011/12/10"
/* arithmetics types */
typedef int32_t mksh_ari_t;

28
var.c
View File

@ -26,7 +26,7 @@
#include <sys/sysctl.h>
#endif
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.136 2011/11/26 00:45:03 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.137 2011/12/10 13:34:19 tg Exp $");
/*-
* Variables
@ -469,10 +469,10 @@ setint(struct tbl *vq, mksh_ari_t n)
static int
getint(struct tbl *vp, mksh_ari_t *nump, bool arith)
{
char *s;
int c, base, neg;
mksh_uari_t num;
const char *s;
bool have_base = false;
mksh_ari_t num;
if (vp->flag&SPECIAL)
getspec(vp);
@ -487,6 +487,13 @@ getint(struct tbl *vp, mksh_ari_t *nump, bool arith)
base = 10;
num = 0;
neg = 0;
#ifdef MKSH_DISABLE_DEPRECATED
if (arith && s[0] == '0' && (s[1] | 0x20) == 'x') {
s += 2;
base = 16;
have_base = true;
}
#else
if (arith && *s == '0' && *(s+1)) {
s++;
if (*s == 'x' || *s == 'X') {
@ -499,19 +506,20 @@ getint(struct tbl *vp, mksh_ari_t *nump, bool arith)
base = 8;
have_base = true;
}
for (c = *s++; c ; c = *s++) {
#endif
while ((c = *s++)) {
if (c == '-') {
neg++;
continue;
} else if (c == '#') {
base = (int)num;
if (have_base || base < 1 || base > 36)
if (have_base || num < 1 || num > 36)
return (-1);
base = (int)num;
if (base == 1) {
unsigned int wc;
if (!UTFMODE)
wc = *(unsigned char *)s;
wc = *(const unsigned char *)s;
else if (utf_mbtowc(&wc, s) == (size_t)-1)
/* OPTU-8 -> OPTU-16 */
/*
@ -519,7 +527,7 @@ getint(struct tbl *vp, mksh_ari_t *nump, bool arith)
* the same as 1#\x80 does, thus is
* not round-tripping correctly XXX)
*/
wc = 0xEF00 + *(unsigned char *)s;
wc = 0xEF00 + *(const unsigned char *)s;
*nump = (mksh_ari_t)wc;
return (1);
}
@ -538,9 +546,7 @@ getint(struct tbl *vp, mksh_ari_t *nump, bool arith)
return (-1);
num = num * base + c;
}
if (neg)
num = -num;
*nump = num;
*nump = neg ? -((mksh_ari_t)num) : (mksh_ari_t)num;
return (base);
}