From 7bffaedde836a26701b3096e8122f24d96815111 Mon Sep 17 00:00:00 2001 From: Erik Boasson Date: Fri, 7 Jun 2019 14:56:06 +0200 Subject: [PATCH] Clear padding in outgoing messages Padding used to not be cleared in this code base, but that has the downside of valgrind reporting nuisance warnings (which could be fixed using valgrind's programmatic interface) but also of potentially leaking information. The cost of clearing the padding appears to be insignificant compared to the cost of doing the real work, and so it is probably best to just clear it. Signed-off-by: Erik Boasson --- src/core/ddsi/src/ddsi_serdata_default.c | 11 ++++------- src/core/ddsi/src/q_ddsi_discovery.c | 1 + src/core/ddsi/src/q_plist.c | 9 +++++++-- src/core/ddsi/src/q_xmsg.c | 13 +++++-------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/core/ddsi/src/ddsi_serdata_default.c b/src/core/ddsi/src/ddsi_serdata_default.c index 630c6c0..8c5b38c 100644 --- a/src/core/ddsi/src/ddsi_serdata_default.c +++ b/src/core/ddsi/src/ddsi_serdata_default.c @@ -102,17 +102,13 @@ static void *serdata_default_append (struct ddsi_serdata_default **d, size_t n) static void *serdata_default_append_aligned (struct ddsi_serdata_default **d, size_t n, size_t a) { -#if CLEAR_PADDING - size_t pos0 = st->pos; -#endif + size_t pos0 = (*d)->pos; char *p; assert (ispowerof2_size (a)); (*d)->pos = (uint32_t) alignup_size ((*d)->pos, a); p = serdata_default_append (d, n); -#if CLEAR_PADDING - if (p && (*d)->pos > pos0) - memset ((*d)->data + pos0, 0, (*d)->pos - pos0); -#endif + while (pos0 < (*d)->pos) + (*d)->data[pos0++] = 0; return p; } @@ -512,6 +508,7 @@ static struct ddsi_serdata *serdata_default_from_sample_rawcdr (const struct dds return NULL; assert (sample->keysize <= 16); serdata_default_append_blob (&d, 1, sample->size, sample->blob); + serdata_default_append_aligned (&d, 0, 4); d->keyhash.m_set = 1; d->keyhash.m_iskey = 1; if (sample->keysize == 0) diff --git a/src/core/ddsi/src/q_ddsi_discovery.c b/src/core/ddsi/src/q_ddsi_discovery.c index 3973840..9218611 100644 --- a/src/core/ddsi/src/q_ddsi_discovery.c +++ b/src/core/ddsi/src/q_ddsi_discovery.c @@ -289,6 +289,7 @@ int spdp_write (struct participant *pp) /* Add PrismTech specific version information */ { ps.present |= PP_PRISMTECH_PARTICIPANT_VERSION_INFO; + memset (&ps.prismtech_participant_version_info, 0, sizeof (ps.prismtech_participant_version_info)); ps.prismtech_participant_version_info.version = 0; ps.prismtech_participant_version_info.flags = NN_PRISMTECH_FL_DDSI2_PARTICIPANT_FLAG | diff --git a/src/core/ddsi/src/q_plist.c b/src/core/ddsi/src/q_plist.c index 7d396c3..0cf2da1 100644 --- a/src/core/ddsi/src/q_plist.c +++ b/src/core/ddsi/src/q_plist.c @@ -158,8 +158,13 @@ static const void *deser_generic_src (const void * __restrict src, size_t *srcof static void *ser_generic_align4 (char * __restrict p, size_t * __restrict off) { - *off = align4size (*off); - return p + *off; + const size_t off1 = align4size (*off); + size_t pad = off1 - *off; + char *dst = p + *off; + *off = off1; + while (pad--) + *dst++ = 0; + return dst; } static dds_return_t deser_uint32 (uint32_t *dst, const struct dd * __restrict dd, size_t * __restrict off) diff --git a/src/core/ddsi/src/q_xmsg.c b/src/core/ddsi/src/q_xmsg.c index 8f942b0..f21214d 100644 --- a/src/core/ddsi/src/q_xmsg.c +++ b/src/core/ddsi/src/q_xmsg.c @@ -506,7 +506,7 @@ void *nn_xmsg_submsg_from_marker (struct nn_xmsg *msg, struct nn_xmsg_marker mar return msg->data->payload + marker.offset; } -void * nn_xmsg_append (struct nn_xmsg *m, struct nn_xmsg_marker *marker, size_t sz) +void *nn_xmsg_append (struct nn_xmsg *m, struct nn_xmsg_marker *marker, size_t sz) { static const size_t a = 4; @@ -809,13 +809,10 @@ void *nn_xmsg_addpar (struct nn_xmsg *m, nn_parameterid_t pid, size_t len) phdr->parameterid = pid; phdr->length = (uint16_t) len4; p = (char *) (phdr + 1); - if (len4 > len) - { - /* zero out padding bytes added to satisfy parameter alignment -- - alternative: zero out, but this way valgrind/purify can tell us - where we forgot to initialize something */ - memset (p + len, 0, len4 - len); - } + /* zero out padding bytes added to satisfy parameter alignment: this way + valgrind can tell us where we forgot to initialize something */ + while (len < len4) + p[len++] = 0; return p; }