We now check return key size for memcached_fetch() to make sure it is not
[m6w6/libmemcached] / libmemcached / fetch.c
1 #include "common.h"
2
3 char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
4 size_t *value_length,
5 uint32_t *flags,
6 memcached_return_t *error)
7 {
8 memcached_result_st *result_buffer= &ptr->result;
9
10 unlikely (ptr->flags.use_udp)
11 {
12 *error= MEMCACHED_NOT_SUPPORTED;
13 return NULL;
14 }
15
16 result_buffer= memcached_fetch_result(ptr, result_buffer, error);
17
18 if (result_buffer == NULL || *error != MEMCACHED_SUCCESS)
19 {
20 WATCHPOINT_ASSERT(result_buffer == NULL);
21 *value_length= 0;
22 return NULL;
23 }
24
25 *value_length= memcached_string_length(&result_buffer->value);
26
27 if (key)
28 {
29 if (result_buffer->key_length > MEMCACHED_MAX_KEY)
30 {
31 *error= MEMCACHED_KEY_TOO_BIG;
32 *value_length= 0;
33
34 return NULL;
35 }
36 strncpy(key, result_buffer->item_key, result_buffer->key_length); // For the binary protocol we will cut off the key :(
37 *key_length= result_buffer->key_length;
38 }
39
40 if (result_buffer->item_flags)
41 *flags= result_buffer->item_flags;
42 else
43 *flags= 0;
44
45 return memcached_string_c_copy(&result_buffer->value);
46 }
47
48 memcached_result_st *memcached_fetch_result(memcached_st *ptr,
49 memcached_result_st *result,
50 memcached_return_t *error)
51 {
52 memcached_server_st *server;
53
54 unlikely (ptr->flags.use_udp)
55 {
56 *error= MEMCACHED_NOT_SUPPORTED;
57 return NULL;
58 }
59
60 if (result == NULL)
61 if ((result= memcached_result_create(ptr, NULL)) == NULL)
62 return NULL;
63
64 while ((server = memcached_io_get_readable_server(ptr)) != NULL)
65 {
66 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
67 *error= memcached_response(server, buffer, sizeof(buffer), result);
68
69 if (*error == MEMCACHED_SUCCESS)
70 return result;
71 else if (*error == MEMCACHED_END)
72 memcached_server_response_reset(server);
73 else if (*error != MEMCACHED_NOTFOUND)
74 break;
75 }
76
77 /* We have completed reading data */
78 if (memcached_is_allocated(result))
79 {
80 memcached_result_free(result);
81 }
82 else
83 {
84 memcached_string_reset(&result->value);
85 }
86
87 return NULL;
88 }
89
90 memcached_return_t memcached_fetch_execute(memcached_st *ptr,
91 memcached_execute_fn *callback,
92 void *context,
93 uint32_t number_of_callbacks)
94 {
95 memcached_result_st *result= &ptr->result;
96 memcached_return_t rc= MEMCACHED_FAILURE;
97 unsigned int x;
98
99 while ((result= memcached_fetch_result(ptr, result, &rc)) != NULL)
100 {
101 if (rc == MEMCACHED_SUCCESS)
102 {
103 for (x= 0; x < number_of_callbacks; x++)
104 {
105 rc= (*callback[x])(ptr, result, context);
106 if (rc != MEMCACHED_SUCCESS)
107 break;
108 }
109 }
110 }
111 return rc;
112 }