ctdb-protocol: Add padding data type to handle structure padding
authorAmitay Isaacs <amitay@gmail.com>
Thu, 6 Jul 2017 07:52:25 +0000 (17:52 +1000)
committerMartin Schwenke <martins@samba.org>
Wed, 30 Aug 2017 12:59:22 +0000 (14:59 +0200)
This takes care of alignment sizes, so that it works on both 32-bit and
64-bit architectures.

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/protocol/protocol_basic.c
ctdb/protocol/protocol_private.h
ctdb/tests/src/protocol_basic_test.c

index 92277840adaf04f334234b3b92e26ca4f371baf1..4e86ef32df7aae386a3dad952d84a72763c67381 100644 (file)
@@ -367,3 +367,36 @@ int ctdb_timeval_pull(uint8_t *buf, size_t buflen, struct timeval *out,
        *npull = sizeof(struct timeval);
        return 0;
 }
+
+/*
+ * Dummy type to tackle structure padding
+ */
+
+size_t ctdb_padding_len(int count)
+{
+       return count % SIZEOF_VOID_P;
+}
+
+void ctdb_padding_push(int count, uint8_t *buf, size_t *npush)
+{
+       uint8_t padding[count];
+       size_t aligned_count = count % SIZEOF_VOID_P;
+
+       if (aligned_count > 0) {
+               memset(padding, 0, aligned_count);
+               memcpy(buf, padding, aligned_count);
+       }
+       *npush = aligned_count;
+}
+
+int ctdb_padding_pull(uint8_t *buf, size_t buflen, int count, size_t *npull)
+{
+       size_t aligned_count = count % SIZEOF_VOID_P;
+
+       if (buflen < aligned_count) {
+               return EMSGSIZE;
+       }
+
+       *npull = aligned_count;
+       return 0;
+}
index 625c277567c27f1b33731b6fb2645359564ea48b..0816bd15d99887af10113362b3f54c46135ee4dc 100644 (file)
@@ -81,6 +81,10 @@ void ctdb_timeval_push(struct timeval *in, uint8_t *buf, size_t *npush);
 int ctdb_timeval_pull(uint8_t *buf, size_t buflen, struct timeval *out,
                      size_t *npull);
 
+size_t ctdb_padding_len(int count);
+void ctdb_padding_push(int count, uint8_t *buf, size_t *npush);
+int ctdb_padding_pull(uint8_t *buf, size_t buflen, int count, size_t *npull);
+
 /*
  * From protocol/protocol_types.c
  */
index f74aad06fbae69f78b456e8e46ef039238829b09..8954035677b25460bf4284cb5ef87d80007efb12 100644 (file)
@@ -61,6 +61,24 @@ PROTOCOL_TYPE2_TEST(const char *, ctdb_stringn);
 PROTOCOL_TYPE1_TEST(pid_t, ctdb_pid);
 PROTOCOL_TYPE1_TEST(struct timeval, ctdb_timeval);
 
+static void test_ctdb_padding(void)
+{
+       int padding;
+       size_t buflen, np = 0;
+       int ret;
+
+       padding = rand_int(8);
+
+       buflen = ctdb_padding_len(padding);
+       assert(buflen < sizeof(BUFFER));
+       ctdb_padding_push(padding, BUFFER, &np);
+       assert(np == buflen);
+       np = 0;
+       ret = ctdb_padding_pull(BUFFER, buflen, padding, &np);
+       assert(ret == 0);
+       assert(np == buflen);
+}
+
 int main(int argc, char *argv[])
 {
        if (argc == 2) {
@@ -84,5 +102,7 @@ int main(int argc, char *argv[])
        TEST_FUNC(ctdb_pid)();
        TEST_FUNC(ctdb_timeval)();
 
+       test_ctdb_padding();
+
        return 0;
 }