News for hackers. Stuff that matters.
Re:Integer Overflow Bug in Boeing 787 Dreamliner by mhogomchungu on Saturday May 02, 2015 @06:49AM (#9476602)
> int64_t ticks; // 100ths of a second

I would go with uint64_t

as it documents "ticks" as a variable that can not hold negative values and also doubles its range of positive values.

Re:Integer Overflow Bug in Boeing 787 Dreamliner by tlb on Saturday May 02, 2015 @07:12AM (#9476655)
I prefer signed time types, because you frequently subtract them. If you use unsigned, then you have to cast the result to signed every time:

  int64_t elapsed = (int64_t)(t1 - t0);
And it's very easy to cause disaster with:

  if ((t1 - t0) > 50) ...
which also succeeds if t1<t0.

While it's theoretically possible, using all 64 bits is tricky and very hard to test.

Re:Integer Overflow Bug in Boeing 787 Dreamliner by Veedrac on Saturday May 02, 2015 @09:22AM (#9476940)
The "doubles its range of positive values" is weak argument because you should never be reaching values more than a few decades, never mind 10-100x the age of the universe. Such a state is a bug.

The "can not hold negative values" argument is also weak because a uint does not prevent generating negative values - it only prevents you from knowing that you've generated negative values. Such a state is a bug.

Using a uint only serves to make it harder to test when your system is in an invalid state.

Re:Integer Overflow Bug in Boeing 787 Dreamliner by speakeron on Saturday May 02, 2015 @06:57AM (#9476616)
I would go with

uint64_t ticksOfDuration10ms; // No comment necessary

Re:Integer Overflow Bug in Boeing 787 Dreamliner by cnvogel on Saturday May 02, 2015 @08:47AM (#9476859)
The concept of timer "ticks" is well established as a unit of time in embeded programming, it's almost universally included in your embedded (realtime-)OS and might increase at any conceivable rate, both limited by the hardware constraints (e.g. a fixed, simple, 16-bit ripple counter that is clocked by the main CPU clock of 8 MHz will clock at 122.07 Hz) or at your application requirements (you let a slightly more configurable timer only count to 40000 at half the CPU clock to get exactly 100 Hz). Hence you shouldn't explicitly inscribe the tick rate in your symbol name, as it can change when requirements change.

You'll almost always have a global variable, preprocessor define... or something similar to get the frequency (or time increase per tick), which you should use whenever you have to convert "ticks" to actual physical units. If the actual effective tick rate is visible at many places in your code, both as a symbol name or as a comment, you are most certainly doing something wrong.

Re:Integer Overflow Bug in Boeing 787 Dreamliner by speakeron on Sunday May 03, 2015 @03:31AM (#9480373)
I think you kind of missed the point of my post (which was a bit tongue-in-cheek). The original code fragment had the tick duration embedded in a comment, so changing a global variable which defines it something other than 10ms is going to cause all sorts of problems in maintaining that code. (Leading possibly to the very problem Boeing had).
Re:Integer Overflow Bug in Boeing 787 Dreamliner by cnvogel on Sunday May 03, 2015 @03:47AM (#9480412)
...well, then my irony-detector is broken ;-).
Re:Integer Overflow Bug in Boeing 787 Dreamliner by tedunangst on Saturday May 02, 2015 @10:02AM (#9477059)
Why not uint64_t thisVariableIncrementsByOneEvery10ms?
Re:Integer Overflow Bug in Boeing 787 Dreamliner by edmccard on Saturday May 02, 2015 @10:28AM (#9477180)
uint64_t thisVariableIncrementsByOneEvery10msSoItWontOverflowForAReallyLongTime

(then you'd know it was safe)