From 8a6c99e6f6887395090c0ffbc992354f15b387c0 Mon Sep 17 00:00:00 2001 From: DenisBiryukov91 <155981813+DenisBiryukov91@users.noreply.github.com> Date: Mon, 15 Jul 2024 11:55:46 +0200 Subject: [PATCH] fix _z_query_copy (#496) --- include/zenoh-pico/net/query.h | 2 +- src/net/query.c | 35 +++++++++++++++++++++++++--------- zenohpico.pc | 2 +- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index 3ed21f24d..d55af1c26 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -36,7 +36,7 @@ typedef struct _z_query_t { _z_query_t _z_query_null(void); void _z_query_clear(_z_query_t *q); -void _z_query_copy(_z_query_t *dst, const _z_query_t *src); +int8_t _z_query_copy(_z_query_t *dst, const _z_query_t *src); void _z_query_free(_z_query_t **query); _Z_REFCOUNT_DEFINE(_z_query, _z_query) diff --git a/src/net/query.c b/src/net/query.c index 08208e741..e957b6650 100644 --- a/src/net/query.c +++ b/src/net/query.c @@ -23,10 +23,19 @@ _z_query_t _z_query_null(void) { ._parameters = NULL, ._request_id = 0, ._value = _z_value_null(), + .attachment = _z_bytes_null(), ._zn = {.in = NULL}, }; } +void _z_query_clear_inner(_z_query_t *q) { + _z_keyexpr_clear(&q->_key); + _z_value_clear(&q->_value); + _z_bytes_drop(&q->attachment); + z_free(q->_parameters); + _z_session_weak_drop(&q->_zn); +} + void _z_query_clear(_z_query_t *q) { // Try to upgrade session weak to rc _z_session_rc_t sess_rc = _z_session_weak_upgrade(&q->_zn); @@ -40,20 +49,28 @@ void _z_query_clear(_z_query_t *q) { _z_session_rc_drop(&sess_rc); } // Clean up memory - z_free(q->_parameters); - _z_keyexpr_clear(&q->_key); - _z_value_clear(&q->_value); - _z_bytes_drop(&q->attachment); - _z_session_weak_drop(&q->_zn); + _z_query_clear_inner(q); } -void _z_query_copy(_z_query_t *dst, const _z_query_t *src) { +int8_t _z_query_copy(_z_query_t *dst, const _z_query_t *src) { + *dst = _z_query_null(); + _Z_RETURN_IF_ERR(_z_keyexpr_copy(&dst->_key, &src->_key)); + _Z_CLEAN_RETURN_IF_ERR(_z_value_copy(&dst->_value, &src->_value), _z_query_clear_inner(dst)); + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_copy(&dst->attachment, &src->attachment), _z_query_clear_inner(dst)); + dst->_parameters = _z_str_clone(src->_parameters); + if (dst->_parameters == NULL && src->_parameters != NULL) { + _z_query_clear_inner(dst); + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + _z_session_weak_copy(&dst->_zn, &src->_zn); + if (dst->_zn.in == NULL) { + _z_query_clear_inner(dst); + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } dst->_anyke = src->_anyke; - dst->_key = _z_keyexpr_duplicate(src->_key); - dst->_parameters = src->_parameters; dst->_request_id = src->_request_id; dst->_zn = src->_zn; - _z_value_copy(&dst->_value, &src->_value); + return _Z_RES_OK; } void _z_query_free(_z_query_t **query) { diff --git a/zenohpico.pc b/zenohpico.pc index 6171fd0b5..99c36210a 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 1.0.20240711dev +Version: 1.0.20240715dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico