From e3e8ce4b81d617b50f91e238d4e93a5075735658 Mon Sep 17 00:00:00 2001 From: Amitay Isaacs Date: Tue, 3 May 2016 16:46:29 +1000 Subject: [PATCH] ctdb-protocol: Fix marshaling of uint arrays Signed-off-by: Amitay Isaacs Reviewed-by: Volker Lendecke --- ctdb/protocol/protocol_types.c | 36 ++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/ctdb/protocol/protocol_types.c b/ctdb/protocol/protocol_types.c index fa11bc2bcc26..d1890cf608e6 100644 --- a/ctdb/protocol/protocol_types.c +++ b/ctdb/protocol/protocol_types.c @@ -97,7 +97,9 @@ size_t ctdb_uint8_array_len(struct ctdb_uint8_array *array) void ctdb_uint8_array_push(struct ctdb_uint8_array *array, uint8_t *buf) { - memcpy(buf, array->val, array->num * sizeof(uint8_t)); + if (array->num > 0) { + memcpy(buf, array->val, array->num * sizeof(uint8_t)); + } } int ctdb_uint8_array_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, @@ -112,12 +114,16 @@ int ctdb_uint8_array_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, array->num = buflen / sizeof(uint8_t); - array->val = talloc_array(array, uint8_t, array->num); - if (array->val == NULL) { - talloc_free(array); - return ENOMEM; + if (array->num > 0) { + array->val = talloc_array(array, uint8_t, array->num); + if (array->val == NULL) { + talloc_free(array); + return ENOMEM; + } + memcpy(array->val, buf, buflen); + } else { + array->val = NULL; } - memcpy(array->val, buf, buflen); *out = array; return 0; @@ -130,7 +136,9 @@ size_t ctdb_uint64_array_len(struct ctdb_uint64_array *array) void ctdb_uint64_array_push(struct ctdb_uint64_array *array, uint8_t *buf) { - memcpy(buf, array->val, array->num * sizeof(uint64_t)); + if (array->num > 0) { + memcpy(buf, array->val, array->num * sizeof(uint64_t)); + } } int ctdb_uint64_array_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, @@ -145,12 +153,16 @@ int ctdb_uint64_array_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, array->num = buflen / sizeof(uint64_t); - array->val = talloc_array(array, uint64_t, array->num); - if (array->val == NULL) { - talloc_free(array); - return ENOMEM; + if (array->num > 0) { + array->val = talloc_array(array, uint64_t, array->num); + if (array->val == NULL) { + talloc_free(array); + return ENOMEM; + } + memcpy(array->val, buf, buflen); + } else { + array->val = NULL; } - memcpy(array->val, buf, buflen); *out = array; return 0; -- 2.34.1