Skip to content

Commit

Permalink
fixed uninitialized owned_bytes pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Jun 26, 2024
1 parent c673cd4 commit acc3f59
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 82 deletions.
8 changes: 8 additions & 0 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
17 changes: 0 additions & 17 deletions include/zenoh-pico/protocol/codec/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions include/zenoh-pico/utils/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
86 changes: 22 additions & 64 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -483,37 +446,32 @@ 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;
if (res != _Z_RES_OK) z_bytes_drop(bytes);
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) {
Expand Down
2 changes: 1 addition & 1 deletion zenohpico.pc
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit acc3f59

Please sign in to comment.