Ensure keys are capped to memcached's limits

Keys in memcached are limited to 250B. Make sure this limit is enforced to avoid
error responses.

Reference code in memcached:

  95e6469bd2/memcached.c (L2205)
  95e6469bd2/memcached.h (L36)
This commit is contained in:
Lluís Vilanova 2018-05-07 15:22:44 +03:00
parent d65c6ef7c2
commit bdc4255ede

View File

@ -8,6 +8,7 @@
#define GENERATOR_H #define GENERATOR_H
#define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#include "config.h" #include "config.h"
@ -197,6 +198,10 @@ public:
double U = (double) h / ULLONG_MAX; double U = (double) h / ULLONG_MAX;
double G = g->generate(U); double G = g->generate(U);
int keylen = MAX(round(G), floor(log10(max)) + 1); int keylen = MAX(round(G), floor(log10(max)) + 1);
// memcached limits keys to 250 bytes
// https://github.com/memcached/memcached/blob/95e6469bd2ceef92bcaaf140e2724fc73d556185/memcached.c#L2205
// https://github.com/memcached/memcached/blob/95e6469bd2ceef92bcaaf140e2724fc73d556185/memcached.h#L36
keylen = MIN(keylen, 250);
char key[256]; char key[256];
snprintf(key, 256, "%0*" PRIu64, keylen, ind); snprintf(key, 256, "%0*" PRIu64, keylen, ind);