I cannot load data from this location.
While updating a year-old PHP application, I encountered a bug I’d never seen before: in certain rare cases, the strlen function can cause PHP to crash on Mac OS X.
My initial problem was this: Based on certain POST inputs, I would get no response to a submitted form. Literally nothing. Safari was the only browser that barfed at all, with an error message along the lines of “Safari can’t load data from this location.”
To debug, I first took a look-see at the raw communication sent over port 80. The POST headers of the submitted form looked fine, but there was no response from the server. That seemed awfully strange, so I looked at the error log on my local server (I test and develop apps on my local Mac before deployment to a production server.) Sure enough, the process running my application was crashing:
[notice] child pid 1977 exit signal Bus error (10)
That, there, my friends, is Not A Good Sign. What in the world caused a “Bus error”? Moreover, the crash was fickle yet reproducible; it only occurred when I left certain checkboxes checked in my html form (thus sending an array rather than a single variable to the server.)
Since to this point the problem was only occuring in my local environment, I uploaded the affected program to a production server. It worked like a charm; no problems whatsoever. Clearly the crash was specific to my computer, either my build of PHP, Apache, or other hardware-related issue.
Just to check my sanity, I wrote up a quick test program emulating the problem as I saw it. The test app worked fine, so that was a dead end. I rebuilt PHP. Nope. Since that didn’t work, I accepted that I would have to narrow this down and find the offending code, a difficult task when PHP crashes rather than giving you an error to work off of. I resorted to debugging the original program by iteratively adding a die() statement to the code and seeing what happened. If the program outputted the die() command, then the problem occured later on in the code execution. And if it crashed, I knew it was somewhere earlier in the execution.
And then I found it. I had localized the problem to a validation routine specific to the Mojavi framework. That section of code contained a line that looked something like this:
if (strlen($variable)>0) {
//then do something
}
However, my $variable in this case was an array, not a string, as normally expected by strlen. I modified that code slightly, removing the strlen function, and voila! No more crashes on Mac OS X. What is odd is that strlen() usually doesn’t barf on arrays (by barf I mean “bus error”). And when I created a test program to conclusively show that the problem was the strlen function, it worked just fine.
So I left it at that.
