summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Miller <emiller@imvu.com>2007-08-22 04:03:01 +0000
committerEvan Miller <emiller@imvu.com>2007-08-22 04:03:01 +0000
commitb95946f45b9692aaeac2db70b69647c692a86f83 (patch)
tree1845172f0827557f1e1499d93113b05e2c732c3a
parent9d99ab666da9ef495dca289a0854f9a624c07e64 (diff)
downloadmemcached-b95946f45b9692aaeac2db70b69647c692a86f83.tar.gz
Incorporate incrememnt patch from Evan Miller
<emiller@imvu.com> to define increment overflow behavior. git-svn-id: http://code.sixapart.com/svn/memcached/trunk/server@608 b0b603af-a30f-0410-a34e-baf09ae79d0b
-rw-r--r--ChangeLog5
-rw-r--r--doc/protocol.txt4
-rw-r--r--memcached.c8
-rwxr-xr-xt/incrdecr.t13
-rw-r--r--thread.c2
5 files changed, 22 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index 5ebc33d..38450cf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2007-08-21 Paul Lindner <lindner@inuus.com>
+ * Incorporate incrememnt patch from Evan Miller
+ <emiller@imvu.com> to define increment overflow
+ behavior.
+
2007-08-07 Leon Brocard <acme@astray.com>
* Bring the memcached.1 manpage up to date
diff --git a/doc/protocol.txt b/doc/protocol.txt
index 019e850..aa71377 100644
--- a/doc/protocol.txt
+++ b/doc/protocol.txt
@@ -279,8 +279,8 @@ The response will be one of:
after the increment/decrement operation was carried out.
Note that underflow in the "decr" command is caught: if a client tries
-to decrease the value below 0, the new value will be 0. Overflow in
-the "incr" command is not checked.
+to decrease the value below 0, the new value will be 0. Overflow in the
+"incr" command will wrap around the 32 bit mark.
Note also that decrementing a number such that it loses length isn't
guaranteed to decrement its returned length. The number MAY be
diff --git a/memcached.c b/memcached.c
index 10f3bcc..a83cf74 100644
--- a/memcached.c
+++ b/memcached.c
@@ -89,7 +89,6 @@ static int ensure_iov_space(conn *c);
static int add_iov(conn *c, const void *buf, int len);
static int add_msghdr(conn *c);
-
/* time handling */
static void set_current_time(void); /* update the global variable holding
global 32-bit seconds-since-start time
@@ -1257,15 +1256,15 @@ static void process_arithmetic_command(conn *c, token_t *tokens, const size_t nt
*
* returns a response string to send back to the client.
*/
-char *do_add_delta(item *it, const int incr, unsigned int delta, char *buf) {
+char *do_add_delta(item *it, const int incr, const unsigned int delta, char *buf) {
char *ptr;
- unsigned int value;
+ uint32_t value;
int res;
ptr = ITEM_data(it);
while ((*ptr != '\0') && (*ptr < '0' && *ptr > '9')) ptr++; // BUG: can't be true
- value = strtol(ptr, NULL, 10);
+ value = strtoul(ptr, NULL, 10);
if(errno == ERANGE) {
return "CLIENT_ERROR cannot increment or decrement non-numeric value";
@@ -2651,6 +2650,7 @@ int main (int argc, char **argv) {
}
}
+
/* initialize main thread libevent instance */
main_base = event_init();
diff --git a/t/incrdecr.t b/t/incrdecr.t
index 4abd691..6849630 100755
--- a/t/incrdecr.t
+++ b/t/incrdecr.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl
use strict;
-use Test::More tests => 13;
+use Test::More tests => 16;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;
@@ -30,6 +30,15 @@ is(scalar <$sock>, "0\r\n", "- 9 = 0");
print $sock "decr num 5\r\n";
is(scalar <$sock>, "0\r\n", "- 5 = 0");
+print $sock "incr num ".(2**32-2)."\r\n";
+is(scalar <$sock>, (2**32-2)."\r\n", "+ ".(2**32-2)." = ".(2**32-2));
+
+print $sock "incr num 1\r\n";
+is(scalar <$sock>, (2**32-1)."\r\n", "+ 1 = ".(2**32-1));
+
+print $sock "incr num 1\r\n";
+is(scalar <$sock>, "0\r\n", "+ 1 = 0");
+
print $sock "decr bogus 5\r\n";
is(scalar <$sock>, "NOT_FOUND\r\n", "can't decr bogus key");
@@ -40,5 +49,3 @@ print $sock "set text 0 0 2\r\nhi\r\n";
is(scalar <$sock>, "STORED\r\n", "stored text");
print $sock "incr text 1\r\n";
is(scalar <$sock>, "1\r\n", "hi - 1 = 0");
-
-
diff --git a/thread.c b/thread.c
index e92b1d4..edbc461 100644
--- a/thread.c
+++ b/thread.c
@@ -462,7 +462,7 @@ char *mt_defer_delete(item *item, time_t exptime) {
/*
* Does arithmetic on a numeric item value.
*/
-char *mt_add_delta(item *item, int incr, unsigned int delta, char *buf) {
+char *mt_add_delta(item *item, int incr, const unsigned int delta, char *buf) {
char *ret;
pthread_mutex_lock(&cache_lock);