summaryrefslogtreecommitdiff
path: root/ctdb/protocol/protocol_basic.c
diff options
context:
space:
mode:
authorAmitay Isaacs <amitay@gmail.com>2017-04-20 12:45:24 +1000
committerMartin Schwenke <martins@samba.org>2017-08-30 14:59:22 +0200
commit1d6840982b70cac7dca9c440af7f464f9244a5f6 (patch)
treebd56524f319dd72b13c9a5ea07b9f39b9725cb2e /ctdb/protocol/protocol_basic.c
parent120210670ce720de3f32064aa27da376031ef6cf (diff)
downloadsamba-1d6840982b70cac7dca9c440af7f464f9244a5f6.tar.gz
ctdb-protocol: Fix marshalling for a string with length
Signed-off-by: Amitay Isaacs <amitay@gmail.com> Reviewed-by: Martin Schwenke <martin@meltin.net>
Diffstat (limited to 'ctdb/protocol/protocol_basic.c')
-rw-r--r--ctdb/protocol/protocol_basic.c62
1 files changed, 29 insertions, 33 deletions
diff --git a/ctdb/protocol/protocol_basic.c b/ctdb/protocol/protocol_basic.c
index e112c5bb887..10824cf4ed7 100644
--- a/ctdb/protocol/protocol_basic.c
+++ b/ctdb/protocol/protocol_basic.c
@@ -271,55 +271,51 @@ int ctdb_string_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
return 0;
}
-struct stringn_wire {
- uint32_t length;
- uint8_t str[1];
-};
-
-size_t ctdb_stringn_len(const char *str)
+size_t ctdb_stringn_len(const char **in)
{
- return sizeof(uint32_t) + ctdb_string_len(&str);
+ uint32_t u32 = ctdb_string_len(in);
+
+ return ctdb_uint32_len(&u32) + u32;
}
-void ctdb_stringn_push(const char *str, uint8_t *buf)
+void ctdb_stringn_push(const char **in, uint8_t *buf, size_t *npush)
{
- struct stringn_wire *wire = (struct stringn_wire *)buf;
- size_t np;
+ size_t offset = 0, np;
+ uint32_t u32 = ctdb_string_len(in);
- wire->length = ctdb_string_len(&str);
- ctdb_string_push(&str, wire->str, &np);
+ ctdb_uint32_push(&u32, buf+offset, &np);
+ offset += np;
+
+ ctdb_string_push(in, buf+offset, &np);
+ offset += np;
+
+ *npush = offset;
}
int ctdb_stringn_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
- const char **out)
+ const char **out, size_t *npull)
{
- char *str;
- struct stringn_wire *wire = (struct stringn_wire *)buf;
+ size_t offset = 0, np;
+ uint32_t u32;
+ int ret;
- if (buflen < sizeof(uint32_t)) {
- return EMSGSIZE;
- }
- if (wire->length > buflen) {
- return EMSGSIZE;
- }
- if (sizeof(uint32_t) + wire->length < sizeof(uint32_t)) {
- return EMSGSIZE;
- }
- if (buflen < sizeof(uint32_t) + wire->length) {
- return EMSGSIZE;
+ ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
+ if (ret != 0) {
+ return ret;
}
+ offset += np;
- if (wire->length == 0) {
- *out = NULL;
- return 0;
+ if (buflen-offset < u32) {
+ return EMSGSIZE;
}
- str = talloc_strndup(mem_ctx, (char *)wire->str, wire->length);
- if (str == NULL) {
- return ENOMEM;
+ ret = ctdb_string_pull(buf+offset, u32, mem_ctx, out, &np);
+ if (ret != 0) {
+ return ret;
}
+ offset += np;
- *out = str;
+ *npull = offset;
return 0;
}