Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
8e2c3920
Commit
8e2c3920
authored
Mar 06, 2011
by
Pieter Noordhuis
Browse files
Fix formatCommand to work with all empty interpolations
parent
bf544ce8
Changes
2
Show whitespace changes
Inline
Side-by-side
hiredis.c
View file @
8e2c3920
...
...
@@ -596,7 +596,7 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
char
*
cmd
=
NULL
;
/* final command */
int
pos
;
/* position in final command */
sds
current
;
/* current argument */
int
interpolat
ed
=
0
;
/*
did we do interpolation on an
argument? */
int
touch
ed
=
0
;
/*
was the current
argument
touched
? */
char
**
argv
=
NULL
;
int
argc
=
0
,
j
;
int
totlen
=
0
;
...
...
@@ -610,13 +610,14 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
while
(
*
c
!=
'\0'
)
{
if
(
*
c
!=
'%'
||
c
[
1
]
==
'\0'
)
{
if
(
*
c
==
' '
)
{
if
(
sdslen
(
current
)
!=
0
)
{
if
(
touched
)
{
addArgument
(
current
,
&
argv
,
&
argc
,
&
totlen
);
current
=
sdsempty
();
interpolat
ed
=
0
;
touch
ed
=
0
;
}
}
else
{
current
=
sdscatlen
(
current
,
c
,
1
);
touched
=
1
;
}
}
else
{
switch
(
c
[
1
])
{
...
...
@@ -625,14 +626,12 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
size
=
strlen
(
arg
);
if
(
size
>
0
)
current
=
sdscatlen
(
current
,
arg
,
size
);
interpolated
=
1
;
break
;
case
'b'
:
arg
=
va_arg
(
ap
,
char
*
);
size
=
va_arg
(
ap
,
size_t
);
if
(
size
>
0
)
current
=
sdscatlen
(
current
,
arg
,
size
);
interpolated
=
1
;
break
;
case
'%'
:
current
=
sdscat
(
current
,
"%"
);
...
...
@@ -678,7 +677,6 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
_format
[
_l
]
=
'\0'
;
va_copy
(
_cpy
,
ap
);
current
=
sdscatvprintf
(
current
,
_format
,
_cpy
);
interpolated
=
1
;
va_end
(
_cpy
);
/* Update current position (note: outer blocks
...
...
@@ -691,13 +689,14 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
va_arg
(
ap
,
void
);
}
}
touched
=
1
;
c
++
;
}
c
++
;
}
/* Add the last argument if needed */
if
(
interpolated
||
sdslen
(
current
)
!=
0
)
{
if
(
touched
)
{
addArgument
(
current
,
&
argv
,
&
argc
,
&
totlen
);
}
else
{
sdsfree
(
current
);
...
...
test.c
View file @
8e2c3920
...
...
@@ -54,6 +54,12 @@ static void test_format_commands(void) {
len
==
4
+
4
+
(
3
+
2
)
+
4
+
(
3
+
2
)
+
4
+
(
0
+
2
));
free
(
cmd
);
test
(
"Format command with an empty string in between proper interpolations: "
);
len
=
redisFormatCommand
(
&
cmd
,
"SET %s %s"
,
""
,
"foo"
);
test_cond
(
strncmp
(
cmd
,
"*3
\r\n
$3
\r\n
SET
\r\n
$0
\r\n\r\n
$3
\r\n
foo
\r\n
"
,
len
)
==
0
&&
len
==
4
+
4
+
(
3
+
2
)
+
4
+
(
0
+
2
)
+
4
+
(
3
+
2
));
free
(
cmd
);
test
(
"Format command with %%b string interpolation: "
);
len
=
redisFormatCommand
(
&
cmd
,
"SET %b %b"
,
"foo"
,
3
,
"b
\0
r"
,
3
);
test_cond
(
strncmp
(
cmd
,
"*3
\r\n
$3
\r\n
SET
\r\n
$3
\r\n
foo
\r\n
$3
\r\n
b
\0
r
\r\n
"
,
len
)
==
0
&&
...
...
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