overflow behavior differs from other shells

Bug #1716980 reported by Elliott Hughes
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
mksh
Invalid
Undecided
Unassigned

Bug Description

$ mksh -c 'echo $((0x7fffffff + 1))'
-2147483648

versus:

$ dash -c 'echo $((0x7fffffff + 1))'
2147483648
$ bash -c 'echo $((0x7fffffff + 1))'
2147483648
$ busybox sh -c 'echo $((0x7fffffff + 1))'
2147483648
$ zsh -c 'echo $((0x7fffffff + 1))'
2147483648

Revision history for this message
Elliott Hughes (enh-9) wrote :

(this seen with R55 but also at least as far back as R46, so not a recent regression.)

Revision history for this message
Thorsten Glaser (mirabilos) wrote :

Not a bug, everything works as intended and documented.

More helpful answer:

If you want POSIX semantics for overflow (have the shell use the host C “long” data type for arithmetics), use the “lksh” binary (l as in long, legacy), see the lksh(1) manpage.

However *do* be aware that 'echo $((0x7FFFFFFF + 1))' on a system with 32-bit long, with POSIX semantics (lksh, GNU bash, dash, …) is ALLOWED TO REMOVE ALL DATA (equal 'rm -rf ~ /') or otherwise crash the system due to ISO C99 Undefined Behaviour. DO NOT DO THAT!

Changed in mksh:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.