#include <stdint.h>
#include <iostream>
#include <sys/time.h>
int
main(int argc, char **argv)
{
struct timeval tv;
tv.tv_sec = 1185410974;
tv.tv_usec = 0;
std::cout << tv.tv_sec << std::endl;
std::cout << (tv.tv_sec * 1000) << std::endl;
std::cout << ((uint64_t) (tv.tv_sec * 1000)) << std::endl;
std::cout << ((uint64_t) tv.tv_sec * 1000) << std::endl;
}

Loading...
Gary Benson | 26-Jul-07 at 12:11 am | Permalink
Still non the wiser!
liava | 26-Jul-07 at 12:24 am | Permalink
Ouch
csm | 26-Jul-07 at 11:14 pm | Permalink
To explain:
At 1185410974 seconds past the epoch (Jan 1, 1970 or so), one thousand times that value not only overflows a signed 32-bit integer, but it will wrap around at that point from a negative integer to a positive integer.
OK. Now, say you had a timer class based on milliseconds since the epoch. You make timers that “expire” by adding a millisecond value to “now.” You start to notice that on July 25, 2007, that timers scheduled twelve hours from now start firing immediately.
What’s going on?
Well, at Wed Jul 25 17:49:34 PDT 2007, that time in milliseconds overflowed a 32-bit integer, and wrapped around to a positive integer again. This means that if “now” was before that time, and “now plus 12 hours” was after that time, that “now” would always be greater than “now plus 12 hours,” IF you, say, happened to have a bug where the seconds value was cast from a 32 bit signed int, which you multiplied by 1000, then cast to a 64 bit integer, that times before then would be very very large (taking into account sign extension), and values after then would be very, very small.
Now say you had an event fire every 12 hours (in theory). Bad things would happen on that day, and that day only, where “now” was always immediately larger than “now plus 12 hours.”
(note, no proof yet that this caused any power failures…)
Gary Benson | 27-Jul-07 at 4:42 am | Permalink
Nice