From d4fe357383d5adbe59dd77108afa92a2b2fc835b Mon Sep 17 00:00:00 2001 From: Date: Wed, 13 Feb 2008 15:08:41 +0530 Subject: [PATCH] MEMCACHED_BAD_KEY_PROVIDED has been added as an error type. When libmemcached is compiled in debug mode it can toss an error if a bad key is provided. --- ChangeLog | 3 +++ include/memcached.h | 1 + lib/Makefile.am | 1 + lib/common.h | 10 +++++++++- lib/memcached_auto.c | 3 +++ lib/memcached_get.c | 3 +++ lib/memcached_storage.c | 3 +++ lib/memcached_strerror.c | 2 ++ tests/function.c | 20 ++++++++++++++++++++ 9 files changed, 45 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 55dff5f4..59a6f932 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,9 @@ * Servers are now sorted, meaning that servers are now ordered so that clients with the same lists, will have same distribution. (Idea from Ross McFarland). + * Added MEMCACHED_BAD_KEY_PROVIDED error for auto, set, and get operations + when the library is compiled with --enable-debug. This additional code + will test for bad keys. 0.15 Tue Jan 29 14:55:44 PST 2008 * More work on the C++ API. diff --git a/include/memcached.h b/include/memcached.h index 095ac435..fa2d305c 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -67,6 +67,7 @@ typedef enum { MEMCACHED_FETCH_NOTFINISHED, MEMCACHED_TIMEOUT, MEMCACHED_BUFFERED, + MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_MAXIMUM_RETURN, /* Always add new error code before */ } memcached_return; diff --git a/lib/Makefile.am b/lib/Makefile.am index 5589e3b9..0783bcf4 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -43,6 +43,7 @@ libmemcached_la_SOURCES = crc.c \ memcached_hosts.c \ memcached_io.c \ md5.c \ + memcached_key.c \ memcached_quit.c \ memcached_parse.c \ memcached_response.c \ diff --git a/lib/common.h b/lib/common.h index 3c707143..610d4320 100644 --- a/lib/common.h +++ b/lib/common.h @@ -21,7 +21,7 @@ #include #include #include -#include "libmemcached_config.h" +#include #if TIME_WITH_SYS_TIME # include @@ -110,5 +110,13 @@ memcached_return value_fetch(memcached_server_st *ptr, memcached_result_st *result); void server_list_free(memcached_st *ptr, memcached_server_st *servers); +memcached_return key_proof(char **keys, size_t *key_length, + unsigned int number_of_keys); + +#ifdef HAVE_DEBUG +#define key_test(A,B,C) key_proof(A,B,C) +#else +#define key_test(A,B,C) MEMCACHED_SUCCESS +#endif #endif /* __COMMON_H__ */ diff --git a/lib/memcached_auto.c b/lib/memcached_auto.c index 4af85e97..36780dc5 100644 --- a/lib/memcached_auto.c +++ b/lib/memcached_auto.c @@ -17,6 +17,9 @@ static memcached_return memcached_auto(memcached_st *ptr, if (ptr->hosts == NULL || ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; + if (key_test(&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED) + return MEMCACHED_BAD_KEY_PROVIDED; + server_key= memcached_generate_hash(ptr, key, key_length); send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, diff --git a/lib/memcached_get.c b/lib/memcached_get.c index 8c3180ab..cfbb13f4 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -75,6 +75,9 @@ memcached_return memcached_mget_by_key(memcached_st *ptr, if (ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; + if (key_test(keys, key_length, number_of_keys) == MEMCACHED_BAD_KEY_PROVIDED) + return MEMCACHED_BAD_KEY_PROVIDED; + if (ptr->flags & MEM_SUPPORT_CAS) { get_command= "gets "; diff --git a/lib/memcached_storage.c b/lib/memcached_storage.c index d8b12432..30585095 100644 --- a/lib/memcached_storage.c +++ b/lib/memcached_storage.c @@ -66,6 +66,9 @@ static inline memcached_return memcached_send(memcached_st *ptr, if (ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; + if (key_test(&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED) + return MEMCACHED_BAD_KEY_PROVIDED; + server_key= memcached_generate_hash(ptr, master_key, master_key_length); if (cas) diff --git a/lib/memcached_strerror.c b/lib/memcached_strerror.c index 8b8438bb..3271682d 100644 --- a/lib/memcached_strerror.c +++ b/lib/memcached_strerror.c @@ -68,6 +68,8 @@ char *memcached_strerror(memcached_st *ptr, memcached_return rc) return "ACTION QUEUED"; case MEMCACHED_TIMEOUT: return "A TIMEOUT OCCURRED"; + case MEMCACHED_BAD_KEY_PROVIDED: + return "A BAD KEY WAS PROVIDED/CHARACTERS OUT OF RANGE"; case MEMCACHED_MAXIMUM_RETURN: return "Gibberish returned!"; default: diff --git a/tests/function.c b/tests/function.c index 0ead1435..1534019c 100644 --- a/tests/function.c +++ b/tests/function.c @@ -457,6 +457,25 @@ uint8_t flush_test(memcached_st *memc) return 0; } +uint8_t bad_key_test(memcached_st *memc) +{ +#ifdef HAVE_DEBUG + memcached_return rc; + char *key= "foo bad"; + char *string; + size_t string_length; + uint32_t flags; + + string= memcached_get(memc, key, strlen(key), + &string_length, &flags, &rc); + assert(rc == MEMCACHED_BAD_KEY_PROVIDED); + assert(string_length == 0); + assert(!string); +#endif + + return 0; +} + uint8_t get_test(memcached_st *memc) { memcached_return rc; @@ -2361,6 +2380,7 @@ test_st tests[] ={ {"behavior_test", 0, get_stats_keys }, {"callback_test", 0, get_stats_keys }, {"version_string_test", 0, version_string_test}, + {"bad_key", 1, bad_key_test }, {0, 0, 0} }; -- 2.30.2