Code

proto: Don't include a string's length in the wire format.
authorSebastian Harl <sh@tokkee.org>
Wed, 24 Dec 2014 15:40:29 +0000 (16:40 +0100)
committerSebastian 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
t/unit/utils/proto_test.c

index beaa07e98397f18876f27952ab38ed83ff673638..f767fa21a887fdd929a32ea45f16fec656ff6b5f 100644 (file)
@@ -91,7 +91,7 @@ static ssize_t
 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);
        }
@@ -102,8 +102,10 @@ static ssize_t
 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)
@@ -87,8 +87,7 @@ START_TEST(test_marshal_data)
                },
                {
                        { 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 } },
@@ -101,7 +100,7 @@ START_TEST(test_marshal_data)
                },
                {
                        { 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 = {
@@ -118,8 +117,7 @@ START_TEST(test_marshal_data)
                {
                        { 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 = {
@@ -136,7 +134,7 @@ START_TEST(test_marshal_data)
                {
                        { 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"
                },
        };
 
@@ -159,8 +157,8 @@ START_TEST(test_marshal_data)
 
                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)