Simo Virokannas

Writings and ramblings

Wonderful null character

I recently programmed a lengthy piece of online software with PHP and JavaScript, utilizing server-side PHP/MySQL queries. But there was a problem that just popped out of nowhere – all but one data entry worked perfectly. This one row, when observed from the SQL server side (with a great tool called Sequel Pro), looked perfectly fine, it came up in queries, appeared normal, everything ok.

But there’s a PHP script on the server side that makes the appropriate query and returns the data as XML for the JavaScript side to parse. And every time a query contained this one particular entry, it broke. The XML was returned normally, but it didn’t work. Empty set.

So, I decided to make the query manually, giving it as Base64-encoded SQL query to check what’s wrong.

It returned a nice set of XML – with one exception: the browser said there’s a 0x0 character on row 15. That would explain a lot, so I checked row 15. Just normal written text as UTF-8 string in the database. I copied the text and pasted it to Skype for the person who wrote it, with a note after the text.

Black hole, galactic representation of a NULL character. Photo by Alain Riazuelo.

The note disappeared. Found the null character.

After a quick search I learned that it is actually possible to write a NULL character from the keyboard. Didn’t know that. This brings up a whole bunch of new problems that everyone should keep in mind when writing software. Especially when dealing with HTTP servers, as they, sometimes when it’s possible, also send the size of the page in the headers, and the client side then downloads everything but doesn’t expect a NULL character, and sometimes, when no size is sent, client may stop at a NULL. Both times you’ll probably end up mishandling the data.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.