31618 Commits

Author SHA1 Message Date
Philip Withnall
98ab3d7a87
xdgmime: Disable -Wsign-conversion warning inside xdgmime
The code wasn’t written for it, and we don’t want to diverge from
upstream for this, so disable -Wsign-conversion in case it was set for
the overall GLib build.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
2025-04-11 14:02:55 +01:00
Philip Withnall
7aeb4d94f2
gcharset: Fix potential negative string offsets in g_get_locale_variants()
The parser was assuming that all three separators (`_@.`) were in order;
but the input might not contain them in order. In that case, the parser
would have passed negative values to `g_strndup()` which would have been
implicitly cast to large positive values, and potentially exposed a lot
of memory (until the first nul byte, which was probably quite soon).

Expand the existing `g_get_locale_variants()` test to cover some invalid
parsing, and add a fuzzing test too.

Spotted by `-Wsign-conversion`.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
2025-04-11 14:02:51 +01:00
Philip Withnall
c6884c95f6
tests: Use g_assert_*() rather than g_assert() in utils tests
It won’t get compiled out with `G_DISABLE_ASSERT`.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-11 14:02:46 +01:00
Philip Withnall
07db5ba5f6
gcharset: Fix a trivial -Wsign-conversion warning
Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
2025-04-10 20:36:00 +01:00
Philip Withnall
79fa5fc305
grel: Fix sign of GRealTuples.width
Thankfully this struct is also internal, so we can happily change the
types of any field except the first one (which is in the public `GTuples`
prefix).

This fixes the remaining `-Wsign-conversion` warnings for `grel.c`.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
2025-04-10 20:35:55 +01:00
Philip Withnall
ea06530b6e
grel: Fix the internal type of GRealTuples.len to match the public API
`GTuples.len` is a `guint`.

This doesn’t seem to have caused any problems or warnings, but let’s get
it correct.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-10 20:35:51 +01:00
Philip Withnall
c5afe67022
grel: Factor out internal helper to improve type safety
We would otherwise have to have cast and lost width and signedness on
both the arguments to, and return value from, `g_relation_count()`.
Simpler to add an internal helper with the right types, and only do the
casts on the public API wrapper where API stability means it’s
unavoidable.

Fixes some more `-Wsign-conversion` warnings.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
2025-04-10 20:35:46 +01:00
Philip Withnall
ef065dd195
grel: Validate field index input to public functions
Previously this was passed straight into an array dereference, so could
easily have under- or over-flowed the array.

Add a precondition check to it. This doesn’t change the API contract,
because the code already emitted a critical warning if the index didn’t
find a table:
```
g_return_val_if_fail (table != NULL, NULL)
```

This fixes a load of `-Wsign-conversion` warnings and makes these
functions safer.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
2025-04-10 20:35:41 +01:00
Philip Withnall
29068a5a37
grel: Check the number of fields is 2
This is what’s documented as supported (and the only value that is
supported), so might as well check for it early.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-10 20:35:35 +01:00
Philip Withnall
03c766879a
grel: Fix some -Wsign-conversion warnings in GRelation
The internals of this struct are private, so we have some freedom to fix
the types used to be wider (to allow addressing all of memory) and
correctly unsigned.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
2025-04-10 20:35:03 +01:00
Philip Withnall
2dd6271a9a
gallocator: Fix a harmless -Wsign-conversion warning
The code already checked that `atom_size` was positive.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
2025-04-10 18:33:20 +01:00
Philip Withnall
40dd9ea9e0
gthread-deprecated: Fix some -Wsign-conversion warnings
These weren’t bugs.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
2025-04-10 18:32:41 +01:00
Philip Withnall
9aa4f9f9c3
libcharset: Disable -Wsign-conversion warning inside libcharset
The code wasn’t written for it, and we don’t want to diverge from
upstream for this, so disable -Wsign-conversion in case it was set for
the overall GLib build.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
2025-04-10 18:31:43 +01:00
Philip Withnall
551cdd98dc Merge branch '3658-file-info-docs' into 'main'
gfile: Expand documentation around file info for inaccessible files

See merge request GNOME/glib!4585
2025-04-08 16:19:34 +00:00
Philip Withnall
a33df3d81e
gfile: Expand documentation around file info for inaccessible files
Make it a bit clearer that a returned `GFileInfo` can be empty if
`stat()` fails (e.g. with `EACCES`), and that it’s the caller’s
responsibility to use `g_file_info_has_attribute()` before retrieving
attribute values.

This better documents what we implemented in, for example, !3336.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3658
2025-04-08 16:59:20 +01:00
Philip Withnall
9d466bddc2
gfile: Port the g_file_query_info() doc comment to gi-docgen
This doesn’t change its content.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3250
2025-04-08 16:54:19 +01:00
Philip Withnall
2cd78508cc Merge branch 'th/bit-lock-and-set' into 'main'
[th/bit-lock-and-set] bitlock: add g_bit_lock_and_get() and g_bit_unlock_and_set() API

See merge request GNOME/glib!4578
2025-04-07 15:53:40 +00:00
Thomas Haller
90e6e2b528 bitlock: factor out duplicated futex wait/wake functions 2025-04-07 17:09:51 +02:00
Thomas Haller
493b9c49af bitlock: mark g_futex_int_address() as G_ALWAYS_INLINE
It probably would get inlined anyway(?). But add G_ALWAYS_INLINE
to be more sure about this. We don't want this mostly trivial
function to be a separate call.
2025-04-07 17:09:51 +02:00
Thomas Haller
893e89d116 bitlock/tests: add test for using bit lock API in parallel
Add a test that runs a few threads, which take a bit lock and
collectively increment a counter.
2025-04-07 17:09:51 +02:00
Thomas Haller
b8f0117b95 bitlock: add g_bit_lock_and_get() and g_bit_unlock_and_set() API
These are similar to g_pointer_bit_lock_and_get() and
g_pointer_bit_unlock_and_set().

The bitlock API is pretty amazing, as it allows to use a single bit of
an integer for locking while using the remaining 31 bits for other
purposes. But those other bits always need to be accessed atomically
too.

There is a use in being able to lock_and_get(), to combine the setting
of the lock bit and fetching the new value at once. For one, that can
safe additional atomic operations to fetch the value afterwards. But
more importantly, it allows to do this change in one atomic operation.

Likewise, unlock_and_set() allows to atomically clear the lock bit and
set a new value (while also preserving unrelated bits, by using the
@preserve_mask parameter).
2025-04-07 17:09:03 +02:00
Thomas Haller
7edc1353bd bitlock: improve doctext for g_pointer_bit_unlock_and_set() 2025-04-07 16:51:14 +02:00
Philip Withnall
976d01f813 Merge branch 'jsparber/allow_nested_handle' into 'main'
gdbus-tool call: Allow handles inside nested containers

Closes #3624

See merge request GNOME/glib!4525
2025-04-07 13:38:35 +00:00
Julian Sparber
91a1ff3b5c gdbus-tool call: Allow handles inside nested containers
This recursively walks the entire input of `gdbus call` to find file
descriptor handles allowing to nest handles inside container types.

Closes: https://gitlab.gnome.org/GNOME/glib/-/issues/3624
2025-04-07 15:12:07 +02:00
Philip Withnall
97fd345cb2 Merge branch 'th/gobj-no-weak-ref-lock' into 'main'
[th/gobj-no-weak-ref-lock] drop OPTIONAL_BIT_LOCK_WEAK_REFS object lock for `g_object_weak_{ref,unref}()`

See merge request GNOME/glib!4581
2025-04-07 12:08:35 +00:00
Thomas Haller
93b5b8a051 gobject: drop OPTIONAL_BIT_LOCK_WEAK_REFS bit lock
With the previous changes, all accesses to the WeakRefStack go through
g_datalist_id_update_atomic() and are guarded by the GData's bit lock.
At this point, the OPTIONAL_BIT_LOCK_WEAK_REFS lock is unnecessary and
can be dropped.

A minor benefit is that g_object_weak_{ref,unref}() needs now one lock
less.

Also note that this rework fixes a potential race for weak refs. Note
that we have two calls

  g_datalist_id_set_data (&object->qdata, quark_weak_notifies, NULL);

that don't take OPTIONAL_BIT_LOCK_WEAK_REFS lock. One of these calls is
right before finalize(). At that point, no other thread can hold a
reference to the object to race and we are good. However, the other call
is from g_object_real_dispose(). At that point, theoretically the object
could have been resurrected and a pointer could have been passed to
another thread. Calling then g_object_weak_ref()/g_object_weak_unref()
will race. We would have required a OPTIONAL_BIT_LOCK_WEAK_REFS lock
around those g_datalist_id_set_data(,,NULL) calls.

Instead, this is now also fixed, because every update to the
WeakRefStack happens while holding the GData lock. So if you call
g_datalist_id_set_data(,,NULL), the WeakRefStack is removed from the
GData (and later freed by weak_refs_notify() and can no longer
concurrently updated by g_object_weak_{ref,unref}().
2025-04-07 12:43:08 +01:00
Thomas Haller
5da7ed2bc9 gobject: rework g_object_weak_ref() to use _g_datalist_id_update_atomic()
This is a step step to drop OPTIONAL_BIT_LOCK_WEAK_REFS lock. See the
next commits why that is done.
2025-04-07 12:43:06 +01:00
Thomas Haller
4e1039ea76 gobject: rework g_object_weak_unref() to use _g_datalist_id_update_atomic()
This is a step step to drop OPTIONAL_BIT_LOCK_WEAK_REFS lock. See the
next commits why that is done.

Also, free the WeakRefStack, if there are no more references.
Previously, it was never freed.
2025-04-07 12:42:49 +01:00
Marco Trevisan
ef3fa35689 Merge branch 'gthread-linux-macro' into 'main'
thread: fix Linux detection

See merge request GNOME/glib!4583
2025-04-07 05:55:10 +00:00
Sam James
90e09a8b9f
thread: fix Linux detection
The usual macro for Linux is `__linux__`, not `__LINUX__`. Noticed this
after Ruby fixed a similar error (6fbc32b5d0da31535cccc0eca1853273313a0b52).

Fixes: 5b84636e62d2c77ad4fee49742750a3df762736c
2025-04-07 03:26:09 +01:00
Philip Withnall
0a7eb121fc Merge branch 'closure-cleanups' into 'main'
gclosure: Allow full set of closure flags to be queried atomically

See merge request GNOME/glib!4577
2025-04-04 15:29:51 +00:00
Philip Withnall
782c276d1e Merge branch 'gerror-improvements-mappedfile' into 'main'
mappedfile: Avoid some allocations

See merge request GNOME/glib!4247
2025-04-04 14:37:55 +00:00
Michael Catanzaro
8628ab1d1a Merge branch 'wip/pwithnall/scanner-test-fixes-macos' into 'main'
tests: Add missing unistd.h include to scannerapi.c

See merge request GNOME/glib!4579
2025-04-04 14:11:40 +00:00
Philip Withnall
374a274c89
tests: Fix a size_t-to-int32 conversion warning on Windows
Spotted while looking at CI output for the previous commit, and I
thought I might as well fix it while I was there.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-04 13:46:39 +01:00
Philip Withnall
198285bda8
tests: Add missing unistd.h include to scannerapi.c
This should fix compile errors on macOS, which couldn’t find the
`write()`, `lseek()`, etc. functions added in commit
020e58bac53e2d06e27cb1ab85d488e743abe9b4.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-04 13:41:39 +01:00
Philip Withnall
3f56b84e75
gclosure: Remove unused macro argument
It was always passed the same value by all users of the macro.

This introduces no functional changes.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-04 13:09:23 +01:00
Philip Withnall
cd45d1ddcd
gclosure: Combine flags reads with refs in some methods
Rather than atomically querying the flags, and then atomically reffing
the `GClosure`, do the atomic ref and return the new flags value as a
side-effect. This eliminates the need for an additional atomic read, and
eliminates a race window between the two accesses.

It does mean we need a new internal version of `g_closure_ref()` which
returns the new `GClosureFlags` though.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-04 13:09:17 +01:00
Philip Withnall
c9ef461aa1
gclosure: Ensure all reads of GClosure.ref_count are atomic
Remove the last few non-atomic reads of `GClosure.ref_count`. See
previous commits for an explanation of why this is important.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-04 01:29:36 +01:00
Philip Withnall
d1beecf1a3
gclosure: Allow full set of closure flags to be queried atomically
Currently, `GClosure` does atomic writes to the flags in its initial
bitfield, but it never does atomic reads from them. This is not safe —
even if the memory read for an int is atomic, the compiler might
duplicate or rearrange a non-atomic read and give an unexpected result.
Using an atomic fetch inserts the necessary compiler and hardware
barriers to ensure a single result is fetched.

(See !4575 of an example of where this kind of bug has caused
misbehaviour.)

So, introduce an atomic read helper for the `GClosure` bitfield. Rather
than reading a single member of the bitfield, it returns the entire
bitfield, as several `GClosure` methods need access to multiple members
of the bitfield, and atomically fetching them all separately would not
be atomic overall.

Fix various `GClosure` methods to use the new atomic read function.
There are more parts of the code still to fix — these were just the
straightforward ones.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-04 01:29:26 +01:00
Philip Withnall
b9a5a465a7
gclosure: Rename ClosureInt helper union
This introduces no functional changes, but will make upcoming commits a
little cleaner.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-04 00:58:24 +01:00
Philip Withnall
148c1ed891 Merge branch 'gclosure-atomic-oops' into 'main'
gclosure: fix ATOMIC_CHANGE_FIELD to read vint atomically

See merge request GNOME/glib!4575
2025-04-03 22:38:46 +00:00
Sam James
d6798089d4
gclosure: fix ATOMIC_CHANGE_FIELD to read vint atomically
Depending on luck, g_closure_ref may access closure->vint and observe
different values between reads. This manifests as a test failure in
signals-refcount{2,4}, properties-refcount1, and closure-refcount depending
on timing and re-runs.

Jakub Jelinek analysed this on GCC bug PR119607 after I'd reported it
over there as a possible GCC regression.

The critical part being g_closure_ref -> ATOMIC_INC_ASSIGN -> ATOMIC_CHANGE_FIELD
where closure->vint gets re-read repeatedly, both outside and inside the retry
loop. To fix that:

1. Atomically fetch it the first time;
2. Use the cached read, not a fresh read, of vint in the loop;
3. Use g_atomic_int_compare_and_exchange_full in the loop so we get a freshly
cached vint if it changed in another thread.

Bug: https://gcc.gnu.org/PR119607
Fixes: 834ddd19 ('turned all modifications to the first 32 integer bits in a closure into')
Co-authored-by: Jakub Jelinek <jakub@redhat.com>
2025-04-03 21:23:56 +01:00
Philip Withnall
3779508a69 Merge branch '3617-date-week-of-year' into 'main'
gdate: Add week-of-year APIs for weeks starting on any day

Closes #3617

See merge request GNOME/glib!4506
2025-04-03 16:07:25 +00:00
Philip Withnall
c4af0f0bd0 Merge branch 'th/gdataset-cleanup' into 'main'
[th/gdataset-cleanup] minor cleanups of gdataset

See merge request GNOME/glib!4520
2025-04-03 15:25:20 +00:00
Philip Withnall
1bf83890a0 Merge branch '1250-socket-listener-leak' into 'main'
gsocketlistener: Fix IPv4 listen() error handling

Closes #1250

See merge request GNOME/glib!643
2025-04-03 15:15:59 +00:00
Philip Withnall
8d9e21ff14
gdate: Add week-of-year APIs for weeks starting on any day
`GDate` currently has some API which is specific to which day of the week
you think the week starts on:
 * `g_date_get_monday_week_of_year()`
 * `g_date_get_monday_weeks_in_year()`
 * `g_date_get_sunday_week_of_year()`
 * `g_date_get_sunday_weeks_in_year()`

This is to deal with the difference between locales which think that the
week starts on a Sunday (such as `LANG=en_US.utf8` or `LANG=he_IL.utf8`),
or a Monday (such as `LANG=en_GB.utf8`).

However, there are some locales which think that the week starts on a
Saturday (such as `LANG=ar_EG.utf8`). Currently, GLib provides no API for
those.

So, add some API which is parameterised by the first day of the week,
which will deal with weeks which start on a Saturday (and also any other
day of the week, although I don’t believe there are any countries which
use a day other than Saturday, Sunday or Monday).

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Fixes: #3617
2025-04-03 16:12:53 +01:00
Philip Withnall
f31c072005 Merge branch 'pgriffis/gresolver-onlyloopback' into 'main'
threadedresolver: Return valid addresses when only having loopback interfaces

Closes #3641

See merge request GNOME/glib!4566
2025-04-03 15:09:58 +00:00
Philip Withnall
792c4505e0 Merge branch 'th/gobj-closure-array-atomic' into 'main'
[th/gobj-closure-array-atomic] use g_datalist_id_update_atomic() for array of closure watches

See merge request GNOME/glib!4536
2025-04-03 15:09:06 +00:00
Philip Withnall
56515e93a7 Merge branch '3527-Wshorten-64-to-32' into 'main'
Fix various -Wshorten-64-to-32 warnings

See merge request GNOME/glib!4387
2025-04-03 15:07:09 +00:00
Philip Withnall
8b3c34a523
build: Post-release version bump
This opens up the development cycle for 2.86.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-03 15:56:40 +01:00