Page 1 of 1

Please Help!

Posted: Sat Mar 07, 2009 6:02 pm
by Chaospandion
I can't figure out whats wrong with my code!
It keeps crashing our production servers every few minutes.

//Clear the string from memory
int i;
int* p = &i;

while (i < 1000000000)
{
    p++;
    (*p) = 0;
    i++;
}

Re: Please Help!

Posted: Sat Mar 07, 2009 8:30 pm
by just brew it!
You've only allocated a single int variable, but are trying to write zeros to approximately 4 GB of RAM starting at the address of that variable. It is not at all surprising that bad things are happening; you've basically created the mother of all buffer overflows. Depending on how much swap space is available, yes this could potentially bring the entire system to its knees.

I can't even guess at how to "fix" this, since you haven't explained what it is actually supposed to be doing. The comment isn't helpful, since I can't tell from the given code fragment what string it is supposed to be clearing.

Edit: You've also failed to initialize the variable i before using it. But this code would likely cause bad things to happen regardless.

Re: Please Help!

Posted: Sun Mar 08, 2009 4:21 pm
by notfred
Always pay attention to any warnings from the compiler and anything static analysis tools tell you. Yes, there are a few false positives, but unless you can state exactly why it is correct and why the SA tool is wrong, it's probably your code in the wrong. Just about every SA tool I know would have a fit over that code.

Re: Please Help!

Posted: Sun Mar 08, 2009 5:30 pm
by Flying Fox
The comment and the code do not jive for me. If you are "clearing a string", why is an integer pointer used? Correctness aside, you run the risk of all sorts of misalignments and other weird stuff. Considering yourself lucky nothing else other than crashes is happening. (ok, just a bit of exaggeration :P)

That POS piece of code has no place in a production server. Besides you don't need such a stupid loop just to clear memory, that's what memset() is for.

If the guy who wrote that code (I hope it is not you) is still around, go beat him up. :lol:

Re: Please Help!

Posted: Tue Mar 17, 2009 11:56 pm
by Chaospandion
:D

I hope I didn't scare anyone with this.
I was just trying to show the power that C holds in the hand of the dim witted.

Re: Please Help!

Posted: Wed Mar 18, 2009 12:10 am
by just brew it!
Any programming language or tool that doesn't put you in a straightjacket can cause trouble if misused or abused. With power comes responsibility.

Re: Please Help!

Posted: Wed Mar 18, 2009 9:07 am
by notfred
I've worked on high level Java projects and low-level C projects with idiots on both. In a high level Java project, the idiots' poor code caused performance issues but didn't cause crashes so they got to commit it and I got to debug it :-( On the low-level C projects the idiots' poor code blew up all over the place so they didn't get to commit :-) I like working in C with strict commit permissions (no compiler warnings, no static analysis warnings), it keeps the idiots out of the code base.

Re: Please Help!

Posted: Wed Mar 18, 2009 9:20 am
by Chaospandion
Is it just me or is everyone very serious on this board?
When I saw this code the second time I had to laugh at how retarded it was.

Re: Please Help!

Posted: Wed Mar 18, 2009 1:26 pm
by titan
Chaospandion wrote:
Is it just me or is everyone very serious on this board?
When I saw this code the second time I had to laugh at how retarded it was.

Well, to be fair, we thought you were serious, so serious replies were required.

Re: Please Help!

Posted: Wed Mar 18, 2009 7:21 pm
by Flying Fox
titan wrote:
Chaospandion wrote:
Is it just me or is everyone very serious on this board?
When I saw this code the second time I had to laugh at how retarded it was.

Well, to be fair, we thought you were serious, so serious replies were required.

Chaospandion wrote:
It keeps crashing our production servers every few minutes.
When you said this is from production servers of course we thought you were serious.

Actually, to be more serious. Stupid code like this that causes core dumps and magic smoke should not even be attempted on production servers. You obviously seem to have access to those box. In a strict, real-life environment, if you pull stunts like this you are just helping someone to make a career decision for you.

Re: Please Help!

Posted: Wed Mar 18, 2009 9:05 pm
by just brew it!
It actually crossed my mind that you might've been joking (or trolling). But I figured it was better to take a joke post seriously than to take a serious post as a joke.

I have seen code that was this bad out in the real world. And I'm not joking when I say this!

Re: Please Help!

Posted: Thu Mar 19, 2009 12:20 am
by Chaospandion
just brew it! wrote:
It actually crossed my mind that you might've been joking (or trolling). But I figured it was better to take a joke post seriously than to take a serious post as a joke.

I have seen code that was this bad out in the real world. And I'm not joking when I say this!


Let this post be a reminder to you all......

GO REVIEW THE NEW GUY'S CODE!!

Re: Please Help!

Posted: Thu Mar 19, 2009 7:57 am
by Flying Fox
Chaospandion wrote:
just brew it! wrote:
It actually crossed my mind that you might've been joking (or trolling). But I figured it was better to take a joke post seriously than to take a serious post as a joke.

I have seen code that was this bad out in the real world. And I'm not joking when I say this!


Let this post be a reminder to you all......

GO REVIEW THE NEW GUY'S CODE!!

Although it does not negate the need for "code review" (used in quotes because this can take many forms including pair programming) of some sort, why not just hire properly in the first place?