Fix issue where stale result set might end up being read (this has never been reporte...
authorBrian Aker <brian@tangent.org>
Tue, 22 May 2012 04:10:39 +0000 (00:10 -0400)
committerBrian Aker <brian@tangent.org>
Tue, 22 May 2012 04:10:39 +0000 (00:10 -0400)
libmemcached/fetch.cc
libmemcached/get.cc
libmemcached/initialize_query.cc
tests/libmemcached-1.0/mem_functions.cc

index 2b5b800693c495245a3265ff49acdf65e81c344c..27ea3a8413722f987cbb3eebeee503c9820b0703 100644 (file)
@@ -155,10 +155,12 @@ memcached_result_st *memcached_fetch_result(memcached_st *ptr,
                                             memcached_return_t *error)
 {
   memcached_return_t unused;
-  if (not error)
+  if (error == NULL)
+  {
     error= &unused;
+  }
 
-  if (not ptr)
+  if (ptr == NULL)
   {
     *error= MEMCACHED_INVALID_ARGUMENTS;
     return NULL;
@@ -176,7 +178,7 @@ memcached_result_st *memcached_fetch_result(memcached_st *ptr,
     // create one.
     if (memcached_is_initialized(&ptr->result))
     {
-      if (not (result= memcached_result_create(ptr, NULL)))
+      if ((result= memcached_result_create(ptr, NULL)) == NULL)
       {
         *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
         return NULL;
index 86f136707e0d3a9ebb92d8c63e33b167e9336a8d..be7d9e5f66b226912f86ced59e7d9bf9e277e953 100644 (file)
@@ -119,8 +119,9 @@ char *memcached_get_by_key(memcached_st *ptr,
   {
     if (ptr->get_key_failure and *error == MEMCACHED_NOTFOUND)
     {
-      memcached_result_reset(&ptr->result);
-      memcached_return_t rc= ptr->get_key_failure(ptr, key, key_length, &ptr->result);
+      memcached_result_st key_failure_result;
+      memcached_result_st* result_ptr= memcached_result_create(ptr, &key_failure_result);
+      memcached_return_t rc= ptr->get_key_failure(ptr, key, key_length, result_ptr);
 
       /* On all failure drop to returning NULL */
       if (rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED)
@@ -135,10 +136,10 @@ char *memcached_get_by_key(memcached_st *ptr,
           }
 
           rc= memcached_set(ptr, key, key_length,
-                            (memcached_result_value(&ptr->result)),
-                            (memcached_result_length(&ptr->result)),
+                            (memcached_result_value(result_ptr)),
+                            (memcached_result_length(result_ptr)),
                             0,
-                            (memcached_result_flags(&ptr->result)));
+                            (memcached_result_flags(result_ptr)));
 
           if (rc == MEMCACHED_BUFFERED and latch == 0)
           {
@@ -148,20 +149,25 @@ char *memcached_get_by_key(memcached_st *ptr,
         else
         {
           rc= memcached_set(ptr, key, key_length,
-                            (memcached_result_value(&ptr->result)),
-                            (memcached_result_length(&ptr->result)),
+                            (memcached_result_value(result_ptr)),
+                            (memcached_result_length(result_ptr)),
                             0,
-                            (memcached_result_flags(&ptr->result)));
+                            (memcached_result_flags(result_ptr)));
         }
 
         if (rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED)
         {
           *error= rc;
-          *value_length= memcached_result_length(&ptr->result);
-          *flags= memcached_result_flags(&ptr->result);
-          return memcached_string_take_value(&ptr->result.value);
+          *value_length= memcached_result_length(result_ptr);
+          *flags= memcached_result_flags(result_ptr);
+          char *result_value=  memcached_string_take_value(&result_ptr->value);
+          memcached_result_free(result_ptr);
+
+          return result_value;
         }
       }
+
+      memcached_result_free(result_ptr);
     }
     assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
 
index dca3c4d8276cc0c33c188f663571c5ad0a106812..0a4aae075dbafe05d9350a7ac2a81da64f463b18 100644 (file)
@@ -59,6 +59,7 @@ memcached_return_t initialize_query(memcached_st *self, bool increment_query_id)
   }
 
   memcached_error_free(*self);
+  memcached_result_reset(&self->result);
 
   return MEMCACHED_SUCCESS;
 }
index f8ac3da67ce8a15482bd91dfa13115ff913c9cb1..8b88697a71c9af781cce0aec919e0def2dbacdd3 100644 (file)
@@ -1026,12 +1026,11 @@ test_return_t bad_key_test(memcached_st *memc)
 }
 
 #define READ_THROUGH_VALUE "set for me"
-static memcached_return_t read_through_trigger(memcached_st *memc,
-                                               char *key,
-                                               size_t key_length,
+static memcached_return_t read_through_trigger(memcached_st *, // memc
+                                               char *, // key
+                                               size_t, //  key_length,
                                                memcached_result_st *result)
 {
-  (void)memc;(void)key;(void)key_length;
   return memcached_result_set_value(result, READ_THROUGH_VALUE, strlen(READ_THROUGH_VALUE));
 }
 
@@ -1062,8 +1061,8 @@ test_return_t read_through(memcached_st *memc)
                         &string_length, &flags, &rc);
 
   test_compare(MEMCACHED_SUCCESS, rc);
-  test_compare(string_length, sizeof(READ_THROUGH_VALUE) -1);
-  test_true(string[sizeof(READ_THROUGH_VALUE) -1] == 0);
+  test_compare(sizeof(READ_THROUGH_VALUE) -1, string_length);
+  test_compare(0, string[sizeof(READ_THROUGH_VALUE) -1]);
   test_strcmp(READ_THROUGH_VALUE, string);
   free(string);