Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Jun 25, 2024
1 parent efdad63 commit b175b35
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion include/zenoh-pico/collections/bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/collections/arc_slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 9 additions & 9 deletions src/collections/bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/net/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/net/reply.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/protocol/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/protocol/codec/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/z_bytes_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b175b35

Please sign in to comment.