Trond Norbye's Weblog

« Previous day (Dec 3, 2008) | Main | Next day (Dec 4, 2008) »

http://blogs.sun.com/trond/date/20081204 Thursday December 04, 2008

Alignment bug in the binary protocol

When we implemented support for the binary protocol in the server we assigned the protocol header to a struct with the following code snippet:

protocol_binary_request_header* req;
req = (protocol_binary_request_header*)c->rcurr;
c->binary_header = *req;

The problem is that some hardware require a certain alignment for various datatypes, and SPARC is one of them. If you don't pipeline commands c->rcurr points to the beginning of a buffer allocated with malloc(), and malloc() will always return pointers to properly aligned memory. The problem occurs if you pipeline multiple commands to the server, and the size of one of them isn't a multiple of the structure alignment ( size % 8 != 0 ). I discovered this bug the other day when I experimented with the Spy memcached client by Dustin Sallings.

Luckily the bug was pretty easy to track down and fix: http://github.com/trondn/memcached/commit/4c3797b1d4fe9088ef96c9f7d571b1221cb40fef

Now I just need to figure out how to write a Perl test for the fix :)


Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.