Fix index error of CRLF when replying with integer-encoded strings (#13711)

close #13709

Fix the index error of CRLF character for integer-encoded strings
in addReplyBulk function

---------

Co-authored-by: debing.sun <debing.sun@redis.com>
This commit is contained in:
raffertyyu 2024-12-31 21:41:10 +08:00 committed by GitHub
parent dc57ee03b1
commit 04f63d4af7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 64 additions and 2 deletions

View File

@ -1080,8 +1080,8 @@ void addReplyBulk(client *c, robj *obj) {
* to the output buffer. */
char buf[34];
size_t len = ll2string(buf,sizeof(buf),(long)obj->ptr);
buf[len+1] = '\r';
buf[len+2] = '\n';
buf[len] = '\r';
buf[len+1] = '\n';
_addReplyLongLongBulk(c, len);
_addReplyToBufferOrList(c,buf,len+2);
} else {

View File

@ -135,6 +135,68 @@ start_server {tags {"protocol network"}} {
assert_equal [r read] {a}
}
test "bulk reply protocol" {
# value=2 (int encoding)
r set crlf 2
assert_equal [r rawread 5] "+OK\r\n"
r get crlf
assert_equal [r rawread 7] "\$1\r\n2\r\n"
r object encoding crlf
assert_equal [r rawread 9] "\$3\r\nint\r\n"
# value=2147483647 (int encoding)
r set crlf 2147483647
assert_equal [r rawread 5] "+OK\r\n"
r get crlf
assert_equal [r rawread 17] "\$10\r\n2147483647\r\n"
r object encoding crlf
assert_equal [r rawread 9] "\$3\r\nint\r\n"
# value=-2147483648 (int encoding)
r set crlf -2147483648
assert_equal [r rawread 5] "+OK\r\n"
r get crlf
assert_equal [r rawread 18] "\$11\r\n-2147483648\r\n"
r object encoding crlf
assert_equal [r rawread 9] "\$3\r\nint\r\n"
# value=-9223372036854775809 (embstr encoding)
r set crlf -9223372036854775809
assert_equal [r rawread 5] "+OK\r\n"
r get crlf
assert_equal [r rawread 27] "\$20\r\n-9223372036854775809\r\n"
r object encoding crlf
assert_equal [r rawread 12] "\$6\r\nembstr\r\n"
# value=9223372036854775808 (embstr encoding)
r set crlf 9223372036854775808
assert_equal [r rawread 5] "+OK\r\n"
r get crlf
assert_equal [r rawread 26] "\$19\r\n9223372036854775808\r\n"
r object encoding crlf
assert_equal [r rawread 12] "\$6\r\nembstr\r\n"
# normal sds (embstr encoding)
r set crlf aaaaaaaaaaaaaaaa
assert_equal [r rawread 5] "+OK\r\n"
r get crlf
assert_equal [r rawread 23] "\$16\r\naaaaaaaaaaaaaaaa\r\n"
r object encoding crlf
assert_equal [r rawread 12] "\$6\r\nembstr\r\n"
# normal sds (raw string encoding) with 45 'a'
set rawstr [string repeat "a" 45]
r set crlf $rawstr
assert_equal [r rawread 5] "+OK\r\n"
r get crlf
assert_equal [r rawread 52] "\$45\r\n$rawstr\r\n"
r object encoding crlf
assert_equal [r rawread 9] "\$3\r\nraw\r\n"
r del crlf
assert_equal [r rawread 4] ":1\r\n"
}
# restore connection settings
r readraw 0
r deferred 0