#include <libmemcached/common.h>
#include <libmemcached/string.hpp>
-static memcached_return_t textual_value_fetch(org::libmemcached::Instance* instance,
+static memcached_return_t textual_value_fetch(memcached_instance_st* instance,
char *buffer,
memcached_result_st *result)
{
}
for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++) {};
+ errno= 0;
result->item_flags= (uint32_t) strtoul(next_ptr, &string_ptr, 10);
- if (end_ptr == string_ptr)
+ if (errno != 0 or end_ptr == string_ptr)
{
goto read_error;
}
}
for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++) {};
+ errno= 0;
value_length= (size_t)strtoull(next_ptr, &string_ptr, 10);
- if (end_ptr == string_ptr)
+ if (errno != 0 or end_ptr == string_ptr)
{
goto read_error;
}
{
string_ptr++;
for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++) {};
+ errno= 0;
result->item_cas= strtoull(next_ptr, &string_ptr, 10);
}
- if (end_ptr < string_ptr)
+ if (errno != 0 or end_ptr < string_ptr)
{
goto read_error;
}
return MEMCACHED_PARTIAL_READ;
}
-static memcached_return_t textual_read_one_response(org::libmemcached::Instance* instance,
+static memcached_return_t textual_read_one_response(memcached_instance_st* instance,
char *buffer, const size_t buffer_length,
memcached_result_st *result)
{
char *response_ptr= index(buffer, ' ');
char *endptr;
+ errno= 0;
long int version= strtol(response_ptr, &endptr, 10);
- if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX or version == 0)
+ if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX or version == 0)
{
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version"));
instance->major_version= uint8_t(version);
endptr++;
+ errno= 0;
version= strtol(endptr, &endptr, 10);
- if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
+ if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX)
{
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse minor version"));
instance->minor_version= uint8_t(version);
endptr++;
+ errno= 0;
version= strtol(endptr, &endptr, 10);
- if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
+ if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX)
{
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
return MEMCACHED_E2BIG;
}
- if (total_read >= memcached_literal_param_size("SERVER_ERROR out of memory storing object") and
- (memcmp(buffer, memcached_literal_param("SERVER_ERROR out of memory storing object")) == 0))
+ if (total_read >= memcached_literal_param_size("SERVER_ERROR out of memory") and
+ ((memcmp(buffer, memcached_literal_param("SERVER_ERROR out of memory")) == 0) or
+ (memcmp(buffer, memcached_literal_param("SERVER_ERROR Out of memory")) == 0)))
{
return MEMCACHED_SERVER_MEMORY_ALLOCATION_FAILURE;
}
case '8': /* INCR/DECR response */
case '9': /* INCR/DECR response */
{
+ errno= 0;
unsigned long long int auto_return_value= strtoull(buffer, (char **)NULL, 10);
if (auto_return_value == ULLONG_MAX and errno == ERANGE)
return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT,
memcached_literal_param("Numeric response was out of range"));
}
+ else if (errno != 0)
+ {
+ result->numeric_value= UINT64_MAX;
+ return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT,
+ memcached_literal_param("Numeric response was out of range"));
+ }
result->numeric_value= uint64_t(auto_return_value);
buffer, total_read);
}
-static memcached_return_t binary_read_one_response(org::libmemcached::Instance* instance,
+static memcached_return_t binary_read_one_response(memcached_instance_st* instance,
char *buffer, const size_t buffer_length,
memcached_result_st *result)
{
memcached_return_t rc;
protocol_binary_response_header header;
+ assert(memcached_is_binary(instance->root));
+
if ((rc= memcached_safe_read(instance, &header.bytes, sizeof(header.bytes))) != MEMCACHED_SUCCESS)
{
WATCHPOINT_ERROR(rc);
* (only the final NOOP), so we need to increment the counter again.
*/
memcached_server_response_increment(instance);
- /* FALLTHROUGH */
+ /* fall through */
case PROTOCOL_BINARY_CMD_GETK:
{
uint16_t keylen= header.response.keylen;
}
char *endptr;
+ errno= 0;
long int version= strtol(version_buffer, &endptr, 10);
- if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX or version == 0)
+ if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX or version == 0)
{
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version"));
instance->major_version= uint8_t(version);
endptr++;
+ errno= 0;
version= strtol(endptr, &endptr, 10);
- if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
+ if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX)
{
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse minor version"));
instance->minor_version= uint8_t(version);
endptr++;
+ errno= 0;
version= strtol(endptr, &endptr, 10);
- if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
+ if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX)
{
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
}
break;
+ case PROTOCOL_BINARY_CMD_TOUCH:
+ {
+ rc= MEMCACHED_SUCCESS;
+ if (bodylen == 4) // The four byte read is a bug?
+ {
+ char touch_buffer[4]; // @todo document this number
+ rc= memcached_safe_read(instance, touch_buffer, sizeof(touch_buffer));
+#if 0
+ fprintf(stderr, "%s:%d %d %d %d %d %.*s(%d)\n", __FILE__, __LINE__,
+ int(touch_buffer[0]),
+ int(touch_buffer[1]),
+ int(touch_buffer[2]),
+ int(touch_buffer[3]),
+ int(bodylen), touch_buffer, int(bodylen));
+#endif
+ }
+ return memcached_set_error(*instance, rc, MEMCACHED_AT);
+ }
+
case PROTOCOL_BINARY_CMD_FLUSH:
case PROTOCOL_BINARY_CMD_QUIT:
case PROTOCOL_BINARY_CMD_SET:
case PROTOCOL_BINARY_CMD_APPEND:
case PROTOCOL_BINARY_CMD_PREPEND:
case PROTOCOL_BINARY_CMD_DELETE:
- case PROTOCOL_BINARY_CMD_TOUCH:
{
WATCHPOINT_ASSERT(bodylen == 0);
return MEMCACHED_SUCCESS;
case PROTOCOL_BINARY_CMD_REPLACEQ:
case PROTOCOL_BINARY_CMD_APPENDQ:
case PROTOCOL_BINARY_CMD_PREPENDQ:
- return binary_read_one_response(instance, buffer, buffer_length, result);
+ return MEMCACHED_FETCH_NOTFINISHED;
default:
break;
return rc;
}
-static memcached_return_t _read_one_response(org::libmemcached::Instance* instance,
+static memcached_return_t _read_one_response(memcached_instance_st* instance,
char *buffer, const size_t buffer_length,
memcached_result_st *result)
{
if (result == NULL)
{
- memcached_st *root= (memcached_st *)instance->root;
+ Memcached *root= (Memcached *)instance->root;
result = &root->result;
}
memcached_return_t rc;
if (memcached_is_binary(instance->root))
{
- rc= binary_read_one_response(instance, buffer, buffer_length, result);
+ do {
+ rc= binary_read_one_response(instance, buffer, buffer_length, result);
+ } while (rc == MEMCACHED_FETCH_NOTFINISHED);
}
else
{
return rc;
}
-memcached_return_t memcached_read_one_response(org::libmemcached::Instance* instance,
+memcached_return_t memcached_read_one_response(memcached_instance_st* instance,
memcached_result_st *result)
{
char buffer[SMALL_STRING_LEN];
return _read_one_response(instance, buffer, sizeof(buffer), result);
}
-memcached_return_t memcached_response(org::libmemcached::Instance* instance,
+memcached_return_t memcached_response(memcached_instance_st* instance,
memcached_result_st *result)
{
char buffer[1024];
return memcached_response(instance, buffer, sizeof(buffer), result);
}
-memcached_return_t memcached_response(org::libmemcached::Instance* instance,
+memcached_return_t memcached_response(memcached_instance_st* instance,
char *buffer, size_t buffer_length,
memcached_result_st *result)
{
return memcached_set_error(*instance, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
}
- /* We may have old commands in the buffer not set, first purge */
+ /* We may have old commands in the buffer not sent, first purge */
if ((instance->root->flags.no_block) and (memcached_is_processing_input(instance->root) == false))
{
(void)memcached_io_write(instance);
}
+ /* Before going into loop wait to see if we have any IO waiting for us */
+ if (0)
+ {
+ memcached_return_t read_rc= memcached_io_wait_for_read(instance);
+ fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, memcached_strerror(NULL, read_rc));
+ }
+
/*
* The previous implementation purged all pending requests and just
* returned the last one. Purge all pending messages to ensure backwards