Commit Graph

135 Commits

Author SHA1 Message Date
Thomas Haller
1efa966b6f array: add internal ptr_array_new() helper for creating GPtrArray
Unify the creation of GPtrArray.

Maybe we will add yet another constructor for creating %NULL terminated
arrays. Unify the constructors by adding an internal helper method.

The alternative instead of adding a ptr_array_new() helper, would be to
let everybody call g_ptr_array_full(). For no strong reasons, choose
this approach because the compiler is more eager to inline the static
helper as it would inlining g_ptr_array_full().
2020-05-15 17:47:11 +02:00
Philip Withnall
9d34514f7d Merge branch 'th/g-ptr-array-variable-cleanups' into 'master'
[th/g-ptr-array-variable-cleanups] minor cleanup of variables for GPtrArray

See merge request GNOME/glib!1482
2020-05-07 09:10:19 +00:00
Thomas Haller
cf1263bbb9 array: combine loop variables in g_ptr_array_remove_range() 2020-05-07 09:07:31 +02:00
Thomas Haller
507818aabd array: use guint type for loop variable iterating over GPtrArray.len elements
GPtrArray.len is guint. The type of the loop variable should match.

While at it, move some of the variables closer to the scope where they are
used.
2020-05-07 09:07:07 +02:00
Thomas Haller
8b542aac0c array: fix corrupt state of GPtrArray after g_ptr_array_extend_and_steal()
g_ptr_array_extend_and_steal() leaves the GPtrArray in an invalid state,
so if you would try to append another pointer, it leads to a crash.

Also adjust the test case so that it would result in the crash (without
the fix).

Fixes: 0675703af0 ('Adding g_ptr_array_extend_and_steal() function to glib/garray.c')
2020-05-07 08:58:41 +02:00
Philip Withnall
46d343e734 garray: Clarify documentation in a few places
Signed-off-by: Philip Withnall <withnall@endlessm.com>

Fixes: #1154
2020-03-03 14:32:37 +00:00
Philip Withnall
d22c762221 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
2020-02-27 14:48:36 +00:00
Philip Withnall
4c20fb990a gptrarray: Add an example to the g_ptr_array_steal() docs
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-12-12 11:59:59 +00:00
Sebastian Dröge
05be19b9f7 Merge branch 'wip/smcv/array-memcpy-ub' into 'master'
array: Avoid use of memcpy(dest, NULL, 0)

See merge request GNOME/glib!1180
2019-10-24 11:49:09 +00:00
Simon McVittie
3837b83f5a array: Avoid use of memcpy(dest, NULL, 0)
glibc declares memcpy() with the first two arguments (the pointers)
annotated as non-null via an attribute, which results in the undefined
behaviour sanitizer considering it to be UB to pass a null pointer
in the second argument, even if we are copying 0 bytes (and hence not
actually dereferencing the pointer).

This shows up in array-test when run with the undefined behaviour
sanitizer.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2019-10-24 12:08:20 +01:00
Simon McVittie
acbbe7b8c4 array: Add tests based on the g_ptr_array_sort[_with_data] doc-comments
Note that I deliberately haven't used g_autoptr here, because while we
encourage GLib users to use g_autoptr in their own code, GLib itself
still supports being compiled in environments like MSVC that can't
support g_autoptr.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2019-10-24 12:02:33 +01:00
Simon McVittie
ee13eb518d array: Fix handling of user_data in doc-comment
The user_data for g_ptr_array_sort_with_data is passed directly, not
with an extra layer of pointer like the data pointers.

Signed-off-by: Simon McVittie <smcv@collabora.com>
Fixes: 52c130f8
2019-10-24 11:57:29 +01:00
Simon McVittie
ef6fe191ac array: Remove unnecessary casts from doc-comments
Let's not encourage library users to sprinkle casts through their code
when they don't need to.

Signed-off-by: Simon McVittie <smcv@collabora.com>
Fixes: 52c130f8
2019-10-24 11:57:13 +01:00
Philip Withnall
038ec3de31 Merge branch 'add_array_steal_function' into 'master'
Add g_array_steal(), g_ptr_array_steal() and g_byte_array_steal()

Closes #285

See merge request GNOME/glib!1019
2019-10-07 13:07:56 +00:00
Paolo Bonzini
7bada8394d Add g_array_steal(), g_ptr_array_steal() and g_byte_array_steal()
Closes issue #285
2019-10-07 14:38:24 +02:00
Lee Bigelow
52c130f888 Add full examples to g_ptr_array_sort() and g_ptr_array_sort_with_data()
With changes by Emmanuel Fleury and Philip Withnall.

Closes issue #9
2019-10-07 09:50:51 +01:00
Alexander Larsson
4f5a2c19e4 garray: Fix reference to GLIB_SIZEOF_INT
This doesn't ever get set. SIZEOF_INT is set though, so use that instead.
2019-09-02 16:05:07 +02:00
Jeffrey Stedfast
2f8c61314c Optimize g_nearest_pow() function in glib/garray.c
Closes issue #83
2019-08-25 18:37:02 +02:00
Philip Withnall
c0f13f3cc8 garray: Fix binary search for non-existent elements on the left
If searching for an element which is smaller than every element in the
array (i.e. the element being searched for is not in the array), the
previous g_array_binary_search() implementation would underflow in the
calculation `right = middle - 1`, and end up trying to dereference an
element way off the right of the array.

Fix that by checking the additions/subtractions before doing them, and
bailing if the bounds are hit. We don’t need to check `middle <
G_MAXUINT`, as `middle` is bounded above by `right`, which is always `<=
_array->len - 1`, and `_array->len <= G_MAXUINT`.

Add some tests for that, and for not-present elements in the middle of
the array. Previously, the tests only checked for not-present elements
which were bigger than every element in the array.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-07-16 12:14:53 +01:00
Philip Withnall
ec61daf503 garray: Rewrite binary search calculation to avoid integer overflow
If `right` and `left` are both near `G_MAXUINT`, it’s possible for the
addition to overflow, even if the eventual result would fit in a
`guint`. Avoid that by operating on the difference instead.

The difference is guaranteed to be positive due to the prior `left <=
right` check.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-07-16 11:35:18 +01:00
Philip Withnall
b4943aa360 gptrarray: Correctly set copied array length in g_ptr_array_copy()
The allocation size was set correctly before, but not the array length,
so the copied array appeared to have zero elements.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-07-16 10:15:57 +01:00
Philip Withnall
1ac8d50331 gptrarray: Set free func on copied array in g_ptr_array_copy()
Otherwise its elements (which have just all been copied) will leak.

Spotted by Xavier Claessens in
https://gitlab.gnome.org/GNOME/glib/merge_requests/918#note_555867.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-07-16 10:15:09 +01:00
Emmanuel Fleury
104fca78cd Add g_array_binary_search() to garray API
Original code written by Christian Hergert

Fix issue #373
2019-07-09 12:12:18 +02:00
Emmanuel Fleury
46f70e9901 Adding a function g_array_copy() to glib/garray.c
Original code from Simon van der Linden

Close issue #236
2019-06-27 13:40:26 +02:00
Emmanuel Fleury
0675703af0 Adding g_ptr_array_extend_and_steal() function to glib/garray.c 2019-06-27 12:28:32 +02:00
Emmanuel Fleury
43ad244df2 Adding g_ptr_array_extend() function to glib/garray.c
Related to issue #269
2019-06-26 15:35:28 +02:00
Emmanuel Fleury
86bcc5c942 Adding g_ptr_array_copy() function to glib/garray.c
Related to issue #269
2019-06-26 15:34:47 +02:00
Emmanuel Fleury
a419146578 Fixing signedness in g_ptr_array_insert():glib/garray.c
glib/garray.c: In function ‘g_ptr_array_insert’:
glib/garray.c:1522:14: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’} [-Werror=sign-compare]
   if (index_ < rarray->len)
              ^
2019-01-28 15:29:21 +01:00
Emmanuel Fleury
5ca8c2fa8f Fixing signedness in g_ptr_array_maybe_expand():garray.c
../glib.git/glib/garray.c: In function ‘g_ptr_array_maybe_expand’:
../glib.git/glib/garray.c:1172:43: error: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘gint’ {aka ‘int’} [-Werror=sign-compare]
   if G_UNLIKELY ((G_MAXUINT - array->len) < len)
2019-01-28 15:24:06 +01:00
Debarshi Ray
0a8f3698a0 docs: Simplify the text for g_array_free
The text about deallocation of GArrays with elements containing
dynamically-allocated memory was confusing. It initially mentioned
clear_func, but later said elements with dynamically allocated memory
"should be freed separately".

Clarify this by using the same structure as g_ptr_array_free —
highlight the need to set a clear_func by consolidating the text about
it in a separate paragraph.

https://gitlab.gnome.org/GNOME/glib/merge_requests/348
2018-09-24 16:34:53 +02:00
Debarshi Ray
30ccd6f01b docs: Use the correct terminology for g_array_free
GArray uses the term clear_func (eg., g_array_set_clear_func), while
element_free_func comes from GPtrArray.

https://gitlab.gnome.org/GNOME/glib/merge_requests/348
2018-09-24 16:34:49 +02:00
Christian Hergert
0c7dc75844 garray: add overflow checks before expanding array
We should bail when we detect that adding a number of items to an array
would cause it to overflow. Since we can't change to using gsize for ABI
reasons we should protect the integrity of the process even if that means
crashing.
2018-07-23 21:05:11 -07:00
Philip Withnall
03bad78947 garray: Fix -Wsign-compare warnings
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-07-10 14:15:42 +02:00
Emmanuele Bassi
439ee4822e Port GArray and friends to gatomicrefcount
Use the newly added API for reference counting instead of rolling our
own.
2018-06-11 14:59:39 +01:00
Philip Withnall
e1e8002998 garray: Optimise over-allocations with g_array_insert_vals()
When over-allocating by inserting values off the end of an array, maybe
expand the array once, rather than twice (once for setting the size and
once for appending the values).

Suggestion by Peter Bloomfield (@peterb) as a follow-up to #1374.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-06-04 11:45:48 +01:00
Philip Withnall
89f45e96b2 garray: Allow over-allocation in g_array_insert_vals()
Previously, g_array_insert_vals() would crash if called with an index
off the end of the array. This is inconsistent with the behaviour of
other methods (like g_array_set_size()), which will expand the array as
necessary.

Modify g_array_insert_vals() to expand the array as necessary. New array
elements will be cleared to zero if the GArray was constructed with
(clear_ == TRUE).

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=795975
2018-06-04 11:11:26 +01:00
Philip Withnall
3eeec77800 garray: Fix (nullable) annotation on GArray.[prepend|insert]_vals()
They do both accept NULL value arrays, but only if the number of
elements in the value array is zero. Fix the annotations and mention
this in the documentation.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=795975
2018-06-04 11:11:26 +01:00
Philip Withnall
d0a48f26c7 garray: Add g_ptr_array_steal_index*() functions
These make it easy to steal elements from pointer arrays without having
the array’s GDestroyNotify called on them, similarly to what’s possible
with g_hash_table_steal().

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=795376
2018-05-09 13:52:05 +01:00
Philip Withnall
e6200eaea2 garray: Document that return value of g_ptr_array_remove() may be junk
If using g_ptr_array_remove*() with a non-NULL GDestroyNotify function,
the value returned will probably be freed memory (depending on what the
GDestroyNotify) function actually does. Warn about that in the
documentation. We can’t just unconditionally return NULL in these cases,
though, since the user might have set the GDestroyNotify to a nifty
function which doesn’t actually free the element; so returning it might
still be valid and useful.

Also add missing (nullable) annotations to that documentation.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=795376
2018-05-09 13:52:05 +01:00
Philip Withnall
2c9a84e5a3 garray: Factor out implementation of g_ptr_array_remove_index*()
They were almost identically the same. This introduces no functional
changes, but will help with upcoming additions to GPtrArray.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=795376
2018-05-09 13:52:05 +01:00
Sam Spilsbury
0e1a26dc49 garray: Steal segment during destruction
And warn in other parts of the code if the caller attempts
to change the array bounds during destruction, this is not
a valid operation.

(Tweaked by Philip Withnall <withnall@endlessm.com> to not use inline
for loop declarations, since we can’t support them in GLib at the
moment.)

https://bugzilla.gnome.org/show_bug.cgi?id=769064
2018-04-20 13:56:55 +01:00
Philip Withnall
90d3337e1d docs: Mention pointer semantics for g_array_set_clear_func()
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Reviewed-by: nobody
2018-02-23 19:23:27 +00:00
Dan Winship
42d3ed0013 glib: document restrictions on various foreach() functions
Some foreach() functions allow you to modify the object they are
iterating, and others don't, but the docs were not generally clear
about this.

https://bugzilla.gnome.org/show_bug.cgi?id=724383
2017-11-16 11:12:32 +00:00
Philip Withnall
2586eb9921 docs: Clarify lack of threading guarantees in GArray
Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=786555
2017-08-22 15:58:18 +01:00
Sébastien Wilmet
f9faac7661 glib/: LGPLv2+ -> LGPLv2.1+
All glib/*.{c,h} files have been processed, as well as gtester-report.

12 of those files are not licensed under LGPL:

	gbsearcharray.h
	gconstructor.h
	glibintl.h
	gmirroringtable.h
	gscripttable.h
	gtranslit-data.h
	gunibreak.h
	gunichartables.h
	gunicomp.h
	gunidecomp.h
	valgrind.h
	win_iconv.c

Some of them are generated files, some are licensed under a BSD-style
license and win_iconv.c is in the public domain.

Sub-directories inside glib/:

	deprecated/: processed in a previous commit
	glib-mirroring-tab/: already LGPLv2.1+
	gnulib/: not modified, the code is copied from gnulib
	libcharset/: a copy
	pcre/: a copy
	tests/: processed in a previous commit

https://bugzilla.gnome.org/show_bug.cgi?id=776504
2017-05-24 11:58:19 +02:00
Philip Withnall
f6f6b3d83c garray: Add g_ptr_array_find[_with_equal_func]()
Partially based on telepathy-glib’s tp_g_ptr_array_contains(), and a
patch by Xavier Claessens <xavier.claessens@collabora.co.uk>.

Test cases included.

https://bugzilla.gnome.org/show_bug.cgi?id=698064
2017-05-02 16:54:41 +01:00
Simon McVittie
e5ed410c8c Avoid calling Standard C string/array functions with NULL arguments
glibc string.h declares memcpy() with attribute(nonnull(1,2)), causing
calls with NULL arguments to be treated as undefined behaviour.
This is consistent with ISO C99 and C11, which state that passing 0
to string functions as an array length does not remove the requirement
that the pointer to the array is a valid pointer.
gcc -fsanitize=undefined catches this while running OSTree's test suite.

Similarly, running the GLib test suite reports similar issues for
qsort(), memmove(), memcmp().

Signed-off-by: Simon McVittie <smcv@debian.org>
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=775510
Reviewed-by: Colin Walters
2016-12-02 19:10:39 +00:00
Christian Hergert
18a33f72db introspection: use (nullable) or (optional) instead of (allow-none)
If we have an input parameter (or return value) we need to use (nullable).
However, if it is an (inout) or (out) parameter, (optional) is sufficient.

It looks like (nullable) could be used for everything according to the
Annotation documentation, but (optional) is more specific.
2016-11-22 14:14:37 -08:00
Philip Withnall
37756a06c9 array: Support clearing an empty array with g_array_remove_range()
Previously, calling g_array_remove_range(array, 0, array->len) on an
empty array would result in a precondition failure in
g_array_remove_range(), as the given start index (0), was not strictly
less than the array length (0).

Allow the index to equal the array length, so that zero elements can be
removed from any array. A subsequent check makes sure that the array
length is not overflowed by the index + length.

https://bugzilla.gnome.org/show_bug.cgi?id=763339
2016-03-09 07:50:07 +00:00
Philip Withnall
25a7c817d3 glib: Add missing (nullable) and (optional) annotations
Add various (nullable) and (optional) annotations which were missing
from a variety of functions. Also port a couple of existing (allow-none)
annotations in the same files to use (nullable) and (optional) as
appropriate instead.

Secondly, add various (not nullable) annotations as needed by the new
default in gobject-introspection of marking gpointers as (nullable). See
https://bugzilla.gnome.org/show_bug.cgi?id=729660.

This includes adding some stub documentation comments for the
assertion macro error functions, which weren’t previously documented.
The new comments are purely to allow for annotations, and hence are
marked as (skip) to prevent the symbols appearing in the GIR file.

https://bugzilla.gnome.org/show_bug.cgi?id=719966
2015-11-07 10:48:32 +01:00