Re: [PATCH 0/3] Use POSIX "$((..))" instead of bashism "$[...]"

From: Thorsten Glaser
Date: Thu Jun 13 2013 - 15:31:39 EST

Geert Uytterhoeven dixit:

>Should these be truncated to 32-bit explicitly, or is this a bug in mksh?

mksh in âmksh modeâ operates specifically on 32-bit integer data
types with defined wraparound and other guarantees beyond what
POSIX does. There is an âlkshâ binary in the mksh binary package
in Debian and derivates now, which is the âlegacy modeâ that uses
the host systemâs âlongâ data type, as POSIX demands, and is now
(mksh_46-2) used for /bin/sh.

However, on mixed 32/64-bit systems, /bin/sh can have either bit
size. Additionally, it is not possible, in POSIX, without invoking
ISO C âUndefined Behaviourâ, to find out the bitsize of the shell

Furthermore, Linux can be cross-compiled, so when building kernels
for 64-bit platforms on 32-bit systems, the arithmetics used MUST
NOT overflow beyond a signed 32-bit âlongâ.

> [1/3] h8300/boot: Use POSIX "$((..))" instead of bashism "$[...]"
> [2/3] ARM: shmobile: Use POSIX "$((..))" instead of bashism "$[...]"
> [3/3] sh/boot: Use POSIX "$((..))" instead of bashism "$[...]"

Independent of the above, Iâve verified all three and can state
that they
â are no regression relative to existing behaviour
â do not invoke any features not in POSIX $((â)) arithmetics
â do not invoke any features not in mksh $((â)) arithmetics

This means you can add my
Signed-off: Thorsten Glaser <tg@xxxxxxxxxx>

However, I urge you to check whether any of these arithmetics
can go beyond 32 bit. If they have even the slightest chance
to do that, you MUST replace them by something different. One
method could be to use bc(1):

$(shell printf 'obase=16\nibase=16\n%s+%s\n' $(FOO) $(BAR) | bc)

Another method could be to operate on the upper half and the
lower half of the 64-bit quantities separately, assuming that
calculations do not overflow (in POSIX sh, overflow is, like
in ISO C, Undefined Behaviour; a C compiler is permitted to
compile the source code in the shell that invokes it to run
ârm -rf ~ /â instead) or carry over (e.g. if itâs known that
the first half is always 0x7FFFFFFF or the last half always
00000000 you can just add that as âstringâ).

FWIW, I'm quite impressed with mksh interactively. I thought it was much
*much* more bare bones. But it turns out it beats the living hell out of
ksh93 in that respect. I'd even consider it for my daily use if I hadn't
wasted half my life on my zsh setup. :-) -- Frank Terbeck in #!/bin/mksh
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at