problem with libmemcache

Evan Martin evan.martin at
Fri Apr 1 20:37:53 PST 2005

It looks to me like you're inserting exactly 12 bytes of your
12-character string, which means you're not including the trailing
NULL.  So there's no way upon retrieval to tell how long the string

(1) Null-terminate your strings when you insert: effectively, just put
strlen(x)+1 bytes in.
(2) Get the number of bytes read back from libmemcache [is this
possible? I'm staring at the API and not seeing it] and use that to
stick a trailing NULL on.  (Be sure your buffer is long enough.)

On Apr 1, 2005 7:32 PM, gavin hurley <gavin at> wrote:
> 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.
> -gsh
> #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");
>          exit(1);
>      }
>      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));
>      free(result);
>      mc_server_disconnect_all(mc);
>      mc_free(mc);
>      return 0;
> }

More information about the memcached mailing list