Commit 1046f5cc authored by 2012-wangjiaqi's avatar 2012-wangjiaqi

fix redis cmd_format bug in the empty string case

parent 06a9cd55
...@@ -78,12 +78,14 @@ RedisCommandFormatV(butil::IOBuf* outbuf, const char* fmt, va_list ap) { ...@@ -78,12 +78,14 @@ RedisCommandFormatV(butil::IOBuf* outbuf, const char* fmt, va_list ap) {
char quote_char = 0; char quote_char = 0;
const char* quote_pos = fmt; const char* quote_pos = fmt;
int nargs = 0; int nargs = 0;
bool is_empty_component = false;
for (; *c; ++c) { for (; *c; ++c) {
if (*c != '%' || c[1] == '\0') { if (*c != '%' || c[1] == '\0') {
if (*c == ' ') { if (*c == ' ') {
if (quote_char) { if (quote_char) {
compbuf.push_back(*c); compbuf.push_back(*c);
} else if (!compbuf.empty()) { } else if (!compbuf.empty() || is_empty_component) {
is_empty_component = false;
AppendHeader(nocount_buf, '$', compbuf.size()); AppendHeader(nocount_buf, '$', compbuf.size());
compbuf.append("\r\n", 2); compbuf.append("\r\n", 2);
nocount_buf.append(compbuf); nocount_buf.append(compbuf);
...@@ -95,6 +97,7 @@ RedisCommandFormatV(butil::IOBuf* outbuf, const char* fmt, va_list ap) { ...@@ -95,6 +97,7 @@ RedisCommandFormatV(butil::IOBuf* outbuf, const char* fmt, va_list ap) {
quote_char = *c; quote_char = *c;
quote_pos = c; quote_pos = c;
} else if (quote_char == *c) { // end quote } else if (quote_char == *c) { // end quote
is_empty_component = (c - quote_pos == 1) ? true : false; // for empty string
quote_char = 0; quote_char = 0;
} else { } else {
compbuf.push_back(*c); compbuf.push_back(*c);
...@@ -235,7 +238,7 @@ RedisCommandFormatV(butil::IOBuf* outbuf, const char* fmt, va_list ap) { ...@@ -235,7 +238,7 @@ RedisCommandFormatV(butil::IOBuf* outbuf, const char* fmt, va_list ap) {
quote_pos, quote_pos - fmt); quote_pos, quote_pos - fmt);
} }
if (!compbuf.empty()) { if (!compbuf.empty() || is_empty_component) {
AppendHeader(nocount_buf, '$', compbuf.size()); AppendHeader(nocount_buf, '$', compbuf.size());
compbuf.append("\r\n", 2); compbuf.append("\r\n", 2);
nocount_buf.append(compbuf); nocount_buf.append(compbuf);
...@@ -273,11 +276,13 @@ RedisCommandNoFormat(butil::IOBuf* outbuf, const butil::StringPiece& cmd) { ...@@ -273,11 +276,13 @@ RedisCommandNoFormat(butil::IOBuf* outbuf, const butil::StringPiece& cmd) {
int ncomponent = 0; int ncomponent = 0;
char quote_char = 0; char quote_char = 0;
const char* quote_pos = cmd.data(); const char* quote_pos = cmd.data();
bool is_empty_component = false;
for (const char* c = cmd.data(); c != cmd.data() + cmd.size(); ++c) { for (const char* c = cmd.data(); c != cmd.data() + cmd.size(); ++c) {
if (*c == ' ') { if (*c == ' ') {
if (quote_char) { if (quote_char) {
compbuf.push_back(*c); compbuf.push_back(*c);
} else if (!compbuf.empty()) { } else if (!compbuf.empty() || is_empty_component) {
is_empty_component = false;
AppendHeader(nocount_buf, '$', compbuf.size()); AppendHeader(nocount_buf, '$', compbuf.size());
compbuf.append("\r\n", 2); compbuf.append("\r\n", 2);
nocount_buf.append(compbuf); nocount_buf.append(compbuf);
...@@ -289,6 +294,7 @@ RedisCommandNoFormat(butil::IOBuf* outbuf, const butil::StringPiece& cmd) { ...@@ -289,6 +294,7 @@ RedisCommandNoFormat(butil::IOBuf* outbuf, const butil::StringPiece& cmd) {
quote_char = *c; quote_char = *c;
quote_pos = c; quote_pos = c;
} else if (quote_char == *c) { // end quote } else if (quote_char == *c) { // end quote
is_empty_component = (c - quote_pos == 1) ? true : false; // for empty string
quote_char = 0; quote_char = 0;
} else { } else {
compbuf.push_back(*c); compbuf.push_back(*c);
...@@ -303,7 +309,7 @@ RedisCommandNoFormat(butil::IOBuf* outbuf, const butil::StringPiece& cmd) { ...@@ -303,7 +309,7 @@ RedisCommandNoFormat(butil::IOBuf* outbuf, const butil::StringPiece& cmd) {
quote_pos, quote_pos - cmd.data()); quote_pos, quote_pos - cmd.data());
} }
if (!compbuf.empty()) { if (!compbuf.empty() || is_empty_component) {
AppendHeader(nocount_buf, '$', compbuf.size()); AppendHeader(nocount_buf, '$', compbuf.size());
compbuf.append("\r\n", 2); compbuf.append("\r\n", 2);
nocount_buf.append(compbuf); nocount_buf.append(compbuf);
......
// Copyright (c) 2014 Baidu, Inc. // Copyright (c) 2014 Baidu, Inc.
// Date: Thu Jun 11 14:30:07 CST 2015 // Date: Thu Jun 11 14:30:07 CST 2015
#if defined(BAIDU_INTERNAL)
#include <iostream> #include <iostream>
#include "butil/time.h" #include "butil/time.h"
...@@ -59,7 +58,9 @@ class RedisTest : public testing::Test { ...@@ -59,7 +58,9 @@ class RedisTest : public testing::Test {
protected: protected:
RedisTest() {} RedisTest() {}
void SetUp() { void SetUp() {
#if defined(BAIDU_INTERNAL)
pthread_once(&download_redis_server_once, DownloadRedisServer); pthread_once(&download_redis_server_once, DownloadRedisServer);
#endif
} }
void TearDown() {} void TearDown() {}
}; };
...@@ -112,6 +113,7 @@ void AssertResponseEqual(const brpc::RedisResponse& r1, ...@@ -112,6 +113,7 @@ void AssertResponseEqual(const brpc::RedisResponse& r1,
} }
} }
#if defined(BAIDU_INTERNAL)
TEST_F(RedisTest, sanity) { TEST_F(RedisTest, sanity) {
brpc::ChannelOptions options; brpc::ChannelOptions options;
options.protocol = brpc::PROTOCOL_REDIS; options.protocol = brpc::PROTOCOL_REDIS;
...@@ -403,5 +405,30 @@ TEST_F(RedisTest, auth) { ...@@ -403,5 +405,30 @@ TEST_F(RedisTest, auth) {
} }
} }
} //namespace
#endif // BAIDU_INTERNAL #endif // BAIDU_INTERNAL
TEST_F(RedisTest, cmd_format) {
brpc::RedisRequest request;
// set empty string
request.AddCommand("set a ''");
ASSERT_STREQ("*3\r\n$3\r\nset\r\n$1\r\na\r\n$0\r\n\r\n",
request._buf.to_string().c_str());
request.Clear();
request.AddCommand("mset b '' c ''");
ASSERT_STREQ("*5\r\n$4\r\nmset\r\n$1\r\nb\r\n$0\r\n\r\n$1\r\nc\r\n$0\r\n\r\n",
request._buf.to_string().c_str());
request.Clear();
// set non-empty string
request.AddCommand("set a 123");
ASSERT_STREQ("*3\r\n$3\r\nset\r\n$1\r\na\r\n$3\r\n123\r\n",
request._buf.to_string().c_str());
request.Clear();
request.AddCommand("mset b '' c ccc");
ASSERT_STREQ("*5\r\n$4\r\nmset\r\n$1\r\nb\r\n$0\r\n\r\n$1\r\nc\r\n$3\r\nccc\r\n",
request._buf.to_string().c_str());
request.Clear();
}
} //namespace
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment