Skip to content
Permalink
Browse files

Bring back overflow guards for strings

  • Loading branch information...
PJK committed Feb 2, 2019
1 parent 963c5d2 commit b951b63045378552c70a97c59bd55af7cdcbe41b
Showing with 26 additions and 16 deletions.
  1. +2 −0 CHANGELOG.md
  2. +5 −1 src/cbor/bytestrings.c
  3. +5 −0 src/cbor/strings.c
  4. +14 −15 test/memory_allocation_test.c
@@ -10,6 +10,8 @@ Next
when memory allocation fails
- It is up to the client to handle such cases
- Globally enforced code style [#83]
- Fix issue possible memory corruption bug on repeated
cbor_(byte)string_add_chunk calls with intermittently failing realloc calls

0.5.0 (2017-02-06)
---------------------
@@ -94,7 +94,11 @@ bool cbor_bytestring_add_chunk(cbor_item_t *item, cbor_item_t *chunk) {
struct cbor_indefinite_string_data *data =
(struct cbor_indefinite_string_data *)item->data;
if (data->chunk_count == data->chunk_capacity) {
/* We need more space */
// TODO: Add a test for this
if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) {
return false;
}

size_t new_chunk_capacity =
data->chunk_capacity == 0 ? 1
: CBOR_BUFFER_GROWTH * (data->chunk_capacity);
@@ -86,6 +86,11 @@ bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk) {
struct cbor_indefinite_string_data *data =
(struct cbor_indefinite_string_data *)item->data;
if (data->chunk_count == data->chunk_capacity) {
// TODO: Add a test for this
if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) {
return false;
}

size_t new_chunk_capacity = data->chunk_capacity == 0
? 1
: CBOR_BUFFER_GROWTH * (data->chunk_capacity);
@@ -202,21 +202,20 @@ static void test_bytestring_add_chunk(void **state) {

static void test_string_add_chunk(void **state) {
WITH_MOCK_MALLOC(
{
cbor_item_t *string = cbor_new_indefinite_string();
cbor_item_t *chunk = cbor_build_string("Hello!");

assert_false(cbor_string_add_chunk(string, chunk));
assert_int_equal(cbor_string_length(string), 0);
assert_int_equal(
((struct cbor_indefinite_string_data *)string->data)
->chunk_capacity,
0);

cbor_decref(&chunk);
cbor_decref(&string);
},
5, MALLOC, MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
{
cbor_item_t *string = cbor_new_indefinite_string();
cbor_item_t *chunk = cbor_build_string("Hello!");

assert_false(cbor_string_add_chunk(string, chunk));
assert_int_equal(cbor_string_length(string), 0);
assert_int_equal(((struct cbor_indefinite_string_data *)string->data)
->chunk_capacity,
0);

cbor_decref(&chunk);
cbor_decref(&string);
},
5, MALLOC, MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
}

int main(void) {

0 comments on commit b951b63

Please sign in to comment.
You can’t perform that action at this time.