From baf0d008bb26281c66307459291b0f4ac7971ced Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 27 Jun 2024 13:40:01 +0200 Subject: [PATCH] ensure that decoded z_bytes never contanin non-_is_alloc slice --- include/zenoh-pico/collections/bytes.h | 2 +- src/collections/bytes.c | 2 +- src/protocol/codec.c | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 82a09aa64..624e1381d 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -73,7 +73,7 @@ int8_t _z_bytes_from_uint64(_z_bytes_t *b, uint64_t val); int8_t _z_bytes_from_float(_z_bytes_t *b, float val); int8_t _z_bytes_from_double(_z_bytes_t *b, double val); size_t _z_bytes_to_buf(const _z_bytes_t *bytes, uint8_t *dst, size_t len); -int8_t _z_bytes_from_buf(_z_bytes_t *b, uint8_t *src, size_t len); +int8_t _z_bytes_from_buf(_z_bytes_t *b, const uint8_t *src, size_t len); int8_t _z_bytes_serialize_from_pair(_z_bytes_t *out, _z_bytes_t *first, _z_bytes_t *second); int8_t _z_bytes_deserialize_into_pair(const _z_bytes_t *bs, _z_bytes_t *first_out, _z_bytes_t *second_out); _z_slice_t _z_bytes_try_get_contiguous(const _z_bytes_t *bs); diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 1d9695559..20177626b 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -106,7 +106,7 @@ int8_t _z_bytes_from_slice(_z_bytes_t *b, _z_slice_t s) { return _z_arc_slice_svec_append(&b->_slices, &arc_s) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY; } -int8_t _z_bytes_from_buf(_z_bytes_t *b, uint8_t *src, size_t len) { +int8_t _z_bytes_from_buf(_z_bytes_t *b, const uint8_t *src, size_t len) { *b = _z_bytes_null(); _z_slice_t s = _z_slice_wrap_copy(src, len); if (s.len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 839ef0e42..cac24c15f 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -281,7 +281,11 @@ int8_t _z_slice_decode(_z_slice_t *bs, _z_zbuf_t *zbf) { return _z_slice_decode_ int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) { _z_slice_t s; _Z_RETURN_IF_ERR(_z_slice_decode(&s, zbf)); - return _z_bytes_from_slice(bs, s); + if (s._is_alloc) { + return _z_bytes_from_slice(bs, s); + } else { + return _z_bytes_from_buf(bs, s.start, s.len); + } } int8_t _z_bytes_encode_val(_z_wbuf_t *wbf, const _z_bytes_t *bs) {