• FEATURED

View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Funniest line of code ever ?

126 replies to this topic

#1Stainless  Members

Posted 17 July 2014 - 02:27 AM

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?

#2Katie  Members

Posted 17 July 2014 - 03:06 AM

POPULAR

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...

#3Bacterius  Members

Posted 17 July 2014 - 03:10 AM

POPULAR

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.”

#4Acid-Chris  Members

Posted 17 July 2014 - 03:11 AM

POPULAR

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. ;)

#5Stainless  Members

Posted 17 July 2014 - 03:19 AM

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.

#6Bacterius  Members

Posted 17 July 2014 - 03:31 AM

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.

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"

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

#7FRex  Members

Posted 17 July 2014 - 03:46 AM

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

if ($pass === "password1" || "password2")  Edited by FRex, 17 July 2014 - 06:34 AM. An in-game LuaConsole for SFML: https://github.com/FRex/LuaConsole #8Hodgman Moderators Posted 17 July 2014 - 03:49 AM POPULAR 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. __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... #9aregee Members Posted 17 July 2014 - 04:12 AM 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 ). if ($pass === "password1" || "password2")


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

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... Edited by aregee, 17 July 2014 - 04:16 AM. #10FRex Members Posted 17 July 2014 - 04:32 AM Ah that zero terminated string thing must have been crashing bad unless you sent NULL to it... I'm not sure what you mean by that. (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, 17 July 2014 - 04:37 AM.

An in-game LuaConsole for SFML: https://github.com/FRex/LuaConsole

#11Álvaro  Members

Posted 17 July 2014 - 04:55 AM

POPULAR

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;
}
}


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

Edited by Álvaro, 17 July 2014 - 04:56 AM.

#12Stainless  Members

Posted 17 July 2014 - 05:28 AM

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__

#13Kimmi  Members

Posted 17 July 2014 - 05:29 AM

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  .

Kimmi

A complicate solution may indicate a not understood problem.

@KimKulling

#14Juliean  Members

Posted 17 July 2014 - 05:43 AM

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?

Posted 17 July 2014 - 06:10 AM

*str=!0

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#16Juliean  Members

Posted 17 July 2014 - 06:12 AM

*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*

#17FRex  Members

Posted 17 July 2014 - 06:36 AM

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?

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, 17 July 2014 - 06:45 AM.

An in-game LuaConsole for SFML: https://github.com/FRex/LuaConsole

#18Álvaro  Members

Posted 17 July 2014 - 06:56 AM

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.

#19SeanMiddleditch  Members

Posted 17 July 2014 - 12:23 PM

POPULAR

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

Then the joke is that your broke your compiler. Any x86 from the i386 onward will get __i386__ defined in a non-broken GCC.

__i386__ identifies that you're compiling for some x86 CPU. If you're compiling for a higher-end CPU (i486, etc.) then the compiler just adds additional defines and does not stop defining __i386__ since any i486 is _also_ an i386 (the CPUs are all backwards-compatible so any i386-specific code will function on an i486, i586, etc).

the most scary of all __WIN64__

What's scary about __WIN64__? That's when you're compiling on Windows in 64-bit mode. If that's being defined in any other case, then again, your compiler is broken. Win64 uses an entirely different ABI than Win32 so it makes sense to let users easily detect this case, as any architecture/platform-specific code possibly needs to be ported to accomodate the changes in syscall interfaces, calling convention, exception handling, etc.

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!

#20Code Fox  GDNet+

Posted 17 July 2014 - 12:27 PM

What he meant was

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


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

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson