• Pezzah (unregistered)

    Obviously we don't know the starting value for _totalseconds though

  • Jack (unregistered)

    You're assuming that totalseconds gets updated once a second. Highly unlikely, if your "programmers" are anything like ours.

  • Pero.peri (unregistered)

    Wolud it evet crash in say, C#, if there wasn't if?

  • Larry (unregistered)

    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.

  • Andy (unregistered)

    This is why your newfangled overly-fancy "high level" languages are TRWTF. In assembler, a counter automatically wraps around to zero.

  • ThaMe90 (unregistered)

    I wouldn't be surprised _totalSeconds is of a different type than ulong.

  • Gigaplex (unregistered) in reply to Andy
    Andy:
    This is why your newfangled overly-fancy "high level" languages are TRWTF. In assembler, a counter automatically wraps around to zero.
    You're assuming the counter is an unsigned integer and that the CPU doesn't signal arithmetic exceptions for integer overflow.
  • Pero.peri (unregistered) in reply to ThaMe90

    Probably signed long

  • Russ (unregistered)

    Sometimes, you just get a little paranoid.

  • Rob (unregistered)

    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.

  • (cs) in reply to Jack

    _totalseconds is probably a signed long that increments once per loop iteration. "A" for effort though.

  • Phil (unregistered)

    Is that what they mean by defensive programming.

  • B (unregistered)

    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.

  • Charles F. (unregistered) in reply to B
    B:
    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.
    So really, if total seconds is 120, you don't know if it's been running for two minutes or 35 trillion years and two minutes.

    TRWTF would be a work-around specifying that you have to reboot your computer at least once every 34 trillion years. Inconvenient!

  • Harrow (unregistered)

    If I saw this in a program

       _totalSeconds += 1;   // ulong

    without the overflow check, the first thing I would do would be to use the debugger to set

       _totalSeconds
    to 18,446,744,073,709,551,613 and let it run.

    -Harrow.

  • (cs)

    In A.D. 35,096,545,043,317

    War was Beginning.

  • unsigned (unregistered)

    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.

  • Manadar (unregistered)

    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;

  • eVil (unregistered) in reply to Manadar
    Manadar:
    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;

    Depends entirely on the purpose for which you are incrementing the counter. Chances are, whatever YOU think this code is for, it isn't.

  • Foo Bar (unregistered) in reply to B

    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.

  • CHall (unregistered)

    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!

  • bcs (unregistered)

    In most languages, a smart compiler will optimize this out.

  • (cs)

    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....

  • VAXcat (unregistered)

    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.

  • F (unregistered) in reply to bcs
    bcs:
    In most languages, a smart compiler will optimize this out.

    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 ...

  • (cs) in reply to Harrow
    Harrow:
    the first thing I would do would be to use the debugger to set _totalSeconds to 18,446,744,073,709,551,613 and let it run.
    Which would tell you what, exactly?
  • Anonymous (unregistered)

    unchecked { ++_totalSeconds; }

  • Damien (unregistered) in reply to B
    B:
    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.

    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...

  • Rnd( (unregistered)

    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.

  • Anon (unregistered)

    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!

  • Harrow (unregistered) in reply to Vanders
    Vanders:
    Harrow:
    the first thing I would do would be to use the debugger to set _totalSeconds to 18,446,744,073,709,551,613 and let it run.
    Which would tell you what, exactly?
    Three seconds later I would find out what happens to the program when _totalSeconds overflows.

    -Harrow.

  • (cs)

    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

  • (cs) in reply to CHall
    CHall:
    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!
    Help me out here. Are you Zager or Evans?
  • Black Bart (unregistered) in reply to unsigned
    unsigned:
    I'd bet that: a) The variable originally was a 16 bit int or something that actually MIGHT overflow.

    That's true - and may have been originally used on an embedded system where ulong was 16 bits.

  • (cs) in reply to Harrow
    Harrow:
    Three seconds later I would find out what happens to the program when _totalSeconds overflows.
    Do you break out your debugger and start changing random bits in memory just to see what will happen, too?

    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.

  • Anon (unregistered) in reply to da Doctah

    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.

  • petervaz (unregistered) in reply to Charles F.
    Charles F.:
    B:
    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.
    So really, if total seconds is 120, you don't know if it's been running for two minutes or 35 trillion years and two minutes.

    TRWTF would be a work-around specifying that you have to reboot your computer at least once every 34 trillion years. Inconvenient!

    if so, how can we be sure it didn't ran a couple iterations already??!! mind blew

  • luptatum (unregistered)

    In the year 2525, There's every chance the process will still be alive.

  • C-Derb (unregistered)

    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.

  • Freudian Trousers (unregistered) in reply to boh

    Re: A Long Running Process 2013-03-05 09:08 • by boh In A.D. 35,096,545,043,317

    War was Beginning.

    Someone set us up the Auditor!

    captcha: I saluto you!

  • HowItWorks (unregistered)

    Perfectly reasonable code if Alexander is working on the Deep Thought code. In that case ulong would be 1024 bit integer, or longer.

  • Sea Sharp, Waves Hurt (unregistered)

    In AD 35,096,545,043,317, overflow was beginning!

  • (cs) in reply to HowItWorks
    HowItWorks:
    Perfectly reasonable code if Alexander is working on the Deep Thought code. In that case ulong would be 1024 bit integer, or longer.

    Exactly. I expect this program to return "42."

  • Sherman (unregistered) in reply to Anon
    Anon:
    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!
    Wait...what does the u in ulong stand for?
  • (cs) in reply to Harrow
    Harrow:
    Vanders:
    Harrow:
    the first thing I would do would be to use the debugger to set _totalSeconds to 18,446,744,073,709,551,613 and let it run.
    Which would tell you what, exactly?
    Three seconds later I would find out what happens to the program when _totalSeconds overflows.

    -Harrow.

    You would discover that somewhere in the code that wraps this function call is something like this:
      try {
          ...
          if (_totalSeconds < ulong.MaxValue) {  
             _totalSeconds += 1;
          } else {  
             _totalSeconds = 0;  
          }
          ...
      } catch (Exception e) {
        _totalSeconds = 0; // time-space continuum rift
      }
    
  • Anonymous Paranoiac (unregistered) in reply to Sherman
    Sherman:
    Anon:
    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!
    Wait...what does the u in ulong stand for?

    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".

  • (cs) in reply to Sherman
    Sherman:
    Anon:
    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!
    Wait...what does the u in ulong stand for?
    ubiquitous and/or continuum, or perhaps unusually long
  • (cs)

    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.

  • mark (unregistered) in reply to Charles F.
    Charles F.:
    B:
    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.
    So really, if total seconds is 120, you don't know if it's been running for two minutes or 35 trillion years and two minutes.

    TRWTF would be a work-around specifying that you have to reboot your computer at least once every 34 trillion years. Inconvenient!

    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.

  • mark (unregistered) in reply to Charles F.
    Charles F.:
    TRWTF would be a work-around specifying that you have to reboot your computer at least once every 34 trillion years. Inconvenient!

    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?

Leave a comment on “A Long Running Process”

Log In or post as a guest

Replying to comment #:

« Return to Article