From 4b571a6093c1936d2b8e8d01836afeeeed7a0eda Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Mon, 12 Mar 2018 22:56:29 +0100 Subject: [PATCH] marshal: make serialization usable for equalities of different objects Ignored fields should be zeroed out. --- src/lldpd-structs.h | 32 ++++++++++++++++---------------- src/marshal.c | 9 ++++++--- src/marshal.h | 13 +++++++++---- tests/check_marshal.c | 2 +- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/lldpd-structs.h b/src/lldpd-structs.h index b44a43f3..56382d5e 100644 --- a/src/lldpd-structs.h +++ b/src/lldpd-structs.h @@ -196,8 +196,8 @@ struct lldpd_chassis { /* WARNING: any change to this structure should also be reflected into `lldpd_copy_chassis()` which is not using marshaling. */ MARSHAL_BEGIN(lldpd_chassis) -MARSHAL_IGNORE(lldpd_chassis, c_entries.tqe_next) -MARSHAL_IGNORE(lldpd_chassis, c_entries.tqe_prev) +MARSHAL_IGNORE(lldpd_chassis, c_entries.tqe_next, void*) +MARSHAL_IGNORE(lldpd_chassis, c_entries.tqe_prev, void*) MARSHAL_FSTR(lldpd_chassis, c_id, c_id_len) MARSHAL_STR(lldpd_chassis, c_name) MARSHAL_STR(lldpd_chassis, c_descr) @@ -288,7 +288,7 @@ struct lldpd_port { MARSHAL_BEGIN(lldpd_port) MARSHAL_TQE(lldpd_port, p_entries) MARSHAL_POINTER(lldpd_port, lldpd_chassis, p_chassis) -MARSHAL_IGNORE(lldpd_port, p_lastframe) +MARSHAL_IGNORE(lldpd_port, p_lastframe, void*) MARSHAL_FSTR(lldpd_port, p_id, p_id_len) MARSHAL_STR(lldpd_port, p_descr) #ifdef ENABLE_LLDPMED @@ -487,19 +487,19 @@ struct lldpd_hardware { #endif }; MARSHAL_BEGIN(lldpd_hardware) -MARSHAL_IGNORE(lldpd_hardware, h_entries.tqe_next) -MARSHAL_IGNORE(lldpd_hardware, h_entries.tqe_prev) -MARSHAL_IGNORE(lldpd_hardware, h_ops) -MARSHAL_IGNORE(lldpd_hardware, h_data) -MARSHAL_IGNORE(lldpd_hardware, h_cfg) -MARSHAL_IGNORE(lldpd_hardware, h_lport_previous) -MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_len) -MARSHAL_IGNORE(lldpd_hardware, h_lchassis_previous_id_subtype) -MARSHAL_IGNORE(lldpd_hardware, h_lchassis_previous_id) -MARSHAL_IGNORE(lldpd_hardware, h_lchassis_previous_id_len) -MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_id_subtype) -MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_id) -MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_id_len) +MARSHAL_IGNORE(lldpd_hardware, h_entries.tqe_next, void*) +MARSHAL_IGNORE(lldpd_hardware, h_entries.tqe_prev, void*) +MARSHAL_IGNORE(lldpd_hardware, h_ops, void*) +MARSHAL_IGNORE(lldpd_hardware, h_data, void*) +MARSHAL_IGNORE(lldpd_hardware, h_cfg, void*) +MARSHAL_IGNORE(lldpd_hardware, h_lport_previous, void*) +MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_len, ssize_t) +MARSHAL_IGNORE(lldpd_hardware, h_lchassis_previous_id_subtype, u_int8_t) +MARSHAL_IGNORE(lldpd_hardware, h_lchassis_previous_id, void*) +MARSHAL_IGNORE(lldpd_hardware, h_lchassis_previous_id_len, int) +MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_id_subtype, u_int8_t) +MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_id, void*) +MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_id_len, int) MARSHAL_SUBSTRUCT(lldpd_hardware, lldpd_port, h_lport) MARSHAL_SUBTQ(lldpd_hardware, lldpd_port, h_rports) MARSHAL_END(lldpd_hardware); diff --git a/src/marshal.c b/src/marshal.c index 16dabe57..120bfdca 100644 --- a/src/marshal.c +++ b/src/marshal.c @@ -80,7 +80,7 @@ marshal_serialize_(struct marshal_info *mi, void *unserialized, void **input, struct marshal_serialized *new = NULL, *serialized = NULL; int dummy = 1; - log_debug("marshal", "start serialization of %s", mi->name); + log_debug("marshal", "start serialization of %s (%zu)", mi->name, mi->size); /* Check if we have already serialized this one. */ if (!refs) { @@ -139,7 +139,10 @@ marshal_serialize_(struct marshal_info *mi, void *unserialized, void **input, size_t padlen; void *source; void *target = NULL; - if (current->kind == ignore) continue; + if (current->kind == ignore) { + memset((unsigned char *)serialized->object + current->offset, 0, current->offset2); + continue; + } if (current->kind == pointer) { memcpy(&source, (unsigned char *)unserialized + current->offset, @@ -314,7 +317,7 @@ marshal_unserialize_(struct marshal_info *mi, void *buffer, size_t len, void **o new = (unsigned char *)*output + current->offset; if (current->kind == ignore) { memset((unsigned char *)*output + current->offset, - 0, sizeof(void *)); + 0, current->offset2); continue; } if (current->kind == pointer) { diff --git a/src/marshal.h b/src/marshal.h index b0e80320..cec67ef1 100644 --- a/src/marshal.h +++ b/src/marshal.h @@ -89,6 +89,11 @@ extern struct marshal_info marshal_info_ignore; .offset2 = offsetof(struct type, len), \ .kind = pointer, \ .mi = &marshal_info_fstring }, +#define MARSHAL_IGNORE(type, member, subtype) \ + { .offset = offsetof(struct type, member), \ + .offset2 = sizeof(subtype), \ + .kind = ignore, \ + .mi = &marshal_info_ignore }, #define MARSHAL_END(type) MARSHAL_SUBINFO_NULL }}; \ MARSHAL_HELPER_FUNCTIONS(type, struct type) #else @@ -98,26 +103,26 @@ extern struct marshal_info marshal_info_ignore; #define MARSHAL_BEGIN(type) extern struct marshal_info MARSHAL_INFO(type); #define MARSHAL_ADD(...) #define MARSHAL_FSTR(...) +#define MARSHAL_IGNORE(...) #define MARSHAL_END(type) MARSHAL_HELPER_FUNCTIONS(type, struct type) #endif /* Shortcuts */ #define MARSHAL_POINTER(...) MARSHAL_ADD(pointer, ##__VA_ARGS__) #define MARSHAL_SUBSTRUCT(...) MARSHAL_ADD(substruct, ##__VA_ARGS__) #define MARSHAL_STR(type, member) MARSHAL_ADD(pointer, type, string, member) -#define MARSHAL_IGNORE(type, member) MARSHAL_ADD(ignore, type, ignore, member) #define MARSHAL_TQE(type, field) \ MARSHAL_POINTER(type, type, field.tqe_next) \ - MARSHAL_IGNORE(type, field.tqe_prev) + MARSHAL_IGNORE(type, field.tqe_prev, void*) /* Support for TAILQ list is partial. Access to last and previous elements is not available. Some operations are therefore not possible. However, TAILQ_FOREACH is still available. */ #define MARSHAL_TQH(type, subtype) \ MARSHAL_POINTER(type, subtype, tqh_first) \ - MARSHAL_IGNORE(type, tqh_last) + MARSHAL_IGNORE(type, tqh_last, void*) #define MARSHAL_SUBTQ(type, subtype, field) \ MARSHAL_POINTER(type, subtype, field.tqh_first) \ - MARSHAL_IGNORE(type, field.tqh_last) + MARSHAL_IGNORE(type, field.tqh_last, void*) #define MARSHAL(type) \ MARSHAL_BEGIN(type) \ MARSHAL_END(type) diff --git a/tests/check_marshal.c b/tests/check_marshal.c index 4f934462..d7190e6c 100644 --- a/tests/check_marshal.c +++ b/tests/check_marshal.c @@ -798,7 +798,7 @@ struct struct_ignore { int t3; }; MARSHAL_BEGIN(struct_ignore) -MARSHAL_IGNORE(struct_ignore, t2) +MARSHAL_IGNORE(struct_ignore, t2, void*) MARSHAL_END(struct_ignore); START_TEST(test_ignore) { -- 2.39.5