Skip to content

Commit 04d2611

Browse files
authored
collections: make slices handle zero-length data (#1095)
- Treat zero-length inputs as _z_slice_null() so callers can always call _z_slice_copy() without special cases for zero-length input. Same for _z_slice_n_copy when len == 0. - Make _z_slice_init() and _z_slice_eq() treat zero-length slices as valid values.
1 parent f77907b commit 04d2611

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/collections/slice.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ _z_delete_context_t _z_delete_context_static(void) { return _z_delete_context_cr
3737

3838
/*-------- Slice --------*/
3939
z_result_t _z_slice_init(_z_slice_t *bs, size_t capacity) {
40-
assert(capacity != 0);
40+
assert(bs != NULL);
41+
if (capacity == 0) {
42+
*bs = _z_slice_null();
43+
return _Z_RES_OK;
44+
}
4145
bs->start = (uint8_t *)z_malloc(capacity);
4246
if (bs->start == NULL) {
4347
bs->len = 0;
@@ -75,6 +79,12 @@ void _z_slice_free(_z_slice_t **bs) {
7579
}
7680

7781
z_result_t _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src) {
82+
assert(src != NULL);
83+
assert(src->len == 0 || src->start != NULL);
84+
if (src->len == 0) {
85+
*dst = _z_slice_null();
86+
return _Z_RES_OK;
87+
}
7888
// Make sure dst slice is not init beforehand, or suffer memory leak
7989
z_result_t ret = _z_slice_init(dst, src->len);
8090
if (ret == _Z_RES_OK) {
@@ -85,6 +95,10 @@ z_result_t _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src) {
8595

8696
z_result_t _z_slice_n_copy(_z_slice_t *dst, const _z_slice_t *src, size_t offset, size_t len) {
8797
assert(offset + len <= src->len);
98+
if (len == 0) {
99+
*dst = _z_slice_null();
100+
return _Z_RES_OK;
101+
}
88102
// Make sure dst slice is not init beforehand, or suffer memory leak
89103
z_result_t ret = _z_slice_init(dst, len);
90104
if (ret == _Z_RES_OK) {
@@ -119,7 +133,17 @@ _z_slice_t _z_slice_steal(_z_slice_t *b) {
119133
return ret;
120134
}
121135
bool _z_slice_eq(const _z_slice_t *left, const _z_slice_t *right) {
122-
return left->len == right->len && memcmp(left->start, right->start, left->len) == 0;
136+
assert(left != NULL);
137+
assert(right != NULL);
138+
if (left->len != right->len) {
139+
return false;
140+
}
141+
if (left->len == 0) {
142+
return true;
143+
}
144+
assert(left->start != NULL);
145+
assert(right->start != NULL);
146+
return memcmp(left->start, right->start, left->len) == 0;
123147
}
124148

125149
bool _z_slice_is_alloced(const _z_slice_t *s) { return !_z_delete_context_is_null(&s->_delete_context); }

0 commit comments

Comments
 (0)