- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Brillant new error handling approach!
do { LogError(); } while (true);
Admin
This kind of bug happens more often than we think. I've recently seen a similar bug, thought not in a infinite loop fashion.
Admin
Hardly a WTF at all. Slap a check for if not connected then log with error_log() or just bail.
An oversight like this isn't a WTF, sheesh.
Admin
>>An oversight like this isn't a WTF, sheesh.
The WTF isn't the code. The WTF is that the previous maintenance developer didn't investigate and just rebooted the server any time there was an error that triggered the error trap infinite loop.
Admin
Yes, when the phonelines go dead, I quickly get on the phone to call the phone company to let them know.
He must have left the position to move on to management.
Admin
The REAL WTF is the eventual stack overflow. That's what my do loop fixes!
Admin
Actually an oversight, leading to an infinite loop thereby crashing your entire program, sounds pretty much like the definition of a WTF.
Not saying I have never done it, that I will at least admit it.
Admin
the code is also a WTF, because the query executed in the executeQuery function is using $sqlText to execute the query, not the parameter $string. Thus when the error occurs, it will continually execute the same query that caused the error instead of executing the error query.
Admin
You seem to have missed the point -- when the database goes down, how will you be able to write a log entry into a table in the very same database that just went down?
Admin
That is not a WTF - it is petty incompetence entrained into a support person by our Microsoft-PC environment.
A WTF is when something is done that rises above the level of normal laziness, incompetence, ignorance, attitude or a combination.
This is not brillant, has no juice and isNotSortOfNull.
In an era of Uncle-Bill-induced reboot-for-memory-leaks, it is the normal learned activity for anybody not supervized by a manager willing to slap the developer/support person/whoever/whatever for not investigating.
Good people - the criteria for acceptance as a WTF is going south in a hurry.
Admin
I guess this was written by a programmer who knew he was going to be fired, and he wanted some closure.
Admin
It also happens to be missing a single quote after the owner
Admin
Keyboard not found! Press any key to continue...
Admin
doesn't executeQuery() only take one argument anyway?
Admin
He could have just coded the shutdown right in the error handling ... it would have been much easier that way and he would have had more time to adjust his fantasy baseball lineup.
Admin
Yeah I ran into one that had a CheckUser() function in the HandleError() (so you could insert the user name into the log). Of course the CheckUser() function also had an error handler that led to the HandleError() function .... similar results. Talk about a stack overflow
Admin
Almost...it's the other way around. Instead of executing the offending command, it executes some random sql query. One of the possible sql queries it could be executing is the query that writes a log message.
It's also odd that writeErrorToLog gives two arguments to executeQuery but executeQuery seems to need only one...
Admin
$sqlText =
"INSERT INTO errorLog (ownerID,time,description,script) " .
"VALUES ('" . $owner . ",Now(),'" . $description . "','" . $script . "')";
isn't an apostrophe missing?
"INSERT INTO errorLog (ownerID,time,description,script) " .
"VALUES ('" . $owner . <FONT style="BACKGROUND-COLOR: #7fffd4">"<FONT color=#ff1493>'</FONT>,Now</FONT>(),'" . $description . "','" . $script . "')";
Admin
I'll agree this is pretty much incompetence, and not really crazy enough.
However, it was the first one in a long time that actually made me laugh out loud when I read the code. (the truly brillant ones usually make me cry, rather than laugh)
Admin
hey, thats exactly what our system did untill not so long ago....and it was a web application too.
lol, i dont even have an account here, but just had to comment on that.
Admin
You missed it.
The code follows this logic:
write data to database, if fail then call the write error to log routine, which calls the write data to database routine to write the error to the log.
It's called an infinite loop.
Admin
store floating point value as integer!
Admin
How often does your MySQL server fail right in the middle of script execution? He could have added a different error handler for when mysql_connect() fails and with the above code he wants to log any queries that might fail for whatever reason.
However, there must have been some error in copying this code for this thread, because mysql_query() will never execute something since the query is contained in $string and not $sqltext, thus it will always log an error.
Admin
...or a recursive call...
--Jim
Admin
You'd be surprised. I have errors try to log to the database first before falling back, and occasionally I get an error *in the database log* saying the database is unavailable. It's good for a laugh every now and then.
Admin
Admin
Actually, that makes perfect sense. The BIOS is giving you an opportunity to plug a keyboard in (when it will still be recognized) before your OS boots and simply assumes that there is never going to be a keyboard. Especially usefull back in the day when plugging in a keyboard would crash DOS/Windows.
Admin
Writing errors to a database, though? Seriously, there's nothing wrong with using plain text flat files for things like this.
Or XML ducks
Admin
function executeQuery($string)
{
return executeQueryHelper($string, 1);
}
function executeQueryIgnoringErrors($string)
{
return executeQueryHelper($string, 0);
}
function executeQueryHelper($string, $dieOnError)
{
GetDatabaseConnection();
$result = mysql_query($sqlText)
if ($dieOnError and not $result) {
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 . "')";
executeQueryIgnoringErrors($sqlText, $_SERVER['PHP_SELF']);
return "";
}
Admin
Recursion is a Good Thing (TM).
Admin
Admin
XML ducks?
Admin
Shouldn't php's maximum execution time kick in a stop the script from running? I don't get why he had to keep rebooting the server, unless he was dumb enough to set php to run sripts forever.
Admin
You'd be surprised. I have errors try to log to the database first before falling back, and occasionally I get an error *in the database log* saying the database is unavailable. It's good for a laugh every now and then.
My apps often open a second database connection when logging an error. So, if the first connection goes south, I can get that kind of error. This was fairly common back in the Win16 days when any other TCP/IP traffic had a good chance of FUBARing SQL Server connections if it happened while you were executing SQL. (Microsoft's "fix" was to go to the 32 bit client.)
Admin
yes. you definitely know the business.
Admin
Infinite Recursion n. see loop
Loop n. see Infinite Recursion
Is there another word for "Thesaurus"?
Admin
They walk in a row (according to a Schema).
Admin
Fat finger problem, the "D" is right next to the "S".
Admin
My previous post was in response to this post.... The Real WTF (tm) is this forum software that doesn't allow editing posts.
Admin
I like how writeErrorToLog is executed as a side-effect of concatenating its return (a constant "" BTW) with the error message that gets printed out, which is conveniently also a really meaningful constant: "Query failed ".
Admin
I can't help but quote Douglas Adams:
Anything that happens happens, anything that in happening causes something else to happen causes something else to happen, and anything that in happening causes itself to happen again, happens again. Although not necessarily in chronological order.
Admin
We had a similar problem around here. Basically, if you hit any unhandled exception it would redirect you to an error page. But the error page was only available to logged in users. Attempting to access this page while not logged in would throw an exception which was not being handled. So any time unlogged in users got an error (i.e. on the public part of the site) or if you got an error while logging in, BAM! Stack overflow.
Admin
I see a sql inyection, because the SQL is never quoted with mysql_real_quote or something alike. Or this guys will use some magic php feature that will aturdetect smoke?
Admin
you all oversee that this code might actually work given that the SQL server that failed was on another machine than the PHP / HTTP server. There might have been an extra database for logging installed on the PHP machine (well, it looks like this wasn't the case), and if it would be not down as well, it'd work.
Admin
There might be such an extra database. Unfortunately, since executeQuery doesn't take a second argument, the extra database won't be targetted. Further, as writeToErrorLog does not check to see if the error was in attempting to write to the error log, it still doesn't matter, because even if it could target the alternate database, a problem with that alternate database could still cause the problem - it just wouldn't happen quite so often.
Admin
>>The WTF is that the previous maintenance developer didn't investigate
Reminds me a bit of Monday's SharkTank:
...a few weeks ago, the server started reporting errors on the screen. She then proudly stated that they would simply shut the power off and turn it back on, and it would work again for a day or two. This morning, it wouldn't come back - it just ran in an infinite reboot mode. Only then did they think to call us."
Admin
I couldn't agree more. Clearly, this is all Micro$oft's fault. Very astute observation.
Admin
Unfortunately, I once did a similar thing on a website. The error page, like the others, tried to connect to the database server in order to show whether the user was logged in. Of course, if the reason for the error had been a MySQL server failure, trying to connect would just fail again, leading to the error page being included again, and so on.
In my defence, I had been very tired, and removed it the next day before I got the opportunity to see what happened if the database failed...
Admin
Well, there is some hope that sooner or later(more likely much later)script will get a connection to DB thus stoping the infinite loop[:D]
Admin
I nearly did something like this once, too my lasting shame. Then I noticed how very silly it was and fixed it.