Skip to content

CDRIVER-5641: Fixes for packed_bit edge cases on systems with 32-bit size_t #1904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/libbson/src/bson/bson-vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,11 @@ bson_vector_float32_binary_data_length (size_t element_count)
static BSON_INLINE uint32_t
bson_vector_packed_bit_binary_data_length (size_t element_count)
{
const size_t max_representable = (size_t) BSON_MIN (
(uint64_t) SIZE_MAX, ((uint64_t) UINT32_MAX - (uint64_t) BSON_VECTOR_HEADER_LEN) * (uint64_t) 8);
const size_t max_representable =
(size_t) BSON_MIN ((uint64_t) SIZE_MAX, ((uint64_t) UINT32_MAX - (uint64_t) BSON_VECTOR_HEADER_LEN) * 8u);
return element_count > max_representable
? 0u
: (uint32_t) ((element_count + (size_t) 7) / (size_t) 8) + (uint32_t) BSON_VECTOR_HEADER_LEN;
: (uint32_t) (((uint64_t) element_count + 7u) / 8u) + (uint32_t) BSON_VECTOR_HEADER_LEN;
}


Expand Down
28 changes: 26 additions & 2 deletions src/libbson/tests/test-bson-vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -1495,21 +1495,45 @@ test_bson_vector_edge_cases_float32 (void)
static void
test_bson_vector_edge_cases_packed_bit (void)
{
// Test UINT32_MAX as an element count. This is the largest representable on systems with a 32-bit size_t.
uint32_t len_for_max_count = (uint32_t) (((uint64_t) UINT32_MAX + 7u) / 8u + BSON_VECTOR_HEADER_LEN);
{
ASSERT_CMPUINT32 (bson_vector_packed_bit_binary_data_length ((size_t) UINT32_MAX), ==, len_for_max_count);
ASSERT_CMPUINT32 (bson_vector_packed_bit_binary_data_length ((size_t) UINT32_MAX - 1u), ==, len_for_max_count);
ASSERT_CMPUINT32 (bson_vector_packed_bit_binary_data_length ((size_t) UINT32_MAX - 6u), ==, len_for_max_count);
ASSERT_CMPUINT32 (
bson_vector_packed_bit_binary_data_length ((size_t) UINT32_MAX - 7u), ==, len_for_max_count - 1u);
ASSERT_CMPUINT32 (
bson_vector_packed_bit_binary_data_length ((size_t) UINT32_MAX - 8u), ==, len_for_max_count - 1u);
}

// Test the real max_representable_elements only if size_t is large enough.
#if SIZE_MAX > UINT32_MAX
size_t max_representable_elements = ((size_t) UINT32_MAX - BSON_VECTOR_HEADER_LEN) * 8u;

// Test binary_data_length (uint32_t) edge cases, without any allocation.
{
ASSERT_CMPUINT32 (bson_vector_packed_bit_binary_data_length ((size_t) UINT32_MAX + 1u), ==, len_for_max_count);
ASSERT_CMPUINT32 (
bson_vector_packed_bit_binary_data_length ((size_t) UINT32_MAX + 2u), ==, len_for_max_count + 1u);
ASSERT_CMPUINT32 (
bson_vector_packed_bit_binary_data_length ((size_t) UINT32_MAX + 9u), ==, len_for_max_count + 1u);
ASSERT_CMPUINT32 (
bson_vector_packed_bit_binary_data_length (max_representable_elements - 8u), ==, UINT32_MAX - 1u);
ASSERT_CMPUINT32 (bson_vector_packed_bit_binary_data_length (max_representable_elements - 7u), ==, UINT32_MAX);
ASSERT_CMPUINT32 (bson_vector_packed_bit_binary_data_length (max_representable_elements), ==, UINT32_MAX);
ASSERT_CMPUINT32 (bson_vector_packed_bit_binary_data_length (max_representable_elements + 1u), ==, 0);
}
#endif // SIZE_MAX > UINT32_MAX

// Needs little real memory because most bytes are never accessed,
// but we should require a virtual address space larger than 32 bits.
// If we additionally have a 64-bit address space, allocate this max-sized vector and run tests.
// Needs little real memory because most bytes are never accessed.
#if BSON_WORD_SIZE > 32

#if !(SIZE_MAX > UINT32_MAX)
#error 64-bit platforms should have a 64-bit size_t
#endif

size_t expected_bson_overhead =
5 /* empty bson document */ + 3 /* "v" element header */ + 5 /* binary item header */;
size_t max_alloc_bytes = (size_t) BSON_MAX_SIZE - expected_bson_overhead - BSON_VECTOR_HEADER_LEN;
Expand Down