Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
bd1ae0ac
Unverified
Commit
bd1ae0ac
authored
Apr 03, 2018
by
Ge Jun
Committed by
GitHub
Apr 03, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #290 from 2012-wangjiaqi/brpc-dev
fix redis cmd_format bug in the empty string case
parents
88481be8
362a18e2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
6 deletions
+54
-6
redis_command.cpp
src/brpc/redis_command.cpp
+10
-4
brpc_redis_unittest.cpp
test/brpc_redis_unittest.cpp
+44
-2
No files found.
src/brpc/redis_command.cpp
View file @
bd1ae0ac
...
...
@@ -78,12 +78,14 @@ RedisCommandFormatV(butil::IOBuf* outbuf, const char* fmt, va_list ap) {
char
quote_char
=
0
;
const
char
*
quote_pos
=
fmt
;
int
nargs
=
0
;
bool
is_empty_component
=
false
;
for
(;
*
c
;
++
c
)
{
if
(
*
c
!=
'%'
||
c
[
1
]
==
'\0'
)
{
if
(
*
c
==
' '
)
{
if
(
quote_char
)
{
compbuf
.
push_back
(
*
c
);
}
else
if
(
!
compbuf
.
empty
())
{
}
else
if
(
!
compbuf
.
empty
()
||
is_empty_component
)
{
is_empty_component
=
false
;
AppendHeader
(
nocount_buf
,
'$'
,
compbuf
.
size
());
compbuf
.
append
(
"
\r\n
"
,
2
);
nocount_buf
.
append
(
compbuf
);
...
...
@@ -95,6 +97,7 @@ RedisCommandFormatV(butil::IOBuf* outbuf, const char* fmt, va_list ap) {
quote_char
=
*
c
;
quote_pos
=
c
;
}
else
if
(
quote_char
==
*
c
)
{
// end quote
is_empty_component
=
(
c
-
quote_pos
==
1
)
?
true
:
false
;
// for empty string
quote_char
=
0
;
}
else
{
compbuf
.
push_back
(
*
c
);
...
...
@@ -235,7 +238,7 @@ RedisCommandFormatV(butil::IOBuf* outbuf, const char* fmt, va_list ap) {
quote_pos
,
quote_pos
-
fmt
);
}
if
(
!
compbuf
.
empty
())
{
if
(
!
compbuf
.
empty
()
||
is_empty_component
)
{
AppendHeader
(
nocount_buf
,
'$'
,
compbuf
.
size
());
compbuf
.
append
(
"
\r\n
"
,
2
);
nocount_buf
.
append
(
compbuf
);
...
...
@@ -273,11 +276,13 @@ RedisCommandNoFormat(butil::IOBuf* outbuf, const butil::StringPiece& cmd) {
int
ncomponent
=
0
;
char
quote_char
=
0
;
const
char
*
quote_pos
=
cmd
.
data
();
bool
is_empty_component
=
false
;
for
(
const
char
*
c
=
cmd
.
data
();
c
!=
cmd
.
data
()
+
cmd
.
size
();
++
c
)
{
if
(
*
c
==
' '
)
{
if
(
quote_char
)
{
compbuf
.
push_back
(
*
c
);
}
else
if
(
!
compbuf
.
empty
())
{
}
else
if
(
!
compbuf
.
empty
()
||
is_empty_component
)
{
is_empty_component
=
false
;
AppendHeader
(
nocount_buf
,
'$'
,
compbuf
.
size
());
compbuf
.
append
(
"
\r\n
"
,
2
);
nocount_buf
.
append
(
compbuf
);
...
...
@@ -289,6 +294,7 @@ RedisCommandNoFormat(butil::IOBuf* outbuf, const butil::StringPiece& cmd) {
quote_char
=
*
c
;
quote_pos
=
c
;
}
else
if
(
quote_char
==
*
c
)
{
// end quote
is_empty_component
=
(
c
-
quote_pos
==
1
)
?
true
:
false
;
// for empty string
quote_char
=
0
;
}
else
{
compbuf
.
push_back
(
*
c
);
...
...
@@ -303,7 +309,7 @@ RedisCommandNoFormat(butil::IOBuf* outbuf, const butil::StringPiece& cmd) {
quote_pos
,
quote_pos
-
cmd
.
data
());
}
if
(
!
compbuf
.
empty
())
{
if
(
!
compbuf
.
empty
()
||
is_empty_component
)
{
AppendHeader
(
nocount_buf
,
'$'
,
compbuf
.
size
());
compbuf
.
append
(
"
\r\n
"
,
2
);
nocount_buf
.
append
(
compbuf
);
...
...
test/brpc_redis_unittest.cpp
View file @
bd1ae0ac
// Copyright (c) 2014 Baidu, Inc.
// Date: Thu Jun 11 14:30:07 CST 2015
#if defined(BAIDU_INTERNAL)
#include <iostream>
#include "butil/time.h"
...
...
@@ -59,7 +58,9 @@ class RedisTest : public testing::Test {
protected
:
RedisTest
()
{}
void
SetUp
()
{
#if defined(BAIDU_INTERNAL)
pthread_once
(
&
download_redis_server_once
,
DownloadRedisServer
);
#endif
}
void
TearDown
()
{}
};
...
...
@@ -112,6 +113,7 @@ void AssertResponseEqual(const brpc::RedisResponse& r1,
}
}
#if defined(BAIDU_INTERNAL)
TEST_F
(
RedisTest
,
sanity
)
{
brpc
::
ChannelOptions
options
;
options
.
protocol
=
brpc
::
PROTOCOL_REDIS
;
...
...
@@ -403,5 +405,45 @@ TEST_F(RedisTest, auth) {
}
}
}
//namespace
#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\n
set
\r\n
$1
\r\n
a
\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\n
mset
\r\n
$1
\r\n
b
\r\n
$0
\r\n\r\n
$1
\r\n
c
\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\n
set
\r\n
$1
\r\n
a
\r\n
$3
\r\n
123
\r\n
"
,
request
.
_buf
.
to_string
().
c_str
());
request
.
Clear
();
request
.
AddCommand
(
"mset b '' c ccc"
);
ASSERT_STREQ
(
"*5
\r\n
$4
\r\n
mset
\r\n
$1
\r\n
b
\r\n
$0
\r\n\r\n
$1
\r\n
c
\r\n
$3
\r\n
ccc
\r\n
"
,
request
.
_buf
.
to_string
().
c_str
());
request
.
Clear
();
request
.
AddCommand
(
"get ''key value"
);
// == get key value
ASSERT_STREQ
(
"*3
\r\n
$3
\r\n
get
\r\n
$3
\r\n
key
\r\n
$5
\r\n
value
\r\n
"
,
request
.
_buf
.
to_string
().
c_str
());
request
.
Clear
();
request
.
AddCommand
(
"get key'' value"
);
// == get key value
ASSERT_STREQ
(
"*3
\r\n
$3
\r\n
get
\r\n
$3
\r\n
key
\r\n
$5
\r\n
value
\r\n
"
,
request
.
_buf
.
to_string
().
c_str
());
request
.
Clear
();
request
.
AddCommand
(
"get 'ext'key value "
);
// == get extkey value
ASSERT_STREQ
(
"*3
\r\n
$3
\r\n
get
\r\n
$6
\r\n
extkey
\r\n
$5
\r\n
value
\r\n
"
,
request
.
_buf
.
to_string
().
c_str
());
request
.
Clear
();
request
.
AddCommand
(
" get key'ext' value "
);
// == get keyext value
ASSERT_STREQ
(
"*3
\r\n
$3
\r\n
get
\r\n
$6
\r\n
keyext
\r\n
$5
\r\n
value
\r\n
"
,
request
.
_buf
.
to_string
().
c_str
());
request
.
Clear
();
}
}
//namespace
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment