31640 Commits

Author SHA1 Message Date
Philip Withnall
ba4f5ca44e
tests: Explicitly cast src value in param conversion tests
This fixes a load of -Wsign-conversion warnings. The dest type setter
function is being used (presumably by design?) so there’s sometimes a
type mismatch (signed/unsigned, or size) with the constant value being
used by the test. This just makes the existing implicit casts explicit.

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

Helps: #3405
2025-04-11 23:48:09 +01:00
Philip Withnall
bad7a32504
tests: Fix various -Wsign-conversion warnings with flags in gobject tests
Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
2025-04-11 23:48:04 +01:00
Philip Withnall
ee2d25b57a
tests: Fix various straightforward -Wsign-conversion warnings
In the gobject tests.

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

Helps: #3405
2025-04-11 23:48:00 +01:00
Philip Withnall
b3ebef609f
tests: Fix various unsigned/signed comparisons in gobject tests
Fix all the instances where `-Wsign-conversion` was pointing out that
`g_assert_cmpint()` had been used on unsigned inputs, or vice-versa.

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

Helps: #3405
2025-04-11 23:47:56 +01:00
Philip Withnall
752c0787d6
gparamspecs: Fix -Wsign-conversion warnings for large constants
Not sure why these constants were chosen the way they were, but that’s
not a problem I’m going to investigate right now. This just makes the
implicit cast explicit to shut the compiler warning up.

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

Helps: #3405
2025-04-11 23:47:51 +01:00
Philip Withnall
633e49c8d1
gobject: Cast various inverted bitfield constants to unsigned
This fixes `-Wsign-conversion` warnings, though I’m not sure why the
compiler is emitting them. The signed/unsigned status of flag enum
members is not particularly well defined in the C standard (and even
less well understood by me), so just do what seems necessary to shut the
compiler up.

The benefits of enabling `-Wsign-conversion` across the codebase
hopefully outweighs this noise.

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

Helps: #3405
2025-04-11 23:47:47 +01:00
Philip Withnall
70ddc35340
gparamspecs: Fix some guint to gboolean conversion warnings
While we’re at it, rename the variables to make the intent a bit
clearer: these functions return a boolean indicating whether any of the
values were modified to make them valid. `n_changed` is a counter of the
number of modified values.

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

Helps: #3405
2025-04-11 23:47:42 +01:00
Philip Withnall
615cd4c10c
gobject: Fix a guint to gboolean conversion warning
Make the conversion explicit. Fixes some `-Wsign-conversion` warnings.

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

Helps: #3405
2025-04-11 23:47:38 +01:00
Philip Withnall
636bbd1d63
gobject: Fix several int/unsigned conversions with atomics
Unfortunately the signatures of our atomic functions alternate between
using signed and unsigned integers across different functions, so we
can’t just use one type as input. Add some explicit casts to fix
harmless `-Wsign-conversion` warnings.

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

Helps: #3405
2025-04-11 23:47:33 +01:00
Philip Withnall
efed9028fa
gobject: Fix various straightforward -Wsign-conversion warnings
None of these should have caused user-visible bugs.

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

Helps: #3405
2025-04-11 23:47:29 +01:00
Philip Withnall
b9d2719222
gboxed: Use new g_string_copy() as boxed copy for GString
Rather than reinventing it ourselves. The old version in `gboxed.c`
could lose the second half of very long strings, as it truncated the
`size_t` string length to the `ssize_t` accepted by
`g_string_new_len()`.

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

Helps: #3405
2025-04-11 23:47:24 +01:00
Philip Withnall
3048266aa3
gstring: Add a new g_string_copy() method
This does a deep copy on the `GString`.

It means we can eliminate the less-efficient version currently used for
`GBoxed`, which also has `-Wsign-conversion` problems. See the following
commit.

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

Helps: #3405
2025-04-11 23:47:19 +01:00
Philip Withnall
b6937c9b32
tests: Remove unnecessary prefix from GString test names
Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-04-11 14:44:21 +01:00
Philip Withnall
2d66caf6cb Merge branch 'wsign-conversion' into 'main'
Various -Wsign-conversion warning fixes

See merge request GNOME/glib!4590
2025-04-11 13:35:16 +00:00
Philip Withnall
1cd4ace6a5
gchecksum: Add an internal helper to avoid signed conversion
Add an internal helper for `g_checksum_update()` to avoid having to take
its length as a `gssize` for internal users who already know the input
length.

Fixes a `-Wsign-conversion` warning.

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

Helps: #3405
2025-04-11 14:03:04 +01:00
Philip Withnall
7bcabd3900
gchecksum: Fix various -Wsign-conversion warnings
These are all fairly straightforward cases.

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

Helps: #3405
2025-04-11 14:02:59 +01:00
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
Marco Trevisan
4f2be73c7e Merge branch 'gstring-gsize' into 'main'
gstring: Make len_unsigned unsigned

See merge request GNOME/glib!4592
2025-04-11 05:52:33 +00:00
Peter Bloomfield
4d435bb480 gstring: Make len_unsigned unsigned 2025-04-11 05:52:33 +00:00
Michael Catanzaro
5c48576223 Merge branch 'mcatanzaro/check-only-has-loopback-interfaces' into 'main'
gthreadedresolver: fix crash in loopback interface check

See merge request GNOME/glib!4591
2025-04-10 21:37:45 +00:00
Michael Catanzaro
defec7eef2
gthreadedresolver: fix crash in loopback interface check
It's expected that an interface may have a NULL socket address, so just
skip it and don't crash if so. In practice, this occurs for VPN
interfaces.

Fixes: fe0139ee985075f3c12932d7123cb594356e7e9d
2025-04-10 15:42:21 -05: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
Michael Catanzaro
44ac298dca Merge branch 'mcatanzaro/gstring-overflow' into 'main'
gstring: carefully handle gssize parameters

See merge request GNOME/glib!4588
2025-04-10 18:39:35 +00: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
Michael Catanzaro
cc647f9e46
gstring: carefully handle gssize parameters
Wherever we use gssize to allow passing -1, we need to ensure we don't
overflow the value by assigning a gsize to it without checking if the
size exceeds the maximum gssize. The safest way to do this is to just
use normal gsize everywhere instead and use gssize only for the
parameter.

Our computers don't have enough RAM to write tests for this. I tried
forcing string->len to high values for test purposes, but this isn't
valid and will just cause out of bounds reads/writes due to
string->allocated_len being unexpectedly small, so I don't think we can
test this easily.
2025-04-10 10:57:20 -05: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