Skip to content

Commit

Permalink
Merge pull request #1959 from hgourvest/master
Browse files Browse the repository at this point in the history
use memcpy on unaligned pointer to avoid crash on armv7 (BUS_ADRALN)
  • Loading branch information
sappo authored Nov 30, 2018
2 parents 05fa2f5 + b83e42f commit bca0888
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
18 changes: 11 additions & 7 deletions src/zhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,24 +685,28 @@ 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;
for (index = 0; index < self->limit; index++) {
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 ((char *) 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;
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/zhashx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...
Expand Down Expand Up @@ -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;
}
Expand Down
19 changes: 10 additions & 9 deletions src/zlistx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down

0 comments on commit bca0888

Please sign in to comment.