Great Site

I just found this site, The Daily WTF. Your average computer user won't find much use in it, but programmers love it! They post pieces of code like this:

if (errMsg.indexOf("Violation of UNIQUE KEY constraint 'UQ__mbrs_pwd'") != -1)
return "The password entered is already in use. Please enter another.";


And there's always some terrible thing wrong with it. The post with that code is called "Uniquely Secure".

I notice users on there that have trouble grasping what's wrong with the code sometimes, and it's good that they visit this site every day or whatever, since it's not only funny, it's a brain teaser every day.

Here's another quick sample:

function executeQuery($string)
{
GetDatabaseConnection();
$result = mysql_query($sqlText) or die(
"Query failed " . writeErrorToLog(
$_SESSION['USERNAME'],
"Query Failed: " .$sqlText . " " . mysql_error()
,$scriptName
));
return $result;
}


function writeErrorToLog($owner,$description,$script)
{
$script = str_replace($_SERVER['DOCUMENT_ROOT'], "", $script);
$sqlText =
"INSERT INTO errorLog (ownerID,time,description,script) " .
"VALUES ('" . $owner . ",Now(),'" . $description . "','" . $script . "')";
executeQuery($sqlText, $_SERVER['PHP_SELF']);

return "";
}


At first glance (I think it's PHP) it doesn't seem like much is wrong, other then they do the cardinal sin of writing to the database to report an error when there's a database error! But anyway, you will notice that if a piece of code fails to write to the database, it will always infinite loop! The reason for this is that, for one, there could be a database connection problem, and it would never succeed, but more interestingly, there seems to be a missing apostrophe (') in one of the values in the sql statement when writing an error! So no matter what, if it fails once, it's doomed for infinite loopage.

And the site has one of these per day, and I love reading stuff like this. I recently bought a book called Java Puzzlers: Traps, Pitfalls and Corner Cases. They're fun brain teasers, and most of the time, in the Java one, you can't tell what's wrong (having 10 years programming under my belt, that's odd!) and then you go read the solution and it's like some bit can't be set when calling a certain function. Java's a pesky language :) I still love it though.

blog comments powered by Disqus