problem with libmemcache

gavin hurley gavin at
Fri Apr 1 19:32:47 PST 2005

I have a problem with libmemcache and I don't know if it's a problem 
with my C skills (which are quite rusty; them newfangled garbage 
collected languages have made me soft) or libmemcache itself. So far, 
the evidence is in favor of a problem with libmemcache.

I have a very simple test program that writes a key/value pair to the 
cache and then gets that value back. Most values work okay but when the 
payload is 12 characters long I have a problem with what's returned. I 
get back 15 characters instead of 12. The extras are non-printable and 
they seem somewhat random (suspiciously like uninitialized memory) 
except that they're always the same.

If I change the payload to make it 11 or 13 charecters long, it works 
beautifully. Also, it doesn't seem to have anything to do with the 
length of the key. I've been debugging for hours but I can't quite 
figure out what's going on in libmemcache when it does the memory 
allocation for mc_aget.

Anyway, here's the program. My environment is Linux. Basically, I'm 
looking for someone to either show that I'm an idiot or bring awareness 
to a bug in libmemcache. Many thanks.

Oh, almost forgot. When I compile and run this on my Mac (FreeBSD core) 
I get the same problem when the payload is 2 characters (and other 
lengths as well). 12 characters seems to be okay.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "memcache.h"

int main () {
     struct memcache *mc = NULL;
     mc = mc_new();
     if (mc == NULL) {
         printf("unable to allocate memcache instance. aborting\n");
     mc_server_add(mc, "", "11211");

     char* key = "test_foo";
     char* payload = "123456789012"; /*payloads of length 12 won't work 
     void* result;

     printf("payload: %s \n", payload);
     mc_set(mc, key, strlen(key), payload, strlen(payload), 0, 0);

     result = mc_aget(mc, key, strlen(key));
     printf("returned from cache: %s length %d \n\n",
             (char *)result, strlen(result));

     return 0;

More information about the memcached mailing list