2524 Commits

Author SHA1 Message Date
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
29e27d8dcd Merge branch 'cancellable-connection-fixes' into 'main'
cancellable: Fix connect and disconnect references handling

Closes #3642 and #3643

See merge request GNOME/glib!4573
2025-04-03 13:55:49 +00:00
Marco Trevisan (Treviño)
a2f98c86ff gio/cancellable: Never call toggle notify callback in locked state
We had code to avoid that we could call a toggle "up" notification
callback in locked state, but this was not covering the case in which
the cancellable second to last reference was removed in its cancellation
callback.

In fact, in such case we end up going from 2 -> 1 references during the
signal callback call and this leads to calling the toggle notify
callback in locked state.

To prevent this, add an even further reference before calling the
callback (in locked state, but there's no risk that a toggle-up
notification happens now), and drop it once unlocked again.
2025-04-03 15:22:16 +02:00
Marco Trevisan (Treviño)
ec2fee555f gio/cancellable: Do not call data destroy function while locked
If when calling g_cancellable_connect() the cancellable was already
cancelled we could have ended up in calling the data cleanup
function while the cancellable lock was held.

This is likely not an issue, but it's still better not to do it,
so protect the code against it
2025-04-03 15:22:16 +02:00
Philip Withnall
7ea714e658 Merge branch 'wip/3v1n0/sanitizer-memory-fixes' into 'main'
Memory sanitizer fixes

See merge request GNOME/glib!4484
2025-04-03 13:04:05 +00:00
Marco Trevisan (Treviño)
49680afd5c gio/cancellable: Always add reference while emitting cancelled signal
When a non-cancelled cancellable ::cancelled signal callback is called
the cancellable has enough references so that it can be unreferenced on
the callback itself. However this doesn't happen if the cancellable has
been already cancelled at the moment we connect to it.

To prevent this, add a temporary reference before calling the signal
callback.

Note that we do this also if the callback has not been already cancelled
to prevent that we may end up calling a toggle-notify callback while we
are locked.

Add tests

Closes: #3643
2025-04-03 14:48:57 +02:00
Marco Trevisan (Treviño)
7432441581 test/cancellable: Check that a cancellable can be unreffed on callback
This works when the cancellable has not been cancelled yet.
2025-04-03 14:48:56 +02:00
Marco Trevisan (Treviño)
ea52feb658 gio/tests/gdbus-export: Ensure we call all the callbacks on return
Ensure we don't do an user-after-free access, as reported by ASAN:

  ==3704==ERROR: AddressSanitizer: stack-use-after-return on address
   0x70a58f8631c0 at pc 0x000000405144 bp 0x7fffff62c7a0 sp 0x7fffff62c798
   READ of size 4 at 0x70a58f8631c0 thread T0
    #0 0x405143 in on_object_unregistered ../../GNOME/glib/gio/tests/gdbus-export.c:597
    #1 0x70a592e858d8 in call_destroy_notify_data_in_idle ../../GNOME/glib/gio/gdbusconnection.c:244
    #2 0x70a5940016a4 in g_idle_dispatch ../../GNOME/glib/glib/gmain.c:6221
    #3 0x70a59401095b in g_main_dispatch ../../GNOME/glib/glib/gmain.c:3348
    #4 0x70a59401095b in g_main_context_dispatch_unlocked ../../GNOME/glib/glib/gmain.c:4197
    #5 0x70a59401ba17 in g_main_context_iterate_unlocked ../../GNOME/glib/glib/gmain.c:4262
    #6 0x70a59401cc73 in g_main_context_iteration ../../GNOME/glib/glib/gmain.c:4327
    #7 0x405658 in test_threaded_unregistration_iteration ../../GNOME/glib/gio/tests/gdbus-export.c:1878
    #8 0x405658 in test_threaded_unregistration ../../GNOME/glib/gio/tests/gdbus-export.c:1952
    #9 0x70a5940dfb04 in test_case_run ../../GNOME/glib/glib/gtestutils.c:2988
    #10 0x70a5940dfb04 in g_test_run_suite_internal ../../GNOME/glib/glib/gtestutils.c:3090
    #11 0x70a5940df893 in g_test_run_suite_internal ../../GNOME/glib/glib/gtestutils.c:3109
    #12 0x70a5940df893 in g_test_run_suite_internal ../../GNOME/glib/glib/gtestutils.c:3109
    #13 0x70a5940e0bc9 in g_test_run_suite ../../GNOME/glib/glib/gtestutils.c:3189
    #14 0x70a5940e0d1f in g_test_run ../../GNOME/glib/glib/gtestutils.c:2275
    #15 0x40eb72 in session_bus_run ../../GNOME/glib/gio/tests/gdbus-sessionbus.c:69
    #16 0x403a2c in main ../../GNOME/glib/gio/tests/gdbus-export.c:1990
    #17 0x70a591d9f149 in __libc_start_call_main (/lib64/libc.so.6+0x28149) (BuildId: 0d710e9d9dc10c500b8119c85da75004183618e2)
    #18 0x70a591d9f20a in __libc_start_main_impl (/lib64/libc.so.6+0x2820a) (BuildId: 0d710e9d9dc10c500b8119c85da75004183618e2)
    #19 0x403b44 in _start (/tmp/_build/gio/tests/gdbus-export+0x403b44) (BuildId: f6312e919c3d94e4c49270b0dfc5c870e1ba550b)

Address 0x70a58f8631c0 is located in stack of thread T0 at offset 192 in frame
    #0 0x40525f in test_threaded_unregistration ../../GNOME/glib/gio/tests/gdbus-export.c:1936

  This frame has 7 object(s):
    [32, 40) 'local_error' (line 1835)
    [64, 72) 'unregister_thread' (line 1836)
    [96, 104) 'value' (line 1838)
    [128, 136) 'value_str' (line 1839)
    [160, 168) 'call_result' (line 1840)
    [192, 204) 'object_registration_data' (line 1834) <== Memory access at offset 192 is inside this variable
    [224, 240) 'data' (line 1833)
2025-04-03 13:58:15 +02:00
Marco Trevisan (Treviño)
6ed59a6f40 gio/tests/gdbus-connection: Fix typo 2025-04-03 13:58:15 +02:00
Philip Withnall
e7b820d66c
tests: Move various test paths to g_get_tmp_dir()
Rather than creating files in the current directory. This is a bit
neater, and avoids races between parallel invocations of the unit tests
if the file names aren’t guaranteed to be unique (e.g. by using
`g_mkstemp()`).

Add `G_TEST_OPTION_ISOLATE_DIRS` too, to make sure we use a unique
subdirectory of `g_get_tmp_dir()`. This means that paths like
`g_get_tmp_dir() / some-file` are guaranteed to be race-free even if the
filename is not unique, because the test tmp dir now is.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-03-21 14:02:49 +00:00
Philip Withnall
4d2b873211
tests: Use g_build_filename() rather than g_strdup_printf()
In the `g-file-info-filesystem-readonly` test.

This doesn’t introduce any functional changes, but makes the code a
little easier to read (because the parts of the path are now in
hierarchical order) and makes it a bit clearer that we’re building a
path rather than an arbitrary string.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-03-21 14:02:45 +00:00
Philip Withnall
4345eb2756
tests: Only use g_mkstemp() in /tmp rather than current directory
It’s not entirely clear from the documentation, but `g_mkstemp()` (and
`g_mkdtemp()`) operate in the current directory, rather than the system
temporary directory.

This meant these tests were all writing files to the build directory.
This is messy, though thankfully not a correctness issue or a race
because `g_mkstemp()` guarantees to return a unique file for each
caller.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-03-21 14:02:36 +00:00
Philip Withnall
20f083ea7c
giounix-private: Fix macro for checking for epoll_create1()
Like many things I touch, I broke this in
fd8ede0b661aa67032bbc3e7afc88aff22d7984a.

Spotted by Sebastian Wilhelmi in
fd8ede0b66 (note_2385263).

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-03-20 11:12:58 +00:00
Philip Withnall
9fe6d8b5f0
tests: Disable socket-listener mock tests on macOS
Its symbol interposition works differently to that of Linux, so our
approach using `dlsym(RTLD_NEXT)` to inject syscalls (and still allow
chaining up to the version from libc) doesn’t work on macOS.

See https://gitlab.gnome.org/GNOME/glib/-/jobs/4861349 for an example
failure.

It would be lovely to have these tests working on macOS, but I am not a
macOS developer, and have spent enough time fixing this leak (#1250)
already. It can wait for follow-up work.

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

Helps: #1250
2025-03-13 13:25:19 +00:00
Philip Withnall
05e529f3f8
tests: Add tests for g_socket_listener_add_any_inet_port() algorithm
The algorithm that `g_socket_listener_add_any_inet_port()` and
`g_socket_listener_add_inet_port()` use to try to connect to IPv4 and/or
IPv6 ports are a bit complex (especially when port allocation has to
happen in the former method). So far they’ve not really been unit
tested, which is unfortunate, and has left latent bugs.

Add some unit tests for both methods, by providing mock `socket()` (and
friends) functions to override those from libc, and using those to cause
specific syscalls to fail according to the test’s needs.

These tests demonstrate the fix for #1250 works, as the tests can be run
under memcheck and show no memory leaks. They’ve revealed a follow-up
issue, though — `g_socket_listener_add_any_inet_port()` doesn’t try a
fallback IPv4-only socket if it tries an IPv6 socket and that socket
accepts IPv4 but then fails to `listen()`. I’ve filed issue #3604 for
that.

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

Helps: #1250
2025-03-13 13:25:06 +00:00
Philip Withnall
3920e0f172
tests: Fix an incorrect array length in stream-rw_all test
The array was declared one byte too short to contain the trailing nul
byte for the string literal. Spotted by gcc 15.

Fix it by allowing the compiler to work out the array length.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-03-04 14:20:48 +00:00
Philip Withnall
db26eb22fd Merge branch 'wip/refi64/fix-marshaller-args' into 'main'
gdbus-codegen: Fix callback GUnixFDList parameter order

See merge request GNOME/glib!4524
2025-02-27 14:21:43 +00:00
Ryan Gonzalez
04ecfa9a30 gdbus-codegen: Fix callback GUnixFDList parameter order
GUnixFDList actually comes *after* the GDBusMethodInvocation, but this
was mistakenly putting it first.

Signed-off-by: Ryan Gonzalez <ryan.gonzalez@collabora.com>
2025-02-26 17:06:46 -06:00
Reuben Thomas
e99dbb943b doc: remove unnecessary and confusing code from examples
Three of the four GApplicationCommandLine examples contained this line:

  g_application_set_inactivity_timeout (app, 10000);

It is not explained (which could be confusing for readers trying to
understand the examplese), or necessary. Worse, it causes two of the
examples to pause for ten seconds if they are invoked with no command-line
arguments, which makes them seem broken (and would presumably be reported
as a bug in any real application).

So, remove these calls.

Fixes #3615
2025-02-26 21:48:30 +00:00
Simon McVittie
9f18bb6258 tests: Search the appropriate directories for our GIR XML inputs
During "as-installed" testing, we should search the GIR_DIR for GIR XML,
instead of hard-coding that it is `${prefix}/share/gir-1.0`. This is
not the case on at least Debian, in order to make it possible to
install more than one architecture's flavour of `GLib-2.0.gir`,
which contains some architecture-specific `#define`s.

Also search GOBJECT_INTROSPECTION_DATADIR/GIR_SUFFIX (in practice
something like `/usr/share/gir-1.0` in all cases) to accommodate
distributions like Debian that move the architecture-independent
majority of GIR XML into /usr/share to avoid duplication, leaving
only the architecture-specific minority of files like `GLib-2.0.gir`
in the GIR_DIR.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2025-02-21 16:40:47 +00:00
Philip Withnall
c753d26b84 Merge branch 'wip/smcv/gdbus-clear-signal' into 'main'
gdbus: Add g_clear_dbus_signal_subscription() inline convenience function

See merge request GNOME/glib!4493
2025-02-20 00:48:33 +00:00
Simon McVittie
030fbca6c2 gdbus: Add g_clear_dbus_signal_subscription() inline convenience function
This is like g_clear_signal_handler(), but for GDBus signal subscriptions.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2025-02-20 00:10:02 +00:00
Marco Trevisan (Treviño)
14da9d459c gio/tests/cancellable: Actually init atomic values with atomic functions
This is to make thread sanitizer happy, even those aren't really issues.
2025-02-19 19:38:12 +01:00
Philip Withnall
67a2f8c593 Merge branch '3560-register-object-with-closures' into 'main'
gdbusconnection: Add g_dbus_connection_register_object_with_closures2() API

Closes #3560

See merge request GNOME/glib!4431
2025-02-18 18:45:03 +00:00
Philip Withnall
9d81da62f2
tests: Fix a minor leak in a new GSubprocess test
And also rearrange the asserts so that the error is checked first (since
that’s the more informative failure to receive if the test did fail).

Spotted by Marco in
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/4494#note_2352013
and by The Machines in
https://gitlab.gnome.org/GNOME/glib/-/jobs/4768121.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-02-18 15:04:06 +00:00
Georges Basile Stavracas Neto
14909ffe1e
gio/subprocess: Initialize pid variable to 0
The documentation for g_spawn_async_with_pipes_and_fds() states:

> If an error occurs, child_pid, stdin_pipe_out, stdout_pipe_out, and
> stderr_pipe_out will not be filled with valid values.

Before 2dc3a6f0c80e5a8f786369eee0c45bfe19b55f4f, the `child_pid`
argument was `self->pid`, and GObject zero-initializes structs. So
the pid field was properly initialized to zero.

After 2dc3a6f0c80e5a8f786369eee0c45bfe19b55f4f, however, the out
variable is now declared inside initable_init(), and it's unitialized.
So if g_spawn_async_with_pipes_and_fds() errors out, `pid` will have
trash value in it, and the following assertion will fail:

```
g_assert (success == (pid != 0));
```

Fix that by initializing the `pid` variable to zero. Add a test to
exercise the fail code path, and prevent errors like this in the
future.
2025-02-14 21:20:24 -03:00
Philip Withnall
40fd0e4759 Merge branch 'wip/3v1n0/gfilemonitor-atomic-cancelled' into 'main'
gio/gfilemonitor: Use atomic API to get / store cancelled state

See merge request GNOME/glib!4487
2025-02-13 18:43:50 +00:00
Marco Trevisan (Treviño)
218b298a1b gio/gfilemonitor: Use atomic API to get / store cancelled state
The cancelled state may be set and read by different threads, so ensure
that it's stored and managed in an atomic way.

We could in fact end up check for `g_file_monitor_is_cancelled()` in a
thread and `g_file_monitor_cancel()` or `g_file_monitor_emit_event` in
in another one.
2025-02-13 15:03:57 +01:00
Marco Trevisan (Treviño)
00ebf4e1eb tests/lib: Add a new unittest type to simplify launching test programs
We were reusing the same logic everywhere, while we can just reuse an
unique class to base our tests on that avoids having to copy-and-paste
code for no good reason
2025-02-11 18:51:15 +01:00
Marco Trevisan (Treviño)
4bcd99de43 tests: Avoid reusing and installing multiple files the taptestrunner
Add some basic support for having glib-tests-only python libraries that
can be shared across the various projects, so that we don't have to
maintain multiple copies of them.
2025-02-11 18:51:15 +01:00
Philip Withnall
14a756ae97
gdbusconnection: Add g_dbus_connection_register_object_with_closures2() API
This replaces `g_dbus_connection_register_object_with_closures()`, and
becomes the new binding-friendly version of
`g_dbus_connection_register_object()`.

The problem with `g_dbus_connection_register_object_with_closures()` is
that the `method_call_closure` kept the reference counting semantics of
`GDBusInterfaceMethodCallFunc`, in that the `invocation` argument was
`(transfer full)`, even though it was wrapped in a `GClosure`. This
couldn’t be described in introspection annotations, so the
`GDBusMethodInvocation` was being leaked by bindings. Some bindings
added workarounds to fix the leak at our direction (see
https://gitlab.gnome.org/GNOME/glib/-/issues/2600#note_1385050), which
meant we could no longer change the reference counting behaviour without
breaking those bindings (see #3559).

So let’s start afresh with
`g_dbus_connection_register_object_with_closures2()`, with correctly
defined reference counting semantics (the `GDBusMethodInvocation` is
`(transfer none)`) from the start.

Unfortunately we can’t add a `(rename-to)` annotation to the new API, as
that would effectively be an API break for existing binding code which
uses the old API via that rename.

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

Fixes: #3560
2025-02-11 14:08:05 +00:00
Marco Trevisan (Treviño)
dba2d618d5 gio/tests/power-profile-monitor*.py: Use upower ppd daemon APIs 2025-02-07 00:55:35 +01:00
Marco Trevisan (Treviño)
71d08b707a gio/tests/power-profile-monitor-*.py: Fix style using black 2025-02-07 00:55:35 +01:00
Philip Withnall
ff2c5c3e74
tests: Fix non-atomic accesses to atomic variables in cancellable test
As suggested by Marco Trevisan in !4206.

This might eliminate some spurious failures of the test.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-02-04 14:06:15 +00:00
Philip Withnall
4641535596
Revert "gdbus: Fix leak of method invocation when registering an object with closures"
This reverts commit 092fedd5f085a2f1966b5c34befe8b603c1a0f07.

This was not the right change to make, and I shouldn’t have accepted the
MR. The situation is laid out in this comment:
https://gitlab.gnome.org/GNOME/glib/-/issues/2600#note_1385050

tl;dr: The reference on the `GDBusMethodInvocation` which is transferred
in to the `GDBusInterfaceMethodCallFunc` is balanced by a reference
transferred to `g_dbus_method_invocation_return_*()`. This is how the
refcounting has always worked for these functions, and even if we’d
probably arrange things differently if the code was written now, we
can’t change those semantics without breaking API.

In particular, bindings have various bits of custom code to account for
these reference tranfers (since they can’t be represented using
gobject-introspection annotations), so changing the semantics will break
bindings.

Fixes: #3559
2024-12-10 15:53:54 +00:00
Philip Withnall
8c3fda5c8d
tests: Skip unsupported dbus-appinfo test on GNU/Hurd for the moment
Parts of the `dbus-appinfo` test need support for converting an FD to a
path, and Hurd doesn’t currently allow that (see
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/4396#note_2279923).

Since there’s no fix for that visible in the medium term (new kernel
APIs will need to be added), skip parts of the `dbus-appinfo` test which
require that functionality for now.

This prevents the whole test from failing, and means we can usefully get
results from the parts of it which don’t depend on converting FDs to
paths.

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

Helps: #3538
2024-12-10 11:47:43 +00:00
Martin Domig
70fe6839a5 Fix typo in test_extended_schema
Signed-off-by: Martin Domig <martin.domig@wolfvision.net>
2024-12-08 22:15:03 +01:00
Martin Domig
d63d27b195 Add test for g_settings_schema_has_key on extended schema
The test checks that the keys of the base schema do all exist in the
extended schema.

Signed-off-by: Martin Domig <martin.domig@wolfvision.net>
2024-12-08 22:13:51 +01:00
Sebastian Dröge
092fedd5f0 gdbus: Fix leak of method invocation when registering an object with closures
The invocation passed in is owned by the callback.
2024-12-02 16:46:17 +00:00
Philip Withnall
c64f82fa79 Merge branch 'wip/pwithnall/procfs-refactor' into 'main'
tests: Factor out a function to convert FD to path

See merge request GNOME/glib!4396
2024-11-26 12:06:28 +00:00
Philip Withnall
0a48bfe79a
tests: Factor out a function to convert FD to path
This could potentially eventually become a public GLib API, but there
doesn’t seem to be a huge need for it right now (e.g. this file contains
the only use of `/proc/self/fd/%d` in GLib), so let’s keep it private
for now and avoid committing to API stability just yet.

This gives time for other platforms to add their platform-specific
implementations for it too, if they need. I’ve added a couple of
pointers to what I *think* the right APIs might be, from my research,
but I have not prototyped those implementations.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2024-11-19 11:54:25 +00:00
Benjamin Otte
33b9338fb0 converteroutputstream: Raise error for data at end
When a conversion is finished, the code would return 0 from its write
vfunc. This is disallowed by the API of g_output_stream_write() and
causes g_output_stream_splice() as used by g_converter_convert_bytes()
to turn into an infinite loop.

Instead, raise a G_IO_ERROR_MESSAGE_TOO_LARGE error so that the calling
code can decide how to deal with it.

Testcase included.

Closes #3532
2024-11-16 07:46:24 +00:00
Philip Withnall
7dacfa7799 Merge branch 'wip/hadess/test-warning-fix' into 'main'
appmonitor: Fix warning building test

See merge request GNOME/glib!4395
2024-11-13 13:40:45 +00:00
Gleb Popov
6d67568da3 Replace procfs linuxism with kinfo freebsdism 2024-11-13 13:29:01 +00:00
Bastien Nocera
48b980ca7d appmonitor: Fix warning building test
gio/tests/appmonitor.c: In function ‘timeout_cb’:
gio/tests/appmonitor.c:95:14: warning: null pointer dereference [-Wnull-dereference]
   95 |   *timed_out = TRUE;
2024-11-13 14:24:50 +01:00
Arjan Molenaar
6e4b70e062 macos: content-type tests should succeed 2024-11-09 13:42:47 +01:00
Arjan Molenaar
bc8b7041a9 macos: Add extra content type tests 2024-11-08 22:34:38 +00:00
Philip Withnall
c14810e08f
tests: Fix calls to deprecated API in unix-mounts tests
This fixes commit aac56f1618aabfcf4c6b3ef1ee5b87322208e9ad — I missed
this while reviewing it, but the unit tests were partially changed to
call the new APIs, without being fully changed. This caused the build to
succeed on Linux, but fail on macOS due to using a deprecated API.

Actually, a better approach for the unit tests would be to consistently
call the *old* APIs, as they all immediately call the new APIs. Then we
get coverage of both old and new for free, at the cost of putting
`G_GNUC_BEGIN_IGNORE_DEPRECATIONS` at the top of the test file.

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

Helps: #3492
2024-10-22 16:15:18 +01:00
Philip Withnall
e795c715b5 Merge branch 'fix-namespace-UnixMountEntry' into 'main'
UnixMountEntry: Deprecate g_unix_mount_* API in favor of g_unix_mount_entry_* API for GUnixMountEntry methods

Closes #3492

See merge request GNOME/glib!4337
2024-10-22 14:48:12 +00:00
Jialu Zhou
aac56f1618 UnixMountEntry: Deprecate g_unix_mount_* API in favor of g_unix_mount_entry_*
This issue arises because the g_unix_mount_* naming convention does not match
the GUnixMountEntry instance type, confusing the introspection generator.

To resolve this, we are deprecating the g_unix_mount_* API functions that take
a GUnixMountEntry parameter and introducing equivalent g_unix_mount_entry_*
functions that correctly associate with the GUnixMountEntry instance. This change
ensures that introspection data correctly treats these as instance methods and
that documentation reflects proper ownership of returned data.

(Some minor tweaks by Philip Withnall.)

Fixes: #3492
2024-10-22 15:31:52 +01:00