Skip to content

Commit

Permalink
Array handling fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PJK committed Apr 26, 2015
1 parent c4a8a1e commit 0d7a973
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ cbor_item_t *cbor_new_definite_array(size_t size)
*item = (cbor_item_t) {
.refcount = 1,
.type = CBOR_TYPE_ARRAY,
.metadata = {.array_metadata = {.type = _CBOR_METADATA_DEFINITE, .size = size}},
.metadata = {.array_metadata = {.type = _CBOR_METADATA_DEFINITE, .size = size, .ptr = 0}},
.data = _CBOR_MALLOC(sizeof(cbor_item_t *) * size)
};
return item;
Expand All @@ -1230,7 +1230,7 @@ cbor_item_t *cbor_new_indefinite_array()
*item = (cbor_item_t) {
.refcount = 1,
.type = CBOR_TYPE_ARRAY,
.metadata = {.array_metadata = {.type = _CBOR_METADATA_INDEFINITE, .size = 0}},
.metadata = {.array_metadata = {.type = _CBOR_METADATA_INDEFINITE, .size = 0, .ptr = 0}},
.data = NULL /* Can be safely realloc-ed */
};
return item;
Expand All @@ -1244,7 +1244,7 @@ cbor_item_t *cbor_array_push(cbor_item_t *array, cbor_item_t *pushee)
cbor_item_t **data = (cbor_item_t **) array->data;
if (cbor_array_is_definite(array)) {
// TODO check size - throw
data[metadata->size++] = pushee;
data[metadata->ptr++] = pushee;
} else {
// TODO exponential reallocs?
data = _CBOR_REALLOC(data, (metadata->size + 1) * sizeof(cbor_item_t *));
Expand Down
1 change: 1 addition & 0 deletions src/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct _cbor_string_metadata {

struct _cbor_array_metadata {
size_t size;
size_t ptr;
_cbor_dst_metadata type;
};

Expand Down
2 changes: 1 addition & 1 deletion test/type_4_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void test_simple_array(void **state)
assert_non_null(arr);
assert_true(cbor_typeof(arr) == CBOR_TYPE_ARRAY);
assert_true(cbor_isa_array(arr));
assert_true(cbor_array_size(arr) == 1);
assert_int_equal(cbor_array_size(arr), 1);
assert_true(res.read == 2);
/* Check the values */
assert_uint8(cbor_array_handle(arr)[0], 1);
Expand Down

0 comments on commit 0d7a973

Please sign in to comment.