diff options
author | Philip Withnall <withnall@endlessm.com> | 2020-02-27 11:20:26 +0000 |
---|---|---|
committer | Philip Withnall <withnall@endlessm.com> | 2020-02-27 14:48:36 +0000 |
commit | d22c762221933bc4fb4a27ec3bbbe48617ced917 (patch) | |
tree | 895c6afcc5b1c3f8982d5d6dc6317e4f0e44c90d /glib/tests/array-test.c | |
parent | 70c5fd53e96e341ea65e3a21c2f2147d484c9666 (diff) | |
download | glib-d22c762221933bc4fb4a27ec3bbbe48617ced917.tar.gz |
garray: Fix copying an array with reserved elements
Spotted by Mohammed Sadiq. `g_array_copy()` was doing a `memcpy()` of
the data from the old array to the new one, based on the reserved
elements in the old array (`array->alloc`). However, the new array was
allocated based on the *assigned* elements in the old array
(`array->len`).
So if the old array had fewer assigned elements than allocated elements,
`memcpy()` would fall off the end of the newly allocated data block.
This was particularly obvious when the old array had no assigned
elements, as the new array’s data pointer would be `NULL`.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Fixes: #2049
Diffstat (limited to 'glib/tests/array-test.c')
-rw-r--r-- | glib/tests/array-test.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/glib/tests/array-test.c b/glib/tests/array-test.c index b90b81f73..bdd6a2cb9 100644 --- a/glib/tests/array-test.c +++ b/glib/tests/array-test.c @@ -811,6 +811,32 @@ test_array_binary_search (void) g_array_free (garray, TRUE); } +static void +test_array_copy_sized (void) +{ + GArray *array1 = NULL, *array2 = NULL, *array3 = NULL; + int val = 5; + + g_test_summary ("Test that copying a newly-allocated sized array works."); + + array1 = g_array_sized_new (FALSE, FALSE, sizeof (int), 1); + array2 = g_array_copy (array1); + + g_assert_cmpuint (array2->len, ==, array1->len); + + g_array_append_val (array1, val); + array3 = g_array_copy (array1); + + g_assert_cmpuint (array3->len, ==, array1->len); + g_assert_cmpuint (g_array_index (array3, int, 0), ==, g_array_index (array1, int, 0)); + g_assert_cmpuint (array3->len, ==, 1); + g_assert_cmpuint (g_array_index (array3, int, 0), ==, val); + + g_array_unref (array3); + g_array_unref (array2); + g_array_unref (array1); +} + /* Check g_ptr_array_steal() function */ static void pointer_array_steal (void) @@ -1955,6 +1981,7 @@ main (int argc, char *argv[]) g_test_add_func ("/array/steal", array_steal); g_test_add_func ("/array/clear-func", array_clear_func); g_test_add_func ("/array/binary-search", test_array_binary_search); + g_test_add_func ("/array/copy-sized", test_array_copy_sized); for (i = 0; i < G_N_ELEMENTS (array_configurations); i++) { |