From 2a3f8105707fe384fc4fb05971d68328c6e89305 Mon Sep 17 00:00:00 2001 From: Henri Gourvest Date: Fri, 30 Nov 2018 01:06:13 +0100 Subject: [PATCH 1/3] use memcpy on unaligned pointer to avoid crash on armv7 (BUS_ADRALN) avoid unnecessary calls to strlen --- src/zhash.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/zhash.c b/src/zhash.c index 3e9581f23..c40e97a30 100644 --- a/src/zhash.c +++ b/src/zhash.c @@ -685,7 +685,9 @@ zhash_pack (zhash_t *self) zframe_t *frame = zframe_new (NULL, frame_size); if (!frame) return NULL; + byte *needle = zframe_data (frame); + // Store size as number-4 *(uint32_t *) needle = htonl ((uint32_t) self->size); needle += 4; @@ -693,16 +695,18 @@ zhash_pack (zhash_t *self) item_t *item = self->items [index]; while (item) { // Store key as string - *needle++ = (byte) strlen ((char *) item->key); - memcpy (needle, item->key, strlen ((char *) item->key)); - needle += strlen ((char *) item->key); + size_t length = strlen ((char *) item->key); + *needle++ = (byte) length; + memcpy (needle, item->key, length); + needle += length; // Store value as longstr - size_t length = strlen ((char *) item->value); - *(uint32_t *) needle = htonl ((u_long) length); + length = strlen (item->value); + uint32_t serialize = htonl ((uint32_t) length); + memcpy (needle, &serialize, 4); needle += 4; - memcpy (needle, (char *) item->value, strlen ((char *) item->value)); - needle += strlen ((char *) item->value); + memcpy (needle, (char *) item->value, length); + needle += length; item = item->next; } } From f36ced8518c5af7129f947e35551c52148f4d610 Mon Sep 17 00:00:00 2001 From: Henri Gourvest Date: Fri, 30 Nov 2018 08:58:41 +0100 Subject: [PATCH 2/3] zlistx and zhashx pack methods use memcpy on unaligned pointer to avoid crash on armv7 (BUS_ADRALN) avoid unnecessary calls to strlen --- src/zhashx.c | 20 +++++++++++--------- src/zlistx.c | 19 ++++++++++--------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/zhashx.c b/src/zhashx.c index 1743c5914..c4aefa394 100644 --- a/src/zhashx.c +++ b/src/zhashx.c @@ -813,16 +813,18 @@ zhashx_pack_own (zhashx_t *self, zhashx_serializer_fn serializer) item_t *item = self->items [index]; while (item) { // Store key as string - *needle++ = (byte) strlen ((char *) item->key); - memcpy (needle, item->key, strlen ((char *) item->key)); - needle += strlen ((char *) item->key); + size_t length = strlen ((char *) item->key); + *needle++ = (byte) length; + memcpy (needle, item->key, length); + needle += length; // Store value as longstr - size_t lenth = strlen (values [vindex]); - *(uint32_t *) needle = htonl ((u_long) lenth); + length = strlen (values [vindex]); + uint32_t serialize = htonl ((u_long) length); + memcpy (needle, &serialize, 4); needle += 4; - memcpy (needle, (char *) values [vindex], strlen ((char *) values [vindex])); - needle += strlen ((char *) values [vindex]); + memcpy (needle, values [vindex], length); + needle += length; item = item->next; // Destroy serialized value @@ -879,7 +881,7 @@ zhashx_unpack_own (zframe_t *frame, zhashx_deserializer_fn deserializer) // Hash will free values in destructor zhashx_set_destructor (self, (zhashx_destructor_fn *) zstr_free); - + assert (frame); if (zframe_size (frame) < 4) return self; // Arguable... @@ -929,7 +931,7 @@ zhashx_unpack_own (zframe_t *frame, zhashx_deserializer_fn deserializer) } if (self) - zhashx_set_duplicator (self, (zhashx_duplicator_fn *) strdup); + zhashx_set_duplicator (self, (zhashx_duplicator_fn *) strdup); return self; } diff --git a/src/zlistx.c b/src/zlistx.c index e3e26fa03..30e4c8376 100644 --- a/src/zlistx.c +++ b/src/zlistx.c @@ -651,7 +651,7 @@ zlistx_pack (zlistx_t *self) { assert (self); // First, calculate the packed data size - size_t frame_size = 4; // List size, number-4 + size_t frame_size = 4; // List size, number-4 char* item = (char *) zlistx_first (self); while (item) { @@ -671,7 +671,8 @@ zlistx_pack (zlistx_t *self) { item = (char *) zlistx_first (self); while (item) { size_t length = strlen (item); - *(uint32_t *) needle = htonl ((u_long) length); + uint32_t serialize = htonl ((u_long) length); + memcpy (needle, &serialize, 4); needle += 4; memcpy (needle, item, length); needle += length; @@ -716,24 +717,24 @@ zlistx_unpack (zframe_t *frame) { item[length] = 0; needle += length; - if (!zlistx_add_end (self, item)) { + if (!zlistx_add_end (self, item)) { zlistx_destroy (&self); break; } } else { zlistx_destroy (&self); - break; + break; } } else { zlistx_destroy (&self); - break; + break; } } if (self) - zlistx_set_duplicator (self, (zlistx_duplicator_fn *) strdup); - + zlistx_set_duplicator (self, (zlistx_duplicator_fn *) strdup); + return self; } @@ -849,7 +850,7 @@ zlistx_test (bool verbose) assert (streq (string, "four")); // Test pack/unpack methods - zframe_t *frame = zlistx_pack (list); + zframe_t *frame = zlistx_pack (list); copy = zlistx_unpack (frame); assert (copy); zframe_destroy (&frame); @@ -858,7 +859,7 @@ zlistx_test (bool verbose) char *item_orig = (char *) zlistx_first (list); char *item_copy = (char *) zlistx_first (copy); while (item_orig) { - assert (strcmp(item_orig, item_copy) == 0); + assert (strcmp(item_orig, item_copy) == 0); item_orig = (char *) zlistx_next (list); item_copy = (char *) zlistx_next (copy); } From b83e42f3328c3798d7ff62f162587a28123c524a Mon Sep 17 00:00:00 2001 From: Henri Gourvest Date: Fri, 30 Nov 2018 11:11:03 +0100 Subject: [PATCH 3/3] make compiler happy --- src/zhash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zhash.c b/src/zhash.c index c40e97a30..0f2311411 100644 --- a/src/zhash.c +++ b/src/zhash.c @@ -701,7 +701,7 @@ zhash_pack (zhash_t *self) needle += length; // Store value as longstr - length = strlen (item->value); + length = strlen ((char *) item->value); uint32_t serialize = htonl ((uint32_t) length); memcpy (needle, &serialize, 4); needle += 4;