People don't know what they want. Technical people are supposed to be able to recognize when a person asks for one thing but would be better off with something else that provides the same or better service.

Linus Torvalds (on why "the customer asked for it" is not an excuse for inferior technical solutions)

Blog About JV Roig

Amazing One-in-a-Billion Typo Bug

Author's avatar

by JV Roig
on November 25, 2017

I was working on something pretty bland and mundane, when I stumbled upon an almost magical bug.

What’s a magical bug, you ask?

It’s a bug that isn’t just harmless (doesn’t produce errors or unintended side effects in any case) – it pretty much also does what was intended to happen anyway.

It’s still a bug though, the most common one, I would imagine: a source code typo.

I was patching some PHP code – creating a new function - when I saw the function right below the new code I was writing:

So there I was, working on making the check_level() function. I was pretty much done. Then suddenly, I noticed the code below mine and noticed a typo. In the screenshot above, I commented out the line with a typo and replaced it with the corrected one right above it.

This is the commented out line, that has the typo:


This is the fixed line, typo corrected:


Did you catch it? The last part in the chain should be stmt_close() (notice the underscore), and not stmt->close() (that means the chain accesses the stmt property of the current object, and then the close() method of the stmt property is called)

Why was this typo (basically mixing up an underscore and an arrow) harmless?

It just so happened that there really was a property called “stmt” anyway, so PHP won’t complain that no such property exists.

And it just so happened that the “stmt” property is itself an object – a MySQL statement resource – so it does have the close() method. So once again, PHP won’t complain that the method doesn’t exist for the stmt object.

Well, so it’s harmless and actually runs. But how does it do what was intended anyway? Because this is what stmt_close() does:

    function stmt_close()
        $this->stmt_bind_args = array();
        return $this;

It just calls the stmt property of the object, and that stmt property actually holds a MySQL statement resource (as discussed above), and calls that object’s close() method. The two lines above it just clear out two other properties, in case the main object (not the stmt object) will be reused for another query (this is basically the code for a data abstraction class). In the setting where the typo happened, it doesn’t matter whether those two other properties are cleared or not, because the function is short and the data abstraction object never gets reused.

So what basically happened was a one-in-a-billion typo bug: through a confluence of factors, there's this bug that is harmless and does what was intended anyway, although purely by accident.

As you can see from the screenshot’s FIXME note, I reminded myself to not let the typo stay there. I fixed the code shortly afterwards, but wanted to blog about it.

If you don’t do any sort of programming, you probably don’t get what the big deal. I get it. You need to be a coder. Go ahead and ask any developer friend of yours if they’ve ever known any bug that not only was harmless, but still pretty much did what was intended anyway, despite being clearly a typo.

If you’re a programmer… you’re welcome! I doubt I’ll encounter a bug as magical as this one, this is the first time I’ve encountered something like this since I started coding ~20 years ago. If you have some pretty amazing bugs that you stumbled upon that you’d like to share, chime in!

Tags: Accident, Trivia, Source Code Typo, Magic