From: Pieter Noordhuis Date: Wed, 15 Dec 2010 10:30:50 +0000 (+0100) Subject: Disable negative offsets for SETRANGE X-Git-Url: https://git.saurik.com/redis.git/commitdiff_plain/8f8eeffec1fe1c0d40b6d078121661d7cfb02c08?ds=sidebyside Disable negative offsets for SETRANGE --- diff --git a/src/t_string.c b/src/t_string.c index f1bab4da..324e01ab 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -195,11 +195,13 @@ void setrangeCommand(redisClient *c) { if (getLongFromObjectOrReply(c,c->argv[2],&offset,NULL) != REDIS_OK) return; + if (offset < 0) { + addReplyError(c,"offset is out of range"); + return; + } + o = lookupKeyWrite(c->db,c->argv[1]); if (o == NULL) { - /* Negative offset is always 0 for non-existing keys */ - if (offset < 0) offset = 0; - /* Return 0 when setting nothing on a non-existing string */ if (sdslen(value) == 0) { addReply(c,shared.czero); @@ -233,15 +235,6 @@ void setrangeCommand(redisClient *c) { return; } - /* Convert negative indexes. Note that for SETRANGE, the meaning of a - * negative index is a little different than for other commands. - * Here, an offset of -1 points to the trailing NULL byte of the - * string instead of the last character. */ - if (offset < 0) { - offset = olen+1+offset; - if (offset < 0) offset = 0; - } - /* Return when the resulting string exceeds allowed size */ if (checkStringLength(c,offset+sdslen(value)) != REDIS_OK) return; diff --git a/tests/unit/basic.tcl b/tests/unit/basic.tcl index 884522a8..a9b25fce 100644 --- a/tests/unit/basic.tcl +++ b/tests/unit/basic.tcl @@ -489,14 +489,6 @@ start_server {tags {"basic"}} { r del mykey assert_equal 4 [r setrange mykey 1 foo] assert_equal "\000foo" [r get mykey] - - r del mykey - assert_equal 3 [r setrange mykey -1 foo] - assert_equal "foo" [r get mykey] - - r del mykey - assert_equal 3 [r setrange mykey -100 foo] - assert_equal "foo" [r get mykey] } test "SETRANGE against string-encoded key" { @@ -512,18 +504,6 @@ start_server {tags {"basic"}} { assert_equal 3 [r setrange mykey 1 b] assert_equal "fbo" [r get mykey] - r set mykey "foo" - assert_equal 6 [r setrange mykey -1 bar] - assert_equal "foobar" [r get mykey] - - r set mykey "foo" - assert_equal 5 [r setrange mykey -2 bar] - assert_equal "fobar" [r get mykey] - - r set mykey "foo" - assert_equal 3 [r setrange mykey -20 bar] - assert_equal "bar" [r get mykey] - r set mykey "foo" assert_equal 7 [r setrange mykey 4 bar] assert_equal "foo\000bar" [r get mykey] @@ -549,18 +529,6 @@ start_server {tags {"basic"}} { assert_encoding raw mykey assert_equal 1334 [r get mykey] - r set mykey 1234 - assert_encoding int mykey - assert_equal 5 [r setrange mykey -1 5] - assert_encoding raw mykey - assert_equal 12345 [r get mykey] - - r set mykey 1234 - assert_encoding int mykey - assert_equal 4 [r setrange mykey -2 5] - assert_encoding raw mykey - assert_equal 1235 [r get mykey] - r set mykey 1234 assert_encoding int mykey assert_equal 6 [r setrange mykey 5 2] @@ -577,7 +545,9 @@ start_server {tags {"basic"}} { test "SETRANGE with out of range offset" { r del mykey assert_error "*maximum allowed size*" {r setrange mykey [expr 512*1024*1024-4] world} + r set mykey "hello" + assert_error "*out of range*" {r setrange mykey -1 world} assert_error "*maximum allowed size*" {r setrange mykey [expr 512*1024*1024-4] world} }