Posted By: C9SpotTheBug | Feb 24th, 2005 @ 4:42 PM
page 1 of 1
Comments: 13 | Views: 20935
A few of us got together (Charles and the two people behind a similar developer education project that has been happening internally for a while: Jon Pincus and Hunter Hudson) and decided that it would be great to start a new "game" on C9 where a small snippet of flawed code will be posted, the thread locked down, people email what they've uncovered and then after a few days (or whenever the flaws outlined in the email messages have been condensed) the flaws in the code will be revealed and the thread unlocked for discussion.

By the way, you will meet Jon Pincus and Hunter Hudson soon, in the usual C9 way: on video.

Here's the basic idea:

Look for the flaws in the code snippet and then mail what you think the problems are to the email address listed at the bottom of the post.  [Update on Feb 28: now that the initial answer is posted, please just post your comments to the thread rather than emailing.]

Why are we doing it this way? Well, unlike general puzzles, we want to give everybody enough time to come up with their own ideas regarding flaws in pattern, design, testability, globalization/localization, security, etc. Code languages will vary (C to vbscript to perl..) as well as complexity levels (easy to really hard).

After a few days, we will release the results of your mails and unlock the thread for lively discussion.

Here is flaw # 1 of Channel 9 Spot the Bug. Enjoy!
This should be fun Smiley

-----------------------------------------------------


Assume that the comment is a correct specification of the function's behavior.  As background, GetTickCount is a Win32 function which retrieves the number of milliseconds that have elapsed since the system was started (limited to the resolution of the system timer).

There are at least two problems here.

<BR><BR>/* generate random number in range LowerBound ... UpperBound - 1 */<BR><BR><BR><BR>ULONG GenerateRandom( ULONG LowerBound, ULONG UpperBound )<BR>{<BR>    static LONG seed = 0;<BR>    ULONG rand;<BR><BR>    /* initialize first time through */<BR><BR>    if (seed == 0)<BR>    {<BR>        seed = GetTickCount();<BR>    }<BR>  <BR>    seed *= (0x41C64E6D + 0x3039);<BR>    rand = (LowerBound + (seed & 0x7FFFFFFF)) % UpperBound;<BR><BR>    return rand;<BR>}<BR><BR>


-----------------------------------------------------

See the first reply for some initial answers; and feel free to jump into the discussion.  If you've got any comments or suggestions, please send them here!

Spot those bugs!

Love,

The C9 Spot the Bug team
Maurits
Maurits
AKA Matthew van Eerde
Perhaps a better way to implement it... without assuming 32-bit longs... would have been to use a GenerateRandomByte() helper function.  Then concatenate enough random bytes to get you up over (UBound - LBound), at least in mask.  GenerateRandomByte() could pull the inner binary digits from a seed-generated random long

EDIT:
no that would favor small values if UBound - LBound was slightly less than a power of 2...
Shaded
Shaded
Mean ugly geek with axe
I do appreciate the opportunity to participate in this challenge.  Thank you for doing such a great job!

I am excited to see I understood this problem so well even though I consider myself a novice.

It was extremely helpful to see I caught some of the advanced issues of this code, and also made some of the common errors everyone else made.

My competitive nature wants to know how many people submitted answers.  Could you provide this interesting (although mostly useless) information?

If not, I completely understand, but it does not hurt to ask.  (usually)

Thanks again,


Shaded
iStation
iStation
Fuujin
Thank you for nice refreshment!
I enjoyed a lot!
What's next?
Smiley
Charles
Charles
Welcome Change

We received 29 replies. Thanks!

Next?

Stay tuned Smiley


Charles

NeoTOM
NeoTOM
OMG WTF REDESIGN
Ulong? Isn't that that shape-shifting pig from Dragonball?
Maurits
Maurits
AKA Matthew van Eerde
No, that's Oolong (named after the tea, I suppose Smiley
Not to be confused w/ Poolo
C9SpotTheBug wrote:

A. Several repsonders suggested that the static variable would get reinitialized each time the function is called. No; whether this is C or C++ code, static and global variables are initialized at program startup time. Is the explicit initialization needed? Microsoft compilers (and I believe gcc as well) do initialze statics and globals to zero, but if you're writing code that needs to work on arbitrary C compilers, a little web searching will reveal that this is something you can't necessarily rely on; and in any case, I would say that putting it in helps communication by pointing out that it actually does matter that the seed starts at zero.



I was aware of that, but given what you said in 5 and what Herb Sutter has recently been telling us about concurrency, under what situation in 2005 would you think that a function level static is acceptable? Or is it always a bug Smiley
ScanIAm
ScanIAm
On a scale of 1 to 10, people are stupid.
Yay!  I got a couple of them.

Of course, it's a bit dubious as to whether the bug submitters (charles, et. al) actually knew all the bugs Smiley  I know I'll be looking at the multi-threading point of view on the next bug.
Charles
Charles
Welcome Change
Rossj wrote:
C9SpotTheBug wrote:

A. Several repsonders suggested that the static variable would get reinitialized each time the function is called. No; whether this is C or C++ code, static and global variables are initialized at program startup time. Is the explicit initialization needed? Microsoft compilers (and I believe gcc as well) do initialze statics and globals to zero, but if you're writing code that needs to work on arbitrary C compilers, a little web searching will reveal that this is something you can't necessarily rely on; and in any case, I would say that putting it in helps communication by pointing out that it actually does matter that the seed starts at zero.



I was aware of that, but given what you said in 5 and what Herb Sutter has recently been telling us about concurrency, under what situation in 2005 would you think that a function level static is acceptable? Or is it always a bug


It is always a "bug" to write code that is not threadsafe and not let consumers "know" this.

What Herb is talking about is the general complexity of writing correct concurrent code (you will hear a lot more about this here on Channel 9 this month).

Charles
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
Maurits wrote:
No, that's Oolong (named after the tea, I suppose
Not to be confused w/ Poolo

Actually that last one is commonly romanized as Pu'ar (real name プーアル 'puuaru'), also a type of Chinese tea. Smiley
Maurits
Maurits
AKA Matthew van Eerde
And she's a cat... "purr"... Smiley
l's and r's are kind of ambiguous in Japanese.
page 1 of 1
Comments: 13 | Views: 20935
Microsoft Communities