Funniest line of code ever ?

Started by
125 comments, last by Orymus3 9 years, 1 month ago

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?

Advertisement

I was working on a project from an outsourced developer and came across an enum which said "{ Z_UP // Z axis points up from the ground, Z_DOWN // Z axis points down, Z_OTHER }"

And no-one had felt the need to comment the third possibility...

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

I've seen things....:


return STATUS_ERROR;  //This returns the status error

Anyway, if i look at 5+ year old code of mine i still would laugh out loud many many times. ;)

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

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

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

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")

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

__i386__ means that you're on an x86 CPU (excluding ones prior to the 386). __x86_64__ means that you're on an x86-64 CPU.

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

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

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"))

This topic is closed to new replies.

Advertisement