Personal computing discussed

Moderators: SecretSquirrel, just brew it!

 
Chaospandion
Gerbil Team Leader
Topic Author
Posts: 201
Joined: Thu Mar 25, 2004 8:20 pm
Location: Pottstown, PA

Please Help!

Sat Mar 07, 2009 6:02 pm

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++;
}
 
just brew it!
Gold subscriber
Administrator
Posts: 44629
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Please Help!

Sat Mar 07, 2009 8:30 pm

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.
The years just pass like trains. I wave, but they don't slow down.
-- Steven Wilson
 
notfred
Maximum Gerbil
Posts: 4199
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Please Help!

Sun Mar 08, 2009 4:21 pm

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.
 
Flying Fox
Gerbil God
Posts: 25121
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: Please Help!

Sun Mar 08, 2009 5:30 pm

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:
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
 
Chaospandion
Gerbil Team Leader
Topic Author
Posts: 201
Joined: Thu Mar 25, 2004 8:20 pm
Location: Pottstown, PA

Re: Please Help!

Tue Mar 17, 2009 11:56 pm

: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.
 
just brew it!
Gold subscriber
Administrator
Posts: 44629
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Please Help!

Wed Mar 18, 2009 12:10 am

Any programming language or tool that doesn't put you in a straightjacket can cause trouble if misused or abused. With power comes responsibility.
The years just pass like trains. I wave, but they don't slow down.
-- Steven Wilson
 
notfred
Maximum Gerbil
Posts: 4199
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Please Help!

Wed Mar 18, 2009 9:07 am

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.
 
Chaospandion
Gerbil Team Leader
Topic Author
Posts: 201
Joined: Thu Mar 25, 2004 8:20 pm
Location: Pottstown, PA

Re: Please Help!

Wed Mar 18, 2009 9:20 am

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.
 
titan
Grand Gerbil Poohbah
Posts: 3314
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains
Contact:

Re: Please Help!

Wed Mar 18, 2009 1:26 pm

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.
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
 
Flying Fox
Gerbil God
Posts: 25121
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: Please Help!

Wed Mar 18, 2009 7:21 pm

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.
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
 
just brew it!
Gold subscriber
Administrator
Posts: 44629
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Please Help!

Wed Mar 18, 2009 9:05 pm

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!
The years just pass like trains. I wave, but they don't slow down.
-- Steven Wilson
 
Chaospandion
Gerbil Team Leader
Topic Author
Posts: 201
Joined: Thu Mar 25, 2004 8:20 pm
Location: Pottstown, PA

Re: Please Help!

Thu Mar 19, 2009 12:20 am

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!!
 
Flying Fox
Gerbil God
Posts: 25121
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: Please Help!

Thu Mar 19, 2009 7:57 am

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?
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.

Who is online

Users browsing this forum: No registered users and 1 guest