From acc3f597c2e28f64d6f0f8591e94ecbe5f0bf7d7 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 26 Jun 2024 15:58:14 +0000 Subject: [PATCH] fixed uninitialized owned_bytes pointer --- include/zenoh-pico/api/primitives.h | 8 +++ include/zenoh-pico/protocol/codec/core.h | 17 ----- include/zenoh-pico/utils/result.h | 28 ++++++++ src/api/api.c | 86 ++++++------------------ zenohpico.pc | 2 +- 5 files changed, 59 insertions(+), 82 deletions(-) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 16fb380b3..a78862a65 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -816,6 +816,14 @@ int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body */ int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second); +/** + * Parameters: + * bytes: Pointer to an unitialized :c:type:`z_lowned_bytes_t` instance. + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_empty(z_owned_bytes_t *bytes); + /** * Returns an iterator for multi-element serialized data. * diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index 59ccadc0d..706e7d9ab 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -24,23 +24,6 @@ #include "zenoh-pico/utils/config.h" #include "zenoh-pico/utils/result.h" -#define _Z_RETURN_IF_ERR(expr) \ - { \ - int8_t __res = expr; \ - if (__res != _Z_RES_OK) { \ - return __res; \ - } \ - } - -#define _Z_CLEAN_RETURN_IF_ERR(base_expr, clean_expr) \ - { \ - int8_t __res = base_expr; \ - if (__res != _Z_RES_OK) { \ - clean_expr; \ - return __res; \ - } \ - } - typedef int8_t (*__z_single_byte_reader_t)(uint8_t *, void *context); /*------------------ Internal Zenoh-net Macros ------------------*/ int8_t _z_consolidation_mode_encode(_z_wbuf_t *wbf, z_consolidation_mode_t en); diff --git a/include/zenoh-pico/utils/result.h b/include/zenoh-pico/utils/result.h index 76d9dc583..82e91d4cc 100644 --- a/include/zenoh-pico/utils/result.h +++ b/include/zenoh-pico/utils/result.h @@ -73,4 +73,32 @@ typedef enum { _Z_ERR_GENERIC = -128 } _z_res_t; +#define _Z_RETURN_IF_ERR(expr) \ + { \ + int8_t __res = expr; \ + if (__res != _Z_RES_OK) { \ + return __res; \ + } \ + } + +#define _Z_CLEAN_RETURN_IF_ERR(base_expr, clean_expr) \ + { \ + int8_t __res = base_expr; \ + if (__res != _Z_RES_OK) { \ + clean_expr; \ + return __res; \ + } \ + } + +#define _Z_IS_OK(expr) (expr == _Z_RES_OK) +#define _Z_IS_ERR(expr) (expr == _Z_RES_OK) +#define _Z_DO_AND_RETURN_IF_ERR(expr, action) \ + { \ + int8_t __res = expr; \ + if (__res != _Z_RES_OK) { \ + { action; } \ + return __res; \ + } \ + } + #endif /* ZENOH_PICO_UTILS_RESULT_H */ diff --git a/src/api/api.c b/src/api/api.c index f4821b457..ccc9d3b4d 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -331,13 +331,8 @@ int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_st int8_t z_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { // Init pair of owned bytes - z_bytes_null(first); - z_bytes_null(second); - first->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - second->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if ((first->_val == NULL) || (second->_val == NULL)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(first)); + _Z_DO_AND_RETURN_IF_ERR(z_bytes_empty(second), z_bytes_drop(second)); return _z_bytes_deserialize_into_pair(bytes, first->_val, second->_val); } @@ -359,11 +354,7 @@ int8_t z_bytes_serialize_from_int64(z_owned_bytes_t *bytes, int64_t val) { int8_t z_bytes_serialize_from_uint8(z_owned_bytes_t *bytes, uint8_t val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_uint8(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -372,11 +363,7 @@ int8_t z_bytes_serialize_from_uint8(z_owned_bytes_t *bytes, uint8_t val) { int8_t z_bytes_serialize_from_uint16(z_owned_bytes_t *bytes, uint16_t val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_uint16(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -385,11 +372,7 @@ int8_t z_bytes_serialize_from_uint16(z_owned_bytes_t *bytes, uint16_t val) { int8_t z_bytes_serialize_from_uint32(z_owned_bytes_t *bytes, uint32_t val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_uint32(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -398,11 +381,7 @@ int8_t z_bytes_serialize_from_uint32(z_owned_bytes_t *bytes, uint32_t val) { int8_t z_bytes_serialize_from_uint64(z_owned_bytes_t *bytes, uint64_t val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_uint64(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -411,11 +390,7 @@ int8_t z_bytes_serialize_from_uint64(z_owned_bytes_t *bytes, uint64_t val) { int8_t z_bytes_serialize_from_float(z_owned_bytes_t *bytes, float val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_float(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -424,11 +399,7 @@ int8_t z_bytes_serialize_from_float(z_owned_bytes_t *bytes, float val) { int8_t z_bytes_serialize_from_double(z_owned_bytes_t *bytes, double val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_double(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -437,11 +408,7 @@ int8_t z_bytes_serialize_from_double(z_owned_bytes_t *bytes, double val) { int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data _z_slice_t s = _z_slice_wrap((uint8_t *)data, len); int8_t res = _z_bytes_from_slice(bytes->_val, s); @@ -451,11 +418,7 @@ int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, int8_t z_bytes_serialize_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Allocate bytes _z_slice_t s = _z_slice_make(len); if (!_z_slice_check(s) && len > 0) { @@ -483,30 +446,16 @@ int8_t z_bytes_serialize_from_string_copy(z_owned_bytes_t *bytes, const char *s) int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body)(z_owned_bytes_t *data, void *context), void *context) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); z_owned_bytes_t data; while (iterator_body(&data, context)) { - int8_t res = _z_bytes_append(bytes->_val, data._val); - if (res != _Z_RES_OK) { - z_bytes_drop(bytes); - return res; - } + _Z_DO_AND_RETURN_IF_ERR(_z_bytes_append(bytes->_val, data._val), z_bytes_drop(bytes)); } return _Z_RES_OK; } int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - z_bytes_drop(first); - z_bytes_drop(second); - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_DO_AND_RETURN_IF_ERR(z_bytes_empty(bytes), z_bytes_drop(first); z_bytes_drop(second)); int8_t res = _z_bytes_serialize_from_pair(bytes->_val, first->_val, second->_val); first->_val = NULL; second->_val = NULL; @@ -514,6 +463,15 @@ int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *firs return res; } +int8_t z_bytes_empty(z_owned_bytes_t *bytes) { + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + *bytes->_val = _z_bytes_null(); + return _Z_RES_OK; +} + z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes) { return _z_bytes_get_iterator(bytes); } _Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *bytes) { diff --git a/zenohpico.pc b/zenohpico.pc index 129afce02..8116ade1e 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.11.20240620dev +Version: 0.11.20240626dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico