News for hackers. Stuff that matters.
Re:Source code of the file with the Zune bug (starts on line... by tlrobinson on Thursday January 01, 2009 @09:17PM (#416922)
Took me a minute to figure out what was wrong. If IsLeapYear() returns true, and "days" equals 366, you get stuck in an infinite loop (you don't modify "days" at all).

This was the first leap year since Zune was released, and yesterday was the 366th day of the year, sooooo....

Also spotted in this code: several "goto"s.

Re:Source code of the file with the Zune bug (starts on line... by thwarted on Thursday January 01, 2009 @10:00PM (#416962)
Normally, gotos wouldn't _necessarily_ be bad, but at least one of these is really really bad.

Consider the function OALIoCtlHalInitRTC, lines 122 and 139-143. The last else block has no obvious terminator, but it looks like the else is empty because of the comment and the indentation. So the line after the label cleanUp is the body of the else. This means that if the if on line 131 is true, the cleanUp code never gets executed. Now this may be intentional (but I doubt it since the label is named cleanUp), but that should be documented in the code.

I consider this use of a goto to be legitimate, since it avoids duplication of the cleanup code (and the last thing you want is to have multiple similar blocks of code that need to be maintained/modified in tandem). This idiom often appears in the linux kernel IIRC. But this code is quite terrible since it doesn't use the idiom correctly, produces an unexpected execution path, is a problem to maintain, and is a problem to read. It seems that it ends up being a no-op since the line after the cleanUp label is a debugging line or something, but that doesn't make this a good practice.

But really, this isn't really a problem with the use of goto, it's a problem with the optional braces on single statement blocks in C. The goto and the label chosen just make it harder to spot. I've made it a habit to use braces around all blocks, even empty ones, to make the intent clearer and avoid this problem.

Re:Source code of the file with the Zune bug (starts on line... by frisco on Friday January 02, 2009 @12:59AM (#417108)
OALIoCtlHalInitRTC -- yeah, there's a great function name.
Re:Source code of the file with the Zune bug (starts on line... by mseebach on Friday January 02, 2009 @09:00AM (#417319)
OAL is obviously a module name, though I can't figure out what it is. "Io Ctl" seems to be something with "IO control", although I can't figure out what IO has to do with it.

The rest reads "HAL (hardware abstraction layer) initialize RTC (real-time clock)".. and that's what it does.

This is from five minutes looking at the source. Very little college embedded programming. What more do you want?

Re:Source code of the file with the Zune bug (starts on line... by JesseAldridge on Friday January 02, 2009 @09:54AM (#417349)
From that function's comments:

// This function is called by WinCE OS to initialize the time after boot.

So how about calling it:

init_time

or maybe

init_time_after_boot

Re:Source code of the file with the Zune bug (starts on line... by sounddust on Friday January 02, 2009 @08:35PM (#418003)
The reason it's not calleld init_time_after_boot - well, first of all Microsoft coding standards call for CamelCase. But secondly, there are several "times", and there are several ways to "initialize" something, and there are several meanings to "boot."

You're either going to have a naming conflict, or you're going to have a bunch of idiots who wanted to initialize a different clock, who start posting to forums saying "i don't understand???! i called init_time waht's wrong???"

Sure, they could call it

SendTheIOCTLCommandToInitializeTheRealTimeClockForTheOEMAbstractionLayer

but sometimes the names of functions are going to be ugly.

Re:Source code of the file with the Zune bug (starts on line... by a-priori on Friday January 02, 2009 @09:16AM (#417327)
Re:Source code of the file with the Zune bug (starts on line... by sounddust on Friday January 02, 2009 @08:36PM (#418007)
OAL is "OEM abstraction layer" and IOCTL probably means that the RTC is set by sending an IOCTL command.
Re:Source code of the file with the Zune bug (starts on line... by palish on Thursday January 01, 2009 @10:47PM (#417003)
...since it avoids duplication of the cleanup code (and the last thing you want is to have multiple similar blocks of code that need to be maintained/modified in tandem).

Isn't that the definition of "when to extract code into its own function"?

Re:Source code of the file with the Zune bug (starts on line... by thwarted on Thursday January 01, 2009 @11:20PM (#417026)
That may be hard to do if it's cleanup code, as the scope changes. Plus, it divorces, visually, the cleanup code from the main body. And there may be additional function call overhead (which may be avoidable by having the compiler in-line it). One reason to use goto is the style that all functions should have one entry and one exit. Even if the cleanup code was put into its own function, you'd still have to ensure that the function was called in all the right places before returning. Without gotos, the logic can become harder to follow through a series of if-else blocks.
Re:Source code of the file with the Zune bug (starts on line... by pmorici on Thursday January 01, 2009 @11:31PM (#417041)
In the case of clean up code where the duplicated code is usually specific to the current scope putting it in a function instead of a goto doesn't buy you anything really.
Re:Source code of the file with the Zune bug (starts on line... by noahlt on Thursday January 01, 2009 @09:58PM (#416960)
A lesson in code smell and good style: I took one look at the code and (before noticing the bug) thought, “why is there an inner if instead of just using a single conditional with a logical AND?”

That is to say, I could have fixed the bug without knowing what it was. (I'm a hs student and don't do much bug-shooting in code that wasn't written by me or my close friends, so I don't know if this is common.)

Re:Source code of the file with the Zune bug (starts on line... by palish on Thursday January 01, 2009 @10:48PM (#417005)
Unfortunately, it's much easier to introduce new bugs when refactoring other people's code.
Re:Source code of the file with the Zune bug (starts on line... by etal on Friday January 02, 2009 @06:38PM (#417896)
C lets a lot of exciting things happen inside an if statement. I once spent a couple hours cleaning up a big, ugly and unfamiliar codebase with transformations like that before I realized I was ignoring some side-effects and was therefore introducing bugs willy-nilly. (Isn't version control great?)