9053 Commits

Author SHA1 Message Date
Philip Withnall
109dc30581 tests: Add descriptions to environment tests
Prompted by !500.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2023-05-22 17:33:29 +01:00
Sebastian Dröge
dae7c2a706 gstring: Add Since marker to g_string_new_take() 2023-05-21 10:04:29 +03:00
Thomas Haller
3694dac983 gmain: ensure boolean value in g_child_watch_check() is strictly 0 or 1
No problem in practice, but it seems nice to ensure that a gboolean is
always either FALSE or TRUE.
2023-05-18 11:26:33 +02:00
Thomas Haller
d5d6ef8f1b gmain: drop redundant using_pidfd field from GChildWatchSource
It's redundant, which leads to impossible code like:

   if (child_watch_source->using_pidfd)
     {
       if (child_watch_source->poll.fd >= 0)
         close (child_watch_source->poll.fd);
2023-05-18 11:26:33 +02:00
Thomas Haller
cecbb25eeb gmain: fix race with waitpid() and child watcher sources
GChildWatchSource uses waitpid(), among pidfd and GetExitCodeProcess().
It thus only works for child processes which the user must ensure to
exist and not being reaped yet. Also, the user must not kill() the PID
after the child process is reaped and must not race kill() against
waitpid(). Also, the user must not call waitpid()/kill() after the child
process is reaped.

Previously, GChildWatchSource would call waitpid() already when adding
the source (g_child_watch_source_new()) and from the worker thread
(dispatch_unix_signals_unlocked()). That is racy:

- if a child watcher is attached and did not yet fire, you cannot call
  kill() on the PID without racing against the PID being reaped on the
  worker thread. That would then lead to ESRCH or even worse, killing
  the wrong process.

- if you g_source_destroy() the source that didn't fire yet, the user
  doesn't know whether the PID was reaped in the background. Any
  subsequent kill()/waitpid() may fail with ESRCH/ECHILD or even address
  the wrong process.

The race is most visible on Unix without pidfd support, because then the
process gets reaped on the worker thread or during g_child_watch_source_new().
But it's also with Windows and pidfd, because we would have waited for
the process in g_child_watch_check(), where other callbacks could fire
between reaping the process status and emitting the source's callback.

Fix all that by calling waitpid() right before dispatching the callback.
2023-05-18 11:26:33 +02:00
Thomas Haller
cdda194844 gmain: remove unnecessary initialization of *timeout in prepare() callbacks
Note that the prepare callback only has one caller, which pre-initializes
the timeout argument to -1. That may be an implementation detail and not
publicly promised, but it wouldn't make sense to do it any other way in
the caller.

Also, note that g_unix_signal_watch_prepare() and the UNIX branch of
g_child_watch_prepare() already relied on that.
2023-05-18 11:26:33 +02:00
Thomas Haller
30b5418608 gmain: remove unnecessary initialization of source_timeout in g_main_context_prepare_unlocked()
Note that the variable source_timeout is already initialized upon
definition, at the beginning of the block.

It's easy to see, that no code changes the variable between the variable
definition, and the place where it was initialized. It was thus
unnecessary.

It's not about dropping the unnecessary code (the compiler could do that
just fine too). It's that there is the other branch of the "if/else", where
the variable is also not initialized. But the other branch also requires
that the variable is in fact initialized to -1, because prepare()
callbacks are free not to explicitly set the output value. So both
branches require the variable to be initialized to -1, but only one of
them did. This poses unnecessary questions about whether anything is
wrong. Avoid that by dropping the redundant code.
2023-05-18 11:26:33 +02:00
Thomas Haller
a71b0c0461 gmain: simplify handling child watchers in dispatch_unix_signals_unlocked()
- if a child watch source has "using_pidfd", it is never linked in the
  unix_child_watches list. Drop that check.
- replace the deep nested if, with an early "continue" in the loop,
  if we detect there is nothing to do. It makes the code easier to
  read.
2023-05-18 11:26:33 +02:00
Thomas Haller
9315a211fa gmain: unify win/unix implementations for child watcher
Let's move the difference between the win/unix implementations closer to
where the difference is. Thereby, we easier see the two implementations
side by side. Splitting it at a higher layer makes the code harder to
read.

This is just a preparation for what comes next.
2023-05-18 11:26:33 +02:00
Thomas Haller
d1e558f492 gmain/tests: add unit test for g_child_watch_add() reaping processes without notifying
The test currently tests broken behavior. It will be fixed next.
2023-05-18 11:26:33 +02:00
Peter Eisenmann
ac4d1e2686 docs: unify "dynamic memory needed" explanations
All `_take` method constructors require dynamic memory, this unifies
theses explanation texts.
2023-05-16 11:27:45 +01:00
Peter Eisenmann
afdab4f493 gstring: add g_string_new_take
Adds a GString constructor that takes over ownership of an existing,
dynamically allocated, string.
2023-05-16 11:27:45 +01:00
hanhuihui
e2973c9a41 tests: Fix assertion failures when running fileutils test as root 2023-05-11 11:46:08 +01:00
hanhuihui
d5d5fb9b5f tests: Move a function to allow looking at CAP_DAC_OVERRIDE
This introduces no functional changes but allows `CAP_DAC_OVERRIDE` to
be used in a subsequent commit.
2023-05-11 11:45:36 +01:00
Philip Withnall
a14803d06d Merge branch 'fix-group-comment' into 'main'
gkeyfile: Fix group comment management

Closes #104 and #2927

See merge request GNOME/glib!3380
2023-05-10 09:24:41 +00:00
Philip Withnall
eee3885077 Merge branch 'gtk-plus' into 'main'
Rename GTK+ to GTK (mostly comments and documentation)

See merge request GNOME/glib!3429
2023-05-10 08:39:58 +00:00
Philip Withnall
329213f742 Merge branch 'main' into 'main'
gtestutils: Improve g_assert_cmpuint

Closes #2997

See merge request GNOME/glib!3424
2023-05-10 08:35:27 +00:00
Arnaud Rebillout
d6e57943ea Fix style-check for {gconvert,gutils,gobject}.c
Also take this chance to change a 'Glib' to 'GLib'.
2023-05-10 10:56:44 +07:00
Arnaud Rebillout
f722f11e57 Rename GTK+ to GTK (mostly comments and documentation)
GTK lost it's '+' suffix back in 2019, according to
<https://mail.gnome.org/archives/gtk-devel-list/2019-February/msg00000.html>

This commit can be re-generated with:

    git grep -l GTK+ \
    | grep -v -e ^NEWS -e ^glib/tests/collate.c \
    | xargs sed -i 's/GTK+/GTK/g'

Most of the changes are in comments and documentation.
2023-05-10 10:56:44 +07:00
Eric Blake
171bcd524a gtestutils: Style touchup
Use more modern styling to the code added in the previous patch:
- split 'label: stmt; stmt;' into multiple lines
- add default: label with g_assert_not_reached() [yes, it's a bit
  weird adding an assertion inside code that handles assertions, but
  we should be okay since g_assertion_message_* are not public
  functions and should only be used by our macros]
- use <inttypes.h> for shorter format strings

Note, however, that using uint64_t in gtestutils.h is not feasible,
since it would require adding an '#include <stdint.h>' with potential
unintended namespace pollution to older clients.

Signed-off-by: Eric Blake <eblake@redhat.com>
2023-05-09 09:29:16 -05:00
Eric Blake
2ab2ce57e6 gtestutils: Improve g_assert_cmpuint
While x86_64 has enough precision in long double to do a round trip
from guint64 to long double and back, this is platform-specific, and
is a disservice to users trying to debug failing unit tests on other
architectures where it loses precision for g_assert_cmp{int,uint,hex}.
See also https://bugzilla.gnome.org/show_bug.cgi?id=788385 which
mentions having to add casts to specifically silence the compiler on
platforms where the precision loss occurs.

Meanwhile, g_assert_cmpuint() does an unsigned comparison, but outputs
signed values if the comparison fails, which is confusing.

Fix both issues by introducing a new g_assertion_message_cmpint()
function with a new 'u' numtype.  For backwards compatibility, the
macros still call into the older g_assertion_message_cmpnum() when not
targetting 2.78, and that function still works when passed 'i' and 'x'
types even though code compiled for 2.78 and later will never invoke
it with numtype anything other than 'f'.  Note that g_assert_cmpmem
can also take advantage of the new code, even though in practice,
comparison between two size_t values representing array lengths that
can actually be compiled is unlikely to have ever hit the precision
loss.  The macros in signals.c test code does not have to worry about
versioning, since it is not part of the glib library proper.

Closes #2997
Signed-off-by: Eric Blake <eblake@redhat.com>
2023-05-09 08:28:09 -05:00
Marco Trevisan (Treviño)
e54f27322d gdataset: Use atomic compare and exchange full to set pointers
In case first exchange failed we can avoid repeating the pointer get
operation given that exchange full can provide us the old value.
2023-05-09 15:04:36 +02:00
Philip Withnall
63e1e83c33 Merge branch 'wip/p3732/timeout-seconds-once' into 'main'
add g_timeout_add_seconds_once

See merge request GNOME/glib!3383
2023-05-09 12:50:39 +00:00
Philip Withnall
85af9b73c9 tests: Zero the rlimit in standalone tests which are expected to abort
This should hopefully stop the kernel spending a lot of memory and disk
bandwidth creating coredumps for them unnecessarily, which slows down
the rest of the tests and generally wastes resources.

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

Fixes: #2939
2023-05-02 12:53:13 +01:00
Peter Eisenmann
467b917719 gtimeout: use helper for seconds_full variant
Use timeout_add_full() helper to deduplicate code
2023-05-02 13:42:54 +02:00
Peter Eisenmann
3abf23b2a7 add g_timeout_add_seconds_once
Add a new call combing behaviors of g_timeout_add_seconds and
g_timeout_add_once.
2023-05-02 13:42:54 +02:00
Philip Withnall
e8c1bdbf4f tests: Add copyright/licensing header to assert-msg-test
This is put together through git archaeology:
```
git log -- glib/tests/assert-msg-test.c tests/assert-msg-test.c
```

The following commits were too trivial to have meaningful copyright:
 - 8e59d8602ca5921d78245f5d2b405b517a5e7cf9
 - 44c004c84e9080040ff4e0e90b322dda0561cf85
 - 207b8cb8a50d68e207d28b59e588311a5cbd9772
 - a1bee97d4f093db01dee834bf3292eabd5b13d17

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

Helps: #1415
2023-05-02 11:03:49 +01:00
Philip Withnall
a1bee97d4f tests: Reformat assert-msg-test.c to follow coding style
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-05-02 11:03:00 +01:00
Arnaud Rebillout
d6faa002a5 gtestutils: Fix section marker in documentation
Apparently 3 hashes is too many, and as a result this line is not
displayed properly in the HTML documentation. 2 hashes seems to be
just right, as can be seen with:

  $ git grep '^ \* ##' | wc -l
  65
  $ git grep '^ \* ###' | wc -l
  1
2023-05-02 13:21:17 +07:00
Philip Withnall
e7398198c5 gthreadpool: Document that g_thread_pool_new() will spawn a thread
Otherwise it might look like it would only start spawning threads when
jobs are enqueued.

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

Helps: #2958
2023-04-28 10:52:22 +01:00
Philip Withnall
7adca698d8 gthreadpool: Clarify that item_free_func is not normally called
If you’re only quickly looking at the API signature, it looks like
`item_free_func` will be called for all items enqueued to the thread
pool.

As it happens, it’s actually only called for the items which are still
enqueued when the thread pool is destroyed. The user’s `GFunc` is
responsible for freeing items which are successfully dequeued and
processed during the lifetime of the thread pool.

That’s a bit of a gotcha, so document it more explicitly.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-04-27 15:48:07 +01:00
Marco Trevisan
59a2235e9d Merge branch '98-current-path' into 'main'
gfileutils: Fix potential integer overflow in g_get_current_dir()

Closes #98

See merge request GNOME/glib!3375
2023-04-27 09:18:07 +00:00
Philip Withnall
73bf66dd2a Merge branch 'unix-fd-source-docs' into 'main'
glib-unix: Improve documentation for g_unix_fd_source_new()

See merge request GNOME/glib!3396
2023-04-26 16:11:42 +00:00
Peter Kjellerstedt
edd1e47f10 Avoid having g_futex_simple() inadvertently modify errno
If both __NR_futex and __NR_futex_time64 are defined, g_futex_simple()
will first call futex_time64(). If that fails with ENOSYS, then
futex_time() is called instead. However, errno was not saved and
restored in this case, which would result in g_futex_simple()
returning with errno set to ENOSYS, even if futex_time() succeeded.
2023-04-26 02:08:46 +02:00
Philip Withnall
df9d3818ac glib-unix: Improve documentation for g_unix_fd_source_new()
In particular, document the type of the callback function.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-04-25 15:30:45 +01:00
Philip Withnall
c176fcf2eb Merge branch 'doc-indentation' into 'main'
Fix small issues in docs

See merge request GNOME/glib!3393
2023-04-24 12:58:08 +00:00
Philip Withnall
d3e7b9e17c Merge branch 'context-checks' into 'main'
gmain: More explicitly document g_main_context_release() prereqs

See merge request GNOME/glib!3314
2023-04-24 12:52:51 +00:00
Philip Withnall
3926af723a gmain: Add precondition assertions to g_main_context_release()
As with the previous commit.

The logic has to be a little contorted here to avoid leaving the context
locked after emitting the critical warning. Execution does (and should)
continue after a critical warning by default, so we should do our best
to recover.

Inspired by https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3302.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-04-24 13:02:36 +01:00
Philip Withnall
44616ebafd gmain: More explicitly document g_main_context_release() prereqs
The documentation was fairly clear before, but we can make it clearer:
it’s a programming error to call `g_main_context_release()` if you have
not received a true return value from `g_main_context_acquire()` before.

Inspired by https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3302.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-04-24 12:49:23 +01:00
Emmanuele Bassi
e2e93056f2 Add init macros for refcounting types
We need a way to initialise refcounted types placed in static storage,
or on the stack. Using proper macros avoids knowing the magic constant
used for grefcount and gatomicrefcount.
2023-04-24 12:48:32 +01:00
Arjan Molenaar
84d8d5647e
Fix small issues in docs
This will make converting the docs to another format
(say, rst) somewhat easier.
2023-04-24 13:18:33 +02:00
Philip Withnall
28b1b9d9c3 Merge branch 'gmain-less-locks' into 'main'
gmain: Avoid some lock/unlock dance during g_main_context_iterate

See merge request GNOME/glib!3235
2023-04-24 10:29:41 +00:00
Philip Withnall
cba49eb6a1 Merge branch 'mutex-tests-more' into 'main'
glib/tests/[rec-]mutex: Allow each thread to work

See merge request GNOME/glib!3389
2023-04-24 10:25:05 +00:00
Arnaud Rebillout
edb85b99f6 gvariant: Fix doc for g_variant_new_object_path
The parameter is named 'object_path', not 'string'.
2023-04-21 21:56:50 +07:00
Marco Trevisan (Treviño)
3aeef671d0 glib/tests/[rec-]mutex: Allow each thread to work
In non-perf mode, we were making only one thread to win the race to increase
the value, but since we're launching more threads anyways let's allow
more of them to have a chance to do something, to make the test more
valuable, even if it's still quick enough.
2023-04-20 17:01:05 +02:00
Gaël Bonithon
86b4b0453e gkeyfile: Fix group comment management
This removes the `comment` member of the GKeyFileGroup structure, which
seemed intended to distinguish comments just above a group from comments
above them, separated by one or more blank lines. Indeed:
* This does not seem to match any specification in the documentation,
  where blank lines and lines starting with `#` are indiscriminately
  considered comments. In particular, no distinction is made between the
  comment above the first group and the comment at the beginning of the
  file.
* This distinction was only half implemented, resulting in confusion
  between comment above a group and comment at the end of the preceding
  group.

Instead, the same logic is used for groups as for keys: the comment
above a group is simply the sequence of key-value pairs of the preceding
group where the key is null, starting from the bottom.

The addition of a blank line above groups when writing, involved in
bugs #104 and #2927, is kept, but:
* It is now added as a comment as soon as the group is added (since a
  blank line is considered a comment), so that
  `g_key_file_get_comment()` returns the correct result right away.
* It is always added if comments are not kept.
* Otherwise it is only added if the group is newly created (not present
  in the original data), in order to really keep comments (existing and
  not existing).

Closes: #104, #2927
2023-04-20 16:59:04 +02:00
Gaël Bonithon
f74589f530 gkeyfile: Replace g_slice_*() with g_new*()/g_free_sized() 2023-04-20 16:52:19 +02:00
Marco Trevisan (Treviño)
164f199afc gmacros: Do not redefine NULL on C++
Even though having NULL as nullptr should be the standard for newer C++
versions, it may break some headers, so let's not touch it for now.

Closes: https://gitlab.gnome.org/GNOME/glib/-/issues/2973
2023-04-19 23:03:43 +02:00
Marco Trevisan (Treviño)
7d5242d34e gmain: Do not dead-look if calling g_main_loop_run from a GSource
This should fail and warn but not leaving the context acquired and
locked.

Add tests.
2023-04-19 22:52:43 +02:00
Marco Trevisan (Treviño)
f8e440335c gmain: Avoid locking dance in g_main_loop_run()
We do it during initialization, this may be if necessary handled by GCond in
case we need to acquire the lock.
2023-04-19 22:04:13 +02:00