[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Swift: overflow raises an error
- From: Coda Highland <chighland@...>
- Date: Thu, 12 Jun 2014 16:59:55 -0700
On Thu, Jun 12, 2014 at 4:48 PM, Tim Hill <drtimhill@gmail.com> wrote:
>
> On Jun 12, 2014, at 4:24 PM, Jay Carlson <nop@nop.com> 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 (&)."[1]
>>
>> 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. :-)
>>
>> Jay
>>
>> [1]: 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).
>
> —Tim
It says it throws an error. That's not clamping, and it will notify
the developer that it's happened.
/s/ Adam