• Advertisement
Sign in to follow this  

Funniest line of code ever ?

This topic is 1058 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Trying to track down a memory leak the other day I ended up in a bit of the code base that hasn't been touched for many years.

 

I slowly worked through it unitl I hit this LOC and just cracked up.

 

#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))

 

Any body else got a good one?

 

 

Share this post


Link to post
Share on other sites
Advertisement

What is wrong with this preprocessor line? It just means to do stuff if this is gcc and we're on an x86 system... I'm not getting the joke.

 

It's basically saying "if your computer is really, really, really old.... or really new"

 

If you have a 486, pentium, or any modern 32 bit processor, you are out of luck.

rolleyes.gif

Share this post


Link to post
Share on other sites

 

What is wrong with this preprocessor line? It just means to do stuff if this is gcc and we're on an x86 system... I'm not getting the joke.

 

It's basically saying "if your computer is really, really, really old.... or really new"

 

If you have a 486, pentium, or any modern 32 bit processor, you are out of luck.

rolleyes.gif

 

 

Not really. I don't know what software the codebase is from, but x86 isn't the only processor architecture. For instance, if you wanted to run some code on a raspberry pi, or an arduino, this preprocessor condition would evaluate to zero. And those are hardly mythical machines like the PDP. So, some context would have probably been appropriate in this case, e.g. "windows game" smile.png 

Share this post


Link to post
Share on other sites

This was during a C programming class, someone wrote that code to 'use' a null terminated string. Both me and the teacher spent quite a while staring at it, trying to 'guess' why it crashes.

void use_str(char * str)
{
  while(*str=!0)
  {
	//do something with char in *str
	++str;
  }
}

Also, a nice attempt to hardcode the password in php that someone I know did on their personal filesharing website (I'm not good with PHP, so this code might be wrong, but you get the idea laugh.png ).

if ($pass === "password1" || "password2")
Edited by FRex

Share this post


Link to post
Share on other sites

 

This was during a C programming class, someone wrote that code to 'use' a null terminated string. Both me and the teacher spent quite a while staring at it, trying to 'guess' why it crashes.

void use_str(const char * str)
{
  while(str=!0)
  {
	//do something with char in *str
	++str;
  }
}

Also, a nice attempt to hardcode the password in php that someone I know did on their personal filesharing website (I'm not good with PHP, so this code might be wrong, but you get the idea laugh.png ).

if ($pass === "password1" || "password2")

 

Ah that zero terminated string thing must have been crashing bad unless you sent NULL to it... biggrin.png

 

The PHP code example, I believe is more common that you would like to know.  Seems like perfectly legal PHP to me, but I assume the point is that you should never put your password in clear text in the source file?  

 

It has been a while since I have touched PHP now, but I used to do a trick like this to make sure I had number values from user input fields:

$user_input += 0;

//Then use in an SQL query, safely knowing it can't be escaped...

Not sure if that is still regarded as a safe trick to force number values.

 

-- Oh, and I actually used the 'eval' function on data that was sent from the user once...  Not a good idea unless you really are knowing what you are doing.

 

I should have written down all the stupid errors I have done during the years, and I would be able to fill a whole book lol...  biggrin.png

Edited by aregee

Share this post


Link to post
Share on other sites

 

Ah that zero terminated string thing must have been crashing bad unless you sent NULL to it... biggrin.png

I'm not sure what you mean by that. biggrin.png (Actually - I made mistake in original snippet, I've corrected that now, the point is that code is writing 1s to string forever instead of checking for 0 because ! and = are swapped).

 

the point is that you should never put your password in clear text in the source file?

The point is that it works as

if (($pass === "password1") || "password2")

and the literal he used was interpreted as true (it wasn't "0" or ""), the condition was always true so any password worked. It also makes sense if you read it out loud (and that wasn't a programmer writing that): "if variable pass is strictly equal to password1 or password2".

What he meant was

if (($pass === "password1") || ($pass === "password2"))
Edited by FRex

Share this post


Link to post
Share on other sites


So it's saying "If you're on x86 or on x86-64..." tongue.png wink.png

 

Not on our compiler, 386 MEANS 386 , not x86 generic, just 386. 

 

We also have things like __ppc__ , __mips__, __arm__, __x86__,  and the most scary of all __WIN64__   ohmy.png

Share this post


Link to post
Share on other sites

I debugged a problem some years ago and found the following line:

logbook->logMutex( true, true, false, true );

Unfortunately at that moment I didn't had much time to laught about it smile.png .

 

Kimmi

Share this post


Link to post
Share on other sites


Why doesn't the compiler complain that you are trying to assign to a read-only variable?

 

Because its a pointer to const char, and not a const pointer to char. The pointer itself is not readonly... or did I miss the joke? :D

Share this post


Link to post
Share on other sites


*str=!0



does assign to *str, it is assigning !0 to it.

 

God, I was looking at the wrong line... *feels ashamed and goes to the corner*

Share this post


Link to post
Share on other sites

 

Why doesn't the compiler complain that you are trying to assign to a read-only variable?

Because I put the const there out of habit and was writing that out of my memory. The original obviously didn't have the const because it modified the str and simply crashed when pointer finally went too far. I've corrected the original snippet (again).

 

The pointer itself is not readonly... or did I miss the joke? biggrin.png

No, that was my mistake, the actual 'joke' (this happened in class so it's more of a funny error than a joke) is that this code looks ok at a glance but the swapped ! and = make it try and fill all the memory, starting with str, with 1s instead of doing intended work...

Edited by FRex

Share this post


Link to post
Share on other sites
I think by now I want a version of C++ that doesn't do any automatic type conversions at all. !0 is the int 1, and you shouldn't be allowed to assign that to a char without an explicit cast.

Share this post


Link to post
Share on other sites

 

What he meant was

if (($pass === "password1") || ($pass === "password2"))

 "Equal to" and "of the same type" ... bit redundant .

Share this post


Link to post
Share on other sites

From a Kinect Game i was refactoring.

 

The level had a fixed size and you were moving through kind of a tube, only capable of strafing and adjusting your speed.

Player.Score += (int)(60f * Player.Velocity.Length() * gameTime.GetDelta());

Share this post


Link to post
Share on other sites

 

 "Equal to" and "of the same type" ... bit redundant .

Maybe, but at least that prevents surprises from numerical conversions and weird interactions between $pass and the string if $pass is some other type. It gets really screwy:

http://php.net/manual/en/types.comparisons.php

I don't remember if it was === or == actually, it could have been ==, it was long ago and that person wasn't a programmer.

Edited by FRex

Share this post


Link to post
Share on other sites

My favorites are always static analyzer stuff.  Frequently annotated with "//to shut up the static analyzer".  I can't think of any recent examples though.

Share this post


Link to post
Share on other sites

I knew a guy who wrapped some code that would never be reached in:

if (superman > batman){
//some code
}

biggrin.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement