From 7524221ddbcdc2fd7780501f3fdf36ecc2e41cb0 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Wed, 15 Feb 2017 14:12:34 -0500 Subject: [PATCH] LP#1652382: handle cases where supplied key is longer than 250 bytes With this patch, if cache clients want to use a key longer than the memcached text protocol limit of 250 bytes, the key is normalized to 'shortened_' + md5_hex(normalized_key). Signed-off-by: Galen Charlton Signed-off-by: Jeff Davis --- src/libopensrf/osrf_cache.c | 11 ++++++++++- src/perl/lib/OpenSRF/Utils/Cache.pm | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/libopensrf/osrf_cache.c b/src/libopensrf/osrf_cache.c index 4fccfd0..dd489e1 100644 --- a/src/libopensrf/osrf_cache.c +++ b/src/libopensrf/osrf_cache.c @@ -15,6 +15,8 @@ GNU General Public License for more details. #include +#define MAX_KEY_LEN 250 + static struct memcached_st* _osrfCache = NULL; static time_t _osrfCacheMaxSeconds = -1; static char* _clean_key( const char* ); @@ -57,7 +59,14 @@ char* _clean_key( const char* key ) { char* clean_key = (char*)strdup(key); char* d = clean_key; char* s = clean_key; - do while(isspace(*s)) s++; while(*d++ = *s++); + do while(isspace(*s) || iscntrl(*s)) s++; while(*d++ = *s++); + if (strlen(clean_key) > MAX_KEY_LEN) { + char *hashed = md5sum(clean_key); + clean_key[0] = '\0'; + strncat(clean_key, "shortened_", 11); + strncat(clean_key, hashed, MAX_KEY_LEN); + free(hashed); + } return clean_key; } diff --git a/src/perl/lib/OpenSRF/Utils/Cache.pm b/src/perl/lib/OpenSRF/Utils/Cache.pm index ba9f1a1..36721d9 100644 --- a/src/perl/lib/OpenSRF/Utils/Cache.pm +++ b/src/perl/lib/OpenSRF/Utils/Cache.pm @@ -2,6 +2,7 @@ package OpenSRF::Utils::Cache; use strict; use warnings; use base qw/OpenSRF/; use Cache::Memcached; +use Digest::MD5 qw(md5_hex); use OpenSRF::Utils::Logger qw/:level/; use OpenSRF::Utils::Config; use OpenSRF::Utils::SettingsClient; @@ -281,6 +282,9 @@ sub _clean_cache_key { my $key = shift; $key =~ s{(\p{Cntrl}|\s)}{}g; + if (length($key) > 250) { # max length of memcahed key + $key = 'shortened_' . md5_hex($key); + } return $key; } -- 2.11.0