- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Obviously we don't know the starting value for _totalseconds though
Admin
You're assuming that totalseconds gets updated once a second. Highly unlikely, if your "programmers" are anything like ours.
Admin
Wolud it evet crash in say, C#, if there wasn't if?
Admin
I can just about guarantee they never tested both sides of the condition.
Captcha: minim - you'd need to leave it running for a minim or two waiting for the counter to overflow.
Admin
This is why your newfangled overly-fancy "high level" languages are TRWTF. In assembler, a counter automatically wraps around to zero.
Admin
I wouldn't be surprised _totalSeconds is of a different type than ulong.
Admin
Admin
Probably signed long
Admin
Sometimes, you just get a little paranoid.
Admin
Can the data be set from somewhere else? If so, this is the right thing to do, all your objections withstanding.
"It can never happen" is the cause of many security vulnerabilities.
Admin
_totalseconds is probably a signed long that increments once per loop iteration. "A" for effort though.
Admin
Is that what they mean by defensive programming.
Admin
TRWTF is that 2^64 seconds is only 584,542,046,091 years. 35,096,545,043,317 AD is closer to 2^70 seconds away.
Admin
TRWTF would be a work-around specifying that you have to reboot your computer at least once every 34 trillion years. Inconvenient!
Admin
If I saw this in a program
without the overflow check, the first thing I would do would be to use the debugger to set
to 18,446,744,073,709,551,613 and let it run.-Harrow.
Admin
In A.D. 35,096,545,043,317
War was Beginning.
Admin
I'd bet that: a) The variable originally was a 16 bit int or something that actually MIGHT overflow. b) someone aded this code as a quick and dirty patch. c) the variable was refactored to ulong. d) the patch was updated by a (silicon or meat) tool.
Admin
TRWTF is still incrementing a seconds valuable by one every second. You should always calculate the difference in seconds between the start datetime and the current datetime.
var secondsElapsed = (start - DateTime.Now).TotalSeconds;
Admin
Admin
Only 584 gigayears? Well that's a different story. If this program is multithreaded, and the totalseconds counter has its own dedicated CPU core, it might overflow in as little as 300 years.
Admin
And, in the year 1,000,000 AD, you'll hear someone say, "Oops. I restarted it." All that incrementation throughout most of human history, lost in the blink of an eye!
Admin
In most languages, a smart compiler will optimize this out.
Admin
Are we sure that ulong.MaxValue isn't used as a "special" value? Maybe the counter is initialised to MaxValue to indicate the start condition, and this tests for the start condition and zeroes the count? A bit agricultural, but I've seen similar.
Maybe the processor board is a dodgy design, and memory corruptions are common, so _totalSeconds can get corrupted? (Yes, I've worked on hardware where you can't trust the contents of RAM).
On the whole, the coder has clearly considered what happens when things wrap; and I prefer that to the alternative....
Admin
That reminds me of some VMS code I had to debug after it had Y2K problems. The developer had put in extensive support for 5 digit years, proudly assuming his application would still be in use and running after the year 10,000, but he hadn't been clever enough to get it to work correctly after Y2K rolled around.
Admin
Most? In most languages, this won't compile because the syntax is quite different (COBOL? FORTRAN? APL? Intercal? Brainfuck?). I suppose you could construe that as optimizing out ...
Admin
Admin
unchecked { ++_totalSeconds; }
Admin
Actually, it's quite a few years more than that. You've used the approximation of 365.25 days per year, which ignores the century leap year rule...
Admin
So for good desing practises for how long in to future should the program be coded and tested for? Will century be good or should we aim to around 10^110 years, which should be sufficient margin after heathdeath of universe.
Admin
But what if we hit that ubiquitous rip in the space/time continuum and suddenly time ran backwards so that the ulong went negative? Ka-POOF!
Admin
-Harrow.
Admin
ulong.MaxValue/31557600 ~ 5.8E11
This is the Coalescence of Local Group step in universe timeline. Stars will only stop forming around 1E14 Universe will be at low energy around 1E100
Admin
Admin
That's true - and may have been originally used on an embedded system where ulong was 16 bits.
Admin
We can read the code and see what will happen: the only question is will your condition ever occur normally? If _totalSeconds counts from 0 when the process starts, for example, the chances of it still being running in 5.5billion years seems unlikely.
Admin
Whoa...talk about your time warps! Haven't heard a reference to that song in...well a ulong would have incremented quite a ways since then.
Admin
Admin
In the year 2525, There's every chance the process will still be alive.
Admin
I think you guys are all wrong. You are all assuming that _totalSeconds is related to time. I think this was pulled out of the Home Town Buffet code base, and given the number of locations they have in the South, this probably rolls over every couple of months.
Admin
Someone set us up the Auditor!
captcha: I saluto you!
Admin
Perfectly reasonable code if Alexander is working on the Deep Thought code. In that case ulong would be 1024 bit integer, or longer.
Admin
In AD 35,096,545,043,317, overflow was beginning!
Admin
Exactly. I expect this program to return "42."
Admin
Admin
Admin
Obviously, it's short for "universal". It's so that anywhere you adventure in the universe, the integer will still be long.
There's also a very small chance it means "unsigned".
Admin
Admin
Are you sure it is counting SECONDS. Regardless of the comments, or the variable names, it could be counting picoseconds or some such.
You never know.
Of course countering overflow on unsigned variables IS kinda stupid.
Admin
Whoa then! If it wasn't going to matter for 35 trillion years, that would be one thing. But only 584 billion, well, that's another story.
Admin
Yeah, it's like when Windows automatically downloads an update and then tells you that it's going to automatically reboot your computer in 34 trillion years. And I always say, Hold on, I'm in the middle of something here! Is there some way to abort that reboot?