[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Swift: overflow raises an error
- From: Tim Hill <drtimhill@...>
- Date: Thu, 12 Jun 2014 16:48:46 -0700
On Jun 12, 2014, at 4:24 PM, Jay Carlson <firstname.lastname@example.org> wrote:
> Apple's programming language Swift is defined to raise an error on integer overflow:
> "Unlike arithmetic operators in C, arithmetic operators in Swift do not overflow by default. Overflow behavior is trapped and reported as an error. To opt in to overflow behavior, use Swift’s second set of arithmetic operators that overflow by default, such as the overflow addition operator (&+). All of these overflow operators begin with an ampersand (&)."
> I wonder how much Apple's security experience shaped this feature. I also wonder if people are disabling it via the "-Ofast" compiler switch...
> In any case, it's a notable exception in language design to "ints shall behave like C"--whatever that really means, anyway. :-)
> : https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html
It’s less “ints behave like C” than “ints follow the behavior of the underlying hardware”, which these days almost always means 2’s complement with overflow. I’m really not too sure clamping is better than overflow .. at least with overflow when it happens it’s (usually) pretty obvious; clamping has greater potential to go undetected. (Case in point: at Chernobyl they initially thought radiation was much lower because some of the radiation meters clamped at their maximum level).