Skip to content
This repository has been archived by the owner on Oct 13, 2020. It is now read-only.

Commit

Permalink
bson: [abi break]: use size_t for buffer sizes when creating bson_t.
Browse files Browse the repository at this point in the history
This breaks ABI on bson_init_static() and bson_new_from_data(), but
worth it to avoid some potential overflows.
  • Loading branch information
Christian Hergert committed May 30, 2014
1 parent 2cb5a86 commit 32002c5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion doc/bson_init_static.page
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<synopsis><code mime="text/x-csrc"><![CDATA[bool
bson_init_static (bson_t *b,
const uint8_t *data,
uint32_t length);
size_t length);
]]></code></synopsis>
</section>

Expand Down
2 changes: 1 addition & 1 deletion doc/bson_new_from_data.page
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<title>Synopsis</title>
<synopsis><code mime="text/x-csrc"><![CDATA[bson_t *
bson_new_from_data (const uint8_t *data,
uint32_t length);
size_t length);
]]></code></synopsis>
</section>

Expand Down
20 changes: 8 additions & 12 deletions src/bson/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1794,9 +1794,9 @@ bson_reinit (bson_t *bson)


bool
bson_init_static (bson_t *bson,
bson_init_static (bson_t *bson,
const uint8_t *data,
uint32_t length)
size_t length)
{
bson_impl_alloc_t *impl = (bson_impl_alloc_t *)bson;
uint32_t len_le;
Expand All @@ -1810,7 +1810,7 @@ bson_init_static (bson_t *bson,

memcpy (&len_le, data, 4);

if (BSON_UINT32_FROM_LE (len_le) != length) {
if ((size_t)BSON_UINT32_FROM_LE (len_le) != length) {
return false;
}

Expand All @@ -1819,7 +1819,7 @@ bson_init_static (bson_t *bson,
}

impl->flags = BSON_FLAG_STATIC | BSON_FLAG_RDONLY;
impl->len = length;
impl->len = (uint32_t)length;
impl->parent = NULL;
impl->depth = 0;
impl->buf = &impl->alloc;
Expand Down Expand Up @@ -1896,30 +1896,26 @@ bson_sized_new (size_t size)

bson_t *
bson_new_from_data (const uint8_t *data,
uint32_t length)
size_t length)
{
uint32_t len_le;
bson_t *bson;

bson_return_val_if_fail (data, NULL);

if (length < 5) {
return NULL;
}

if (data[length - 1]) {
if ((length < 5) || (length > INT_MAX) || data [length - 1]) {
return NULL;
}

memcpy (&len_le, data, 4);

if (length != BSON_UINT32_FROM_LE (len_le)) {
if (length != (size_t)BSON_UINT32_FROM_LE (len_le)) {
return NULL;
}

bson = bson_sized_new (length);
memcpy (_bson_data (bson), data, length);
bson->len = length;
bson->len = (uint32_t)length;

return bson;
}
Expand Down
6 changes: 3 additions & 3 deletions src/bson/bson.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ bson_init_from_json (bson_t *bson,
* Returns: true if initialized successfully; otherwise false.
*/
bool
bson_init_static (bson_t *b,
bson_init_static (bson_t *b,
const uint8_t *data,
uint32_t length);
size_t length);


/**
Expand Down Expand Up @@ -261,7 +261,7 @@ bson_reinit (bson_t *b);
*/
bson_t *
bson_new_from_data (const uint8_t *data,
uint32_t length);
size_t length);


/**
Expand Down

0 comments on commit 32002c5

Please sign in to comment.