Skip to content

Commit

Permalink
fix: allocate attachment z_bytes to avoid going out of scope
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Apr 24, 2024
1 parent 801bf92 commit b22ced6
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/protocol/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ int8_t _z_encoded_attachment_iteration_driver(const void *this_, z_attachment_it

z_attachment_t _z_encoded_as_attachment(const _z_owned_encoded_attachment_t *att) {
if (att->is_encoded) {
return (z_attachment_t){.data = &att->body.encoded, .iteration_driver = _z_encoded_attachment_iteration_driver};
// Recopy z_bytes data in allocated memory to avoid it going out of scope
z_bytes_t *att_data = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t));
if (att_data == NULL) {
return att->body.decoded;
}
*att_data = att->body.encoded;
return (z_attachment_t){.data = att_data, .iteration_driver = _z_encoded_attachment_iteration_driver};
} else {
return att->body.decoded;
}
Expand All @@ -147,6 +153,7 @@ void _z_attachment_copy(z_attachment_t *dst, const z_attachment_t *src) {
void z_attachment_drop(z_attachment_t *att) {
if (att->iteration_driver == _z_encoded_attachment_iteration_driver) {
_z_bytes_clear((z_bytes_t *)att->data);
z_free((z_bytes_t *)att->data);
}
}

Expand Down

0 comments on commit b22ced6

Please sign in to comment.