Skip to content

Commit

Permalink
fix: 0 size allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Feb 3, 2025
1 parent e5ec8b2 commit 3c1f05e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/collections/slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void _z_delete_context_delete(_z_delete_context_t *c, void *data) {

/*-------- Slice --------*/
z_result_t _z_slice_init(_z_slice_t *bs, size_t capacity) {
assert(capacity != 0);
bs->start = (uint8_t *)z_malloc(capacity);
if (bs->start == NULL) {
bs->len = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/protocol/definitions/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ void _z_t_msg_copy_init(_z_t_msg_init_t *clone, _z_t_msg_init_t *msg) {
void _z_t_msg_copy_open(_z_t_msg_open_t *clone, _z_t_msg_open_t *msg) {
clone->_lease = msg->_lease;
clone->_initial_sn = msg->_initial_sn;
_z_slice_copy(&clone->_cookie, &msg->_cookie);
if (!_z_slice_is_empty(&msg->_cookie)) {
_z_slice_copy(&clone->_cookie, &msg->_cookie);
}
}

void _z_t_msg_copy_close(_z_t_msg_close_t *clone, _z_t_msg_close_t *msg) { clone->_reason = msg->_reason; }
Expand Down
7 changes: 6 additions & 1 deletion src/system/freertos_plus_tcp/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ void z_random_fill(void *buf, size_t len) {
}

/*------------------ Memory ------------------*/
void *z_malloc(size_t size) { return pvPortMalloc(size); }
void *z_malloc(size_t size) {
if (size == 0) {
return NULL;
}
return pvPortMalloc(size);
}

void *z_realloc(void *ptr, size_t size) {
_ZP_UNUSED(ptr);
Expand Down
7 changes: 6 additions & 1 deletion src/system/rpi_pico/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ void z_random_fill(void *buf, size_t len) {
}

/*------------------ Memory ------------------*/
void *z_malloc(size_t size) { return pvPortMalloc(size); }
void *z_malloc(size_t size) {
if (size == 0) {
return NULL;
}
return pvPortMalloc(size);
}

void *z_realloc(void *ptr, size_t size) {
_ZP_UNUSED(ptr);
Expand Down
6 changes: 3 additions & 3 deletions src/transport/unicast/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static z_result_t _z_unicast_handshake_client(_z_transport_unicast_establish_par
return ret;
}
// Try to receive response
_z_transport_message_t iam;
_z_transport_message_t iam = {0};
_Z_RETURN_IF_ERR(_z_link_recv_t_msg(&iam, zl));
if ((_Z_MID(iam._header) != _Z_MID_T_INIT) || !_Z_HAS_FLAG(iam._header, _Z_FLAG_T_INIT_A)) {
_z_t_msg_clear(&iam);
Expand Down Expand Up @@ -213,7 +213,7 @@ static z_result_t _z_unicast_handshake_client(_z_transport_unicast_establish_par
return ret;
}
// Try to receive response
_z_transport_message_t oam;
_z_transport_message_t oam = {0};
_Z_RETURN_IF_ERR(_z_link_recv_t_msg(&oam, zl));
if ((_Z_MID(oam._header) != _Z_MID_T_OPEN) || !_Z_HAS_FLAG(oam._header, _Z_FLAG_T_OPEN_A)) {
_z_t_msg_clear(&oam);
Expand All @@ -234,7 +234,7 @@ static z_result_t _z_unicast_handshake_client(_z_transport_unicast_establish_par
static z_result_t _z_unicast_handshake_listener(_z_transport_unicast_establish_param_t *param, const _z_link_t *zl,
const _z_id_t *local_zid, enum z_whatami_t whatami) {
// Read t message from link
_z_transport_message_t tmsg;
_z_transport_message_t tmsg = {0};
z_result_t ret = _z_link_recv_t_msg(&tmsg, zl);
if (ret != _Z_RES_OK) {
return ret;
Expand Down

0 comments on commit 3c1f05e

Please sign in to comment.