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
37a840dc
Commit
37a840dc
authored
Jul 10, 2013
by
Wolfgang Richter
Committed by
Pieter Noordhuis
Jul 10, 2013
Browse files
Emphasize size_t length for %b formatting
Closes #121.
parent
afc462d3
Changes
4
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
37a840dc
...
@@ -68,7 +68,7 @@ When you need to pass binary safe strings in a command, the `%b` specifier can b
...
@@ -68,7 +68,7 @@ When you need to pass binary safe strings in a command, the `%b` specifier can b
used. Together with a pointer to the string, it requires a
`size_t`
length argument
used. Together with a pointer to the string, it requires a
`size_t`
length argument
of the string:
of the string:
reply = redisCommand(context, "SET foo %b", value, valuelen);
reply = redisCommand(context, "SET foo %b", value,
(size_t)
valuelen);
Internally, Hiredis splits the command in different arguments and will
Internally, Hiredis splits the command in different arguments and will
convert it to the protocol used to communicate with Redis.
convert it to the protocol used to communicate with Redis.
...
...
examples/example.c
View file @
37a840dc
...
@@ -32,7 +32,7 @@ int main(void) {
...
@@ -32,7 +32,7 @@ int main(void) {
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
/* Set a key using binary safe API */
/* Set a key using binary safe API */
reply
=
redisCommand
(
c
,
"SET %b %b"
,
"bar"
,
3
,
"hello"
,
5
);
reply
=
redisCommand
(
c
,
"SET %b %b"
,
"bar"
,
(
size_t
)
3
,
"hello"
,
(
size_t
)
5
);
printf
(
"SET (binary API): %s
\n
"
,
reply
->
str
);
printf
(
"SET (binary API): %s
\n
"
,
reply
->
str
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
...
...
hiredis.c
View file @
37a840dc
...
@@ -917,7 +917,7 @@ err:
...
@@ -917,7 +917,7 @@ err:
* %b represents a binary safe string
* %b represents a binary safe string
*
*
* When using %b you need to provide both the pointer to the string
* When using %b you need to provide both the pointer to the string
* and the length in bytes. Examples:
* and the length in bytes
as a size_t
. Examples:
*
*
* len = redisFormatCommand(target, "GET %s", mykey);
* len = redisFormatCommand(target, "GET %s", mykey);
* len = redisFormatCommand(target, "SET %s %b", mykey, myval, myvallen);
* len = redisFormatCommand(target, "SET %s %b", mykey, myval, myvallen);
...
...
test.c
View file @
37a840dc
...
@@ -130,13 +130,13 @@ static void test_format_commands(void) {
...
@@ -130,13 +130,13 @@ static void test_format_commands(void) {
free
(
cmd
);
free
(
cmd
);
test
(
"Format command with %%b string interpolation: "
);
test
(
"Format command with %%b string interpolation: "
);
len
=
redisFormatCommand
(
&
cmd
,
"SET %b %b"
,
"foo"
,
3
,
"b
\0
r"
,
3
);
len
=
redisFormatCommand
(
&
cmd
,
"SET %b %b"
,
"foo"
,
(
size_t
)
3
,
"b
\0
r"
,
(
size_t
)
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
&&
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
&&
len
==
4
+
4
+
(
3
+
2
)
+
4
+
(
3
+
2
)
+
4
+
(
3
+
2
));
len
==
4
+
4
+
(
3
+
2
)
+
4
+
(
3
+
2
)
+
4
+
(
3
+
2
));
free
(
cmd
);
free
(
cmd
);
test
(
"Format command with %%b and an empty string: "
);
test
(
"Format command with %%b and an empty string: "
);
len
=
redisFormatCommand
(
&
cmd
,
"SET %b %b"
,
"foo"
,
3
,
""
,
0
);
len
=
redisFormatCommand
(
&
cmd
,
"SET %b %b"
,
"foo"
,
(
size_t
)
3
,
""
,(
size_t
)
0
);
test_cond
(
strncmp
(
cmd
,
"*3
\r\n
$3
\r\n
SET
\r\n
$3
\r\n
foo
\r\n
$0
\r\n\r\n
"
,
len
)
==
0
&&
test_cond
(
strncmp
(
cmd
,
"*3
\r\n
$3
\r\n
SET
\r\n
$3
\r\n
foo
\r\n
$0
\r\n\r\n
"
,
len
)
==
0
&&
len
==
4
+
4
+
(
3
+
2
)
+
4
+
(
3
+
2
)
+
4
+
(
0
+
2
));
len
==
4
+
4
+
(
3
+
2
)
+
4
+
(
3
+
2
)
+
4
+
(
0
+
2
));
free
(
cmd
);
free
(
cmd
);
...
@@ -182,7 +182,7 @@ static void test_format_commands(void) {
...
@@ -182,7 +182,7 @@ static void test_format_commands(void) {
FLOAT_WIDTH_TEST
(
double
);
FLOAT_WIDTH_TEST
(
double
);
test
(
"Format command with invalid printf format: "
);
test
(
"Format command with invalid printf format: "
);
len
=
redisFormatCommand
(
&
cmd
,
"key:%08p %b"
,(
void
*
)
1234
,
"foo"
,
3
);
len
=
redisFormatCommand
(
&
cmd
,
"key:%08p %b"
,(
void
*
)
1234
,
"foo"
,
(
size_t
)
3
);
test_cond
(
len
==
-
1
);
test_cond
(
len
==
-
1
);
const
char
*
argv
[
3
];
const
char
*
argv
[
3
];
...
@@ -335,7 +335,7 @@ static void test_blocking_connection(struct config config) {
...
@@ -335,7 +335,7 @@ static void test_blocking_connection(struct config config) {
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
test
(
"%%b String interpolation works: "
);
test
(
"%%b String interpolation works: "
);
reply
=
redisCommand
(
c
,
"SET %b %b"
,
"foo"
,
3
,
"hello
\x00
world"
,
11
);
reply
=
redisCommand
(
c
,
"SET %b %b"
,
"foo"
,
(
size_t
)
3
,
"hello
\x00
world"
,
(
size_t
)
11
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
reply
=
redisCommand
(
c
,
"GET foo"
);
reply
=
redisCommand
(
c
,
"GET foo"
);
test_cond
(
reply
->
type
==
REDIS_REPLY_STRING
&&
test_cond
(
reply
->
type
==
REDIS_REPLY_STRING
&&
...
...
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