From b175b3537b91ff2b44cb0acce0ebcaf930c465c4 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Tue, 25 Jun 2024 10:46:56 +0200 Subject: [PATCH] fixes --- include/zenoh-pico/collections/bytes.h | 2 +- src/collections/arc_slice.c | 6 ++++-- src/collections/bytes.c | 18 +++++++++--------- src/net/query.c | 2 +- src/net/reply.c | 8 ++++---- src/protocol/codec.c | 4 ++-- src/protocol/codec/message.c | 5 +++-- tests/z_bytes_test.c | 4 ++-- 8 files changed, 26 insertions(+), 23 deletions(-) diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 878396d82..82a09aa64 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -48,7 +48,7 @@ typedef struct { _Bool _z_bytes_check(const _z_bytes_t *bytes); _z_bytes_t _z_bytes_null(void); int8_t _z_bytes_append(_z_bytes_t *dst, _z_bytes_t *src); -int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s); +_Bool _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s); int8_t _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src); _z_bytes_t _z_bytes_duplicate(const _z_bytes_t *src); void _z_bytes_move(_z_bytes_t *dst, _z_bytes_t *src); diff --git a/src/collections/arc_slice.c b/src/collections/arc_slice.c index b22804f8b..663cb0ae6 100644 --- a/src/collections/arc_slice.c +++ b/src/collections/arc_slice.c @@ -42,12 +42,14 @@ _z_arc_slice_t _z_arc_slice_get_subslice(const _z_arc_slice_t* s, size_t offset, assert(s->slice.in != NULL || (len == 0 && offset == 0)); _z_arc_slice_t out; - out.len = len; - out.start = offset; if (s->slice.in == NULL) { out.slice.in = NULL; + out.start = 0; + out.len = 0; } else { out.slice = _z_slice_rc_clone(&s->slice); + out.len = len; + out.start = out.start + offset; } return out; } diff --git a/src/collections/bytes.c b/src/collections/bytes.c index dfd8d0664..03c112e47 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -86,18 +86,18 @@ void _z_bytes_free(_z_bytes_t **bs) { size_t _z_bytes_to_buf(const _z_bytes_t *bytes, uint8_t *dst, size_t len) { uint8_t *start = dst; - - for (size_t i = 0; i < _z_bytes_num_slices(bytes) && len > 0; ++i) { + size_t remaining = len; + for (size_t i = 0; i < _z_bytes_num_slices(bytes) && remaining > 0; ++i) { // Recopy data _z_arc_slice_t *s = _z_bytes_get_slice(bytes, i); size_t s_len = _z_arc_slice_len(s); - size_t len_to_copy = len >= s_len ? s_len : len; + size_t len_to_copy = remaining >= s_len ? s_len : remaining; memcpy(start, _z_arc_slice_data(s), len_to_copy); start += s_len; - len -= len_to_copy; + remaining -= len_to_copy; } - return len; + return len - remaining; } int8_t _z_bytes_from_slice(_z_bytes_t *b, _z_slice_t s) { *b = _z_bytes_null(); @@ -132,13 +132,13 @@ int8_t _z_bytes_to_slice(const _z_bytes_t *bytes, _z_slice_t *s) { return _Z_RES_OK; } -int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { return _z_arc_slice_svec_append(&dst->_slices, s); } +_Bool _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { return _z_arc_slice_svec_append(&dst->_slices, s); } int8_t _z_bytes_append_inner(_z_bytes_t *dst, _z_bytes_t *src) { _Bool success = true; for (size_t i = 0; i < _z_bytes_num_slices(src); ++i) { _z_arc_slice_t *s = _z_bytes_get_slice(src, i); - success = success && _z_arc_slice_svec_append(&dst->_slices, s); + success = success && _z_bytes_append_slice(dst, s); } if (!success) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; @@ -156,7 +156,7 @@ int8_t _z_bytes_append(_z_bytes_t *dst, _z_bytes_t *src) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } _z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, l_len); - _z_arc_slice_svec_append(&dst->_slices, &arc_s); + _z_bytes_append_slice(dst, &arc_s); if (dst->_slices._val == NULL) { _z_arc_slice_drop(&arc_s); @@ -419,7 +419,7 @@ int8_t _z_bytes_reader_read_slices(_z_bytes_reader_t *reader, size_t len, _z_byt res = _Z_ERR_SYSTEM_OUT_OF_MEMORY; break; } - res = _z_arc_slice_svec_append(&out->_slices, &ss); + res = _z_bytes_append_slice(out, &ss) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY; if (res != _Z_RES_OK) { _z_arc_slice_drop(&ss); break; diff --git a/src/net/query.c b/src/net/query.c index 2b87c0e03..fb79133a7 100644 --- a/src/net/query.c +++ b/src/net/query.c @@ -51,7 +51,7 @@ _z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, con memcpy(q._parameters, parameters->start, parameters->len); q._parameters[parameters->len] = 0; q._anyke = (strstr(q._parameters, Z_SELECTOR_QUERY_MATCH) == NULL) ? false : true; - q.attachment = attachment; + _z_bytes_copy(&q.attachment, &attachment); _z_keyexpr_copy(&q._key, key); _z_value_copy(&q._value, value); diff --git a/src/net/reply.c b/src/net/reply.c index 48bff2c6c..3df4b9fe2 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -93,12 +93,12 @@ _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, reply.data.replier_id = id; // Create sample _z_sample_t sample = _z_sample_null(); - sample.keyexpr = keyexpr; // FIXME: call z_keyexpr_move or copy - sample.encoding = encoding; // FIXME: call z_encoding_move or copy - sample.payload = payload; // FIXME: call z_bytes_move or copy + sample.keyexpr = keyexpr; // FIXME: call z_keyexpr_move or copy + sample.encoding = encoding; // FIXME: call z_encoding_move or copy + _z_bytes_copy(&sample.payload, &payload); // FIXME: call z_bytes_move or copy sample.kind = kind; sample.timestamp = _z_timestamp_duplicate(timestamp); - sample.attachment = attachment; // FIXME: call z_bytes_move or copy + _z_bytes_copy(&sample.attachment, &attachment); // FIXME: call z_bytes_move or copy // Create sample rc from value reply.data.sample = _z_sample_rc_new_from_val(sample); diff --git a/src/protocol/codec.c b/src/protocol/codec.c index ee159c192..53b15a1a0 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -144,7 +144,7 @@ uint8_t _z_zint64_encode_buf(uint8_t *buf, uint64_t v) { buf[start++] = c; } - return start; + return (uint8_t)start; } int8_t _z_zint64_encode(_z_wbuf_t *wbf, uint64_t v) { @@ -181,7 +181,7 @@ int8_t _z_zint64_decode_with_reader(uint64_t *zint, __z_single_byte_reader_t rea int8_t _z_zsize_decode_with_reader(_z_zint_t *zint, __z_single_byte_reader_t reader, void *context) { uint64_t i = 0; int8_t res = _z_zint64_decode_with_reader(&i, reader, context); - if (res == _Z_RES_OK && i > SIZE_MAX) { + if (res != _Z_RES_OK || i > SIZE_MAX) { res = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } else { *zint = (_z_zint_t)i; diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 90688369d..7c8952295 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -446,8 +446,9 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { } case _Z_MSG_EXT_ENC_ZBUF | 0x03: { // Payload _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); - ret = _z_encoding_decode(&msg->_ext_value.encoding, &zbf); - ret |= _z_bytes_from_buf(&msg->_ext_value.payload, (uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf)); + _Z_RETURN_IF_ERR(_z_encoding_decode(&msg->_ext_value.encoding, &zbf)); + _Z_RETURN_IF_ERR( + _z_bytes_from_buf(&msg->_ext_value.payload, (uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf))); break; } case _Z_MSG_EXT_ENC_ZBUF | 0x05: { // Attachment diff --git a/tests/z_bytes_test.c b/tests/z_bytes_test.c index d03755c7a..07a37bf84 100644 --- a/tests/z_bytes_test.c +++ b/tests/z_bytes_test.c @@ -43,7 +43,7 @@ void test_slice(void) { assert(_z_bytes_num_slices(&b) == 1); assert(_z_slice_eq(&_z_bytes_get_slice(&b, 0)->slice.in->val, &s)); - _z_bytes_to_buf(&b, data_out, 5); + assert(_z_bytes_to_buf(&b, data_out, 5) == 5); assert(memcmp(data, data_out, 5) == 0); _z_bytes_drop(&b); @@ -73,7 +73,7 @@ void test_append(void) { assert(_z_slice_eq(&_z_bytes_get_slice(&b, 1)->slice.in->val, &s2.slice.in->val)); assert(_z_slice_eq(&_z_bytes_get_slice(&b, 2)->slice.in->val, &s3.slice.in->val)); - _z_bytes_to_buf(&b, data_out, 10); + assert(_z_bytes_to_buf(&b, data_out, 15) == 10); assert(memcmp(data_in, data_out, 10) == 0); _z_bytes_drop(&b);