summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 147e814)
raw | patch | inline | side by side (parent: 147e814)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 24 Dec 2014 15:40:29 +0000 (16:40 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 24 Dec 2014 15:40:29 +0000 (16:40 +0100) |
That's not necessary given that all strings are null-terminated.
src/utils/proto.c | patch | blob | history | |
t/unit/utils/proto_test.c | patch | blob | history |
diff --git a/src/utils/proto.c b/src/utils/proto.c
index beaa07e98397f18876f27952ab38ed83ff673638..f767fa21a887fdd929a32ea45f16fec656ff6b5f 100644 (file)
--- a/src/utils/proto.c
+++ b/src/utils/proto.c
marshal_binary(char *buf, size_t buf_len, size_t len, const unsigned char *v)
{
uint32_t tmp = htonl((uint32_t)len);
- if (buf_len >= len) {
+ if (buf_len >= sizeof(tmp) + len) {
memcpy(buf, &tmp, sizeof(tmp));
memcpy(buf + sizeof(tmp), v, len);
}
marshal_string(char *buf, size_t buf_len, const char *v)
{
/* The actual string including the terminating null byte. */
- return marshal_binary(buf, buf_len,
- strlen(v) + 1, (const unsigned char *)v);
+ size_t len = strlen(v) + 1;
+ if (buf_len >= len)
+ memcpy(buf, v, len);
+ return len;
} /* marshal_string */
/*
index 9f24c81e43cf1b054be3e45c1c391cbca6636900..c718529894aa369183593bd596d45d8fae46b8f8 100644 (file)
},
{
{ SDB_TYPE_STRING, { .string = "some string" } },
- /* length includes the null byte */
- 20, STRING_TYPE "\0\0\0\xc" "some string\0",
+ 16, STRING_TYPE "some string\0",
},
{
{ SDB_TYPE_DATETIME, { .datetime = 1418923804000000 } },
},
{
{ SDB_TYPE_REGEX, { .re = { "dummy", dummy_re } } },
- 14, REGEX_TYPE "\0\0\0\x6" "dummy\0",
+ 10, REGEX_TYPE "dummy\0",
},
{
{ SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, { .array = {
{
{ SDB_TYPE_STRING | SDB_TYPE_ARRAY, { .array = {
2, string_values } } },
- 25, STRING_ARRAY "\0\0\0\x2" "\0\0\0\x4" "foo\0"
- "\0\0\0\x5" "abcd\0"
+ 17, STRING_ARRAY "\0\0\0\x2" "foo\0" "abcd\0"
},
{
{ SDB_TYPE_DATETIME | SDB_TYPE_ARRAY, { .array = {
{
{ SDB_TYPE_REGEX | SDB_TYPE_ARRAY, { .array = {
1, regex_values } } },
- 24, REGEX_ARRAY "\0\0\0\1" "\0\0\0\xc" "dummy regex\0"
+ 20, REGEX_ARRAY "\0\0\0\1" "dummy regex\0"
},
};
len = sdb_proto_marshal_data(buf, sizeof(buf), &golden_data[i].datum);
fail_unless(len == golden_data[i].expected_len,
- "sdb_proto_marshal_data(<buf>, <size>, %s) = %zi; expected: %zi",
- v, len, golden_data[i].expected_len);
+ "sdb_proto_marshal_data(<buf>, %zu, %s) = %zi; expected: %zi",
+ sizeof(buf), v, len, golden_data[i].expected_len);
if (memcmp(buf, golden_data[i].expected, len) != 0) {
size_t pos;
for (pos = 0; pos < (size_t)len; ++pos)