Skip to content

Commit 3e4ec4b

Browse files
author
Micah Scott
committed
test-bson-vector read/write edge cases use malloc'ed value buffers
The main reason for this change is to avoid an unhelpful implementation of -Warray-bounds in gcc 11 which notices that the memcpy in these tests can be out-of-range, but doesn't notice that the out-of-range memcpy will never be executed. The easiest fix was to dynamically allocate the value buffers, to prevent gcc from associating range info with these pointers. This replaces some arbitrary nonzero test values with zeroes, out of convenience.
1 parent fc7772d commit 3e4ec4b

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/libbson/tests/test-bson-vector.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,10 +1431,11 @@ test_bson_vector_edge_cases_int8 (void)
14311431

14321432
// Test some read and write boundaries.
14331433
{
1434-
int8_t values[] = {1, 2, 3, 4, 5};
1435-
size_t values_size = sizeof values / sizeof values[0];
1434+
size_t values_size = 100;
1435+
int8_t *values = bson_malloc0 (values_size * sizeof *values);
14361436
TEST_BSON_VECTOR_EDGE_CASES_RW_COMMON (
14371437
view, max_alloc_elements, values, values_size, bson_vector_int8_view_read, bson_vector_int8_view_write);
1438+
bson_free (values);
14381439
}
14391440

14401441
bson_destroy (&doc);
@@ -1477,10 +1478,11 @@ test_bson_vector_edge_cases_float32 (void)
14771478

14781479
// Test some read and write boundaries.
14791480
{
1480-
float values[] = {1.f, 2.f, 3.f, 4.f, 5.f};
1481-
size_t values_size = sizeof values / sizeof values[0];
1481+
size_t values_size = 100;
1482+
float *values = bson_malloc0 (values_size * sizeof *values);
14821483
TEST_BSON_VECTOR_EDGE_CASES_RW_COMMON (
14831484
view, max_alloc_elements, values, values_size, bson_vector_float32_view_read, bson_vector_float32_view_write);
1485+
bson_free (values);
14841486
}
14851487

14861488
bson_destroy (&doc);
@@ -1524,28 +1526,28 @@ test_bson_vector_edge_cases_packed_bit (void)
15241526
// Test pack and unpack boundaries with the same tests used for read/write of non-packed element types.
15251527
// Only tests one length, but it's chosen to be greater than 8 and not a multiple of 8.
15261528
{
1527-
bool values[190];
1528-
size_t values_size = sizeof values / sizeof values[0];
1529-
for (size_t i = 0; i < values_size; i++) {
1530-
values[i] = (i & 3) == 3;
1531-
}
1529+
size_t values_size = 190;
1530+
bool *values = bson_malloc0 (values_size * sizeof *values);
15321531
TEST_BSON_VECTOR_EDGE_CASES_RW_COMMON (view,
15331532
max_alloc_elements,
15341533
values,
15351534
values_size,
15361535
bson_vector_packed_bit_view_unpack_bool,
15371536
bson_vector_packed_bit_view_pack_bool);
1537+
bson_free (values);
15381538
}
15391539

15401540
// Test read and write boundaries on packed bytes.
15411541
{
1542-
uint8_t packed[] = {0x12, 0x34, 0x56, 0x78, 0x9A};
1542+
size_t packed_size = 50;
1543+
uint8_t *packed = bson_malloc0 (packed_size);
15431544
TEST_BSON_VECTOR_EDGE_CASES_RW_COMMON (view,
15441545
max_alloc_bytes,
15451546
packed,
1546-
sizeof packed,
1547+
packed_size,
15471548
bson_vector_packed_bit_view_read_packed,
15481549
bson_vector_packed_bit_view_write_packed);
1550+
bson_free (packed);
15491551
}
15501552

15511553
bson_destroy (&doc);

0 commit comments

Comments
 (0)