From b052576b8a85ac17a80fb7b8fea79d0502f8d95b Mon Sep 17 00:00:00 2001 From: "Schanzenbach, Martin" Date: Sun, 24 Jun 2018 17:59:13 +0200 Subject: [PATCH] fix https://gnunet.org/bugs/view.php?id=4927 --- src/gnsrecord/gnsrecord_serialization.c | 34 ++++++++++++++++++++----- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/gnsrecord/gnsrecord_serialization.c b/src/gnsrecord/gnsrecord_serialization.c index adbf02755..82b09c24e 100644 --- a/src/gnsrecord/gnsrecord_serialization.c +++ b/src/gnsrecord/gnsrecord_serialization.c @@ -89,6 +89,7 @@ GNUNET_GNSRECORD_records_get_size (unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd) { size_t ret; + size_t no_padding; ret = sizeof (struct NetworkRecord) * rd_count; for (unsigned int i=0;irecord_type) + return ret; + /** + * Efficiently round up to the next + * power of 2 for padding + * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 + */ + no_padding = ret; + ret--; + ret |= ret >> 1; + ret |= ret >> 2; + ret |= ret >> 4; + ret |= ret >> 8; + ret |= ret >> 16; + ret++; + //If padding record does not fit, pad again. + if ((ret - no_padding) < sizeof (struct NetworkRecord)) + ret = ret << 1; return (ssize_t) ret; } @@ -135,9 +155,9 @@ GNUNET_GNSRECORD_records_get_size (unsigned int rd_count, */ ssize_t GNUNET_GNSRECORD_records_serialize (unsigned int rd_count, - const struct GNUNET_GNSRECORD_Data *rd, - size_t dest_size, - char *dest) + const struct GNUNET_GNSRECORD_Data *rd, + size_t dest_size, + char *dest) { struct NetworkRecord rec; size_t off; @@ -190,7 +210,7 @@ GNUNET_GNSRECORD_records_serialize (unsigned int rd_count, } #endif } - return off; + return dest_size; } @@ -205,9 +225,9 @@ GNUNET_GNSRECORD_records_serialize (unsigned int rd_count, */ int GNUNET_GNSRECORD_records_deserialize (size_t len, - const char *src, - unsigned int rd_count, - struct GNUNET_GNSRECORD_Data *dest) + const char *src, + unsigned int rd_count, + struct GNUNET_GNSRECORD_Data *dest) { struct NetworkRecord rec; size_t off; -- 2.25.1