Commit Graph

31956 Commits

Author SHA1 Message Date
Thomas Haller
7a1dfea458 gdataset: check for valid key before taking lock
In g_dataset_id_remove_no_notify() and g_dataset_id_get_data(), check
first the argument before taking a lock.

Note that this doesn't matter much for g_dataset_id_get_data(). We could
leave the check as it was or drop it altogether. That is because
g_dataset_id_get_data() calls g_datalist_id_get_data(), which is fine
with a zero key_id. Also, passing a zero key is not something that would
somebody to normally, so the check is not necessary at all. I leave the
check for consistency with g_dataset_id_remove_no_notify() and do the
change for the same reason as there.

However, the check for key matters for g_dataset_id_remove_no_notify().
That function calls g_data_set_internal(), which must not be called with
zero key.

At this point, it seems a code smell to perform the check after taking
the lock. Something you notice every time that you look at it, and while
technically no problem, it is ugly. Hence the change.
2025-05-20 15:37:36 +02:00
Thomas Haller
8a273a7a3e gdataset: add assertions that we never add a zero key
All public API already ensures that this cannot ever happen.

The assertion is only for the reviewer and ease of mind.
2025-05-20 15:37:36 +02:00
Thomas Haller
8df7da0cc2 gdataset: drop "Since" annotation from internal g_datalist_id_update_atomic()
This is internal API, and is only used via GLIB_PRIVATE_CALL(). Such
private API is not stable. That is, you must use the same version of
e.g. libgobject and libglib.

In that case, the "Since" annotation makes no sense.
2025-05-20 15:37:36 +02:00
Thomas Haller
75ea33be11 gdataset: check arguments of g_datalist_id_update_atomic()
g_datalist_id_update_atomic() is internal API only, and all callers
(supposedly) get this right.

Add assertions for these requirements.
2025-05-20 15:37:36 +02:00
Axel Karjalainen
8153cd8551 gmessages: add SYSLOG_IDENTIFIER to structured logs using select functions
Fixes #3656
2025-05-20 13:19:07 +01:00
Philip Withnall
c697f682a6 Merge branch 'wip/jtojnar/fix-macros-doc-syntax' into 'main'
docs: Fix formatting of definition lists

See merge request GNOME/glib!4638
2025-05-20 09:49:01 +00:00
Michael Catanzaro
7c7d7c6c78 Merge branch 'wip/finaw/search-prefer-early-tokens' into 'main'
gdesktopappinfo: Prefer matches that occur earlier in the match string

See merge request GNOME/glib!4369
2025-05-19 10:51:28 -05:00
Luca Bacci
01454b823b Rework Windows implementation of g_getenv()
Fixes the following issues:

1. g_getenv didn't properly differentiate between non-exisiting variables
   and variables with empty values. This happened bacause we didn't call
   SetLastError (0) before GetEnvironmentVariable (as with most APIs,
   GetEnvironmentVariable doesn't set the error code to 0 on success).

2. g_getenv called GetEnvironmentVariable, g_free, and GetLastError in sequence;
   the call to g_free could change the last error code.

3. We can use the looping pattern to call APIs that return the required buffer
   size. The looping pattern makes the two phases (get size and retrieve value)
   appear as just one call. It's also important for values that can change at any
   time like environment variables [1].

[1] https://devblogs.microsoft.com/oldnewthing/20220302-00/?p=106303
2025-05-19 10:27:50 +02:00
Fina Wilke
b36e646b54 gdesktopappinfo: Prefer matches that occur earlier in the match string
Some apps names or keywords contain multiple words. For example 'LibreOffice
Calc' contains the word 'Calc'. This is rightfully detected as a prefix match,
however generally it is expected that searching for 'calc' would consistantly
return 'Calculator' in first position, instead of ranking them equally.

We now prioritise tokens that would otherwise rank equal based on where they
occur in the string, giving earlier occurences precedence.
2025-05-17 15:43:13 +02:00
Jan Tojnar
ec19c0c6b5 docs: Fix formatting of definition lists
Definitions in definition lists in most markdown implementations,
including the GitLab one, support laziness for the definition text
(https://spec.commonmark.org/0.31.2/#lazy-continuation-line).
As a result, each defined term would be collapsed into preceding definition.
To fix this, definitions need to be separated by blank line.
2025-05-16 21:43:34 +02:00
Philip Withnall
39b5613e73 Merge branch 'glib-merge-request-main' into 'main'
gmain: Mark several GSourceFuncs members as nullable

Closes #3686

See merge request GNOME/glib!4636
2025-05-14 17:03:16 +00:00
Michael Catanzaro
290d90473d Merge branch 'gzlib-docs' into 'main'
gzlibcompressor: Convert docs to gi-docgen linking syntax

See merge request GNOME/glib!4633
2025-05-14 11:35:03 -05:00
BZZZZ
4b938fc34b gmain: Mark several GSourceFuncs members as nullable
Closes: https://gitlab.gnome.org/GNOME/glib/-/issues/3686
2025-05-14 17:30:50 +01:00
Philip Withnall
696c03ea4d Merge branch 'key_file_non_existent_key' into 'main'
docs: Non-existent group name/key passed to g_key_file_set_comment

Closes #490

See merge request GNOME/glib!4635
2025-05-14 16:22:59 +00:00
marklkram
6d1a820340 docs: Non-existent group name/key passed to g_key_file_set_comment
Specifies that passing a non-existent group name/key to g_key_file_set_comment returns an error.

Closes #490
2025-05-14 11:42:53 -04:00
Philip Withnall
843779903c Merge branch 'socket_address_native_size' into 'main'
docs: Description of return value of get_native_size

Closes #2377

See merge request GNOME/glib!4625
2025-05-14 10:44:20 +00:00
Philip Withnall
13a73f9008 gzlibcompressor: Convert docs to gi-docgen linking syntax
Improve formatting while I’m there, and try and ensure all the docs in
these two files matches the
[guidelines](https://developer.gnome.org/documentation/guidelines/devel-docs.html#writing-api-references).

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

Helps: #3250
2025-05-14 11:38:05 +01:00
Philip Withnall
997c41b648 Merge branch 'wip/otte/zlib-os' into 'main'
gio: Add GZlibCompressor:os property

Closes #3663

See merge request GNOME/glib!4597
2025-05-14 10:20:01 +00:00
Philip Withnall
e5fd9c42ff Merge branch 'wip/otte/win32-version' into 'main'
win32: Only print one OS version

See merge request GNOME/glib!4632
2025-05-14 09:20:09 +00:00
Benjamin Otte
4cc75b144c win32: Only print one OS version
Exit the version-checking loop after the first successful check instead
of trying again.
2025-05-14 10:22:54 +02:00
Philip Withnall
d6beb02518 Merge branch 'de-translation' into 'main'
Update German translation

See merge request GNOME/glib!4631
2025-05-13 12:06:20 +00:00
Philip Withnall
cb4d8e02e2 Merge branch 'zbrown/galloc-docs' into 'main'
gallocator: mark as deprecated

See merge request GNOME/glib!4621
2025-05-13 12:00:50 +00:00
Philipp Kiemle
ff3b7f0600 Update German translation 2025-05-12 18:18:43 +02:00
Benjamin Otte
f5b0415539 gio: Add GZlibCompressor:os property
The use case for exposing this field is GTK wanting reproducible
encoding output across different OSes.

I decided to expose the OS as an integer because zlib uses an int
in its header and does not make its OS codes available as a custom
type in its API.

I also decided to limit it to values between 0 and 255 because zlib
encodes the OS as a single byte.

Test included.

Fixes: #3663
2025-05-12 09:07:57 +02:00
Michael Catanzaro
314d63fcf7 Merge branch 'simultaneous-main-context-source-destruction' into 'main'
glib: add a proper weak-ref-like relationship between GSource and GMainContext

Closes #803

See merge request GNOME/glib!2187
2025-05-11 09:31:21 -05:00
marklkram
c0c0e212d9 docs: Description of return value of get_native_size
Specified that if the user passes an invalid address, the function returns -1.

closes #2377
2025-05-10 22:28:18 -04:00
Matthew Waters
a235350bc0 source: minimise direct access to source->context
Doing so can cause a use-after-free as the main context referenced can
be destroyed by another thread freeing the GMainContext.

Fixes https://gitlab.gnome.org/GNOME/glib/-/issues/803
2025-05-11 10:40:10 +10:00
Matthew Waters
9251e99cdc glib: add a proper weak-ref-like relationship between GSource and GMainContext
Like the GWeakRef's in GObject, there is a global lock that is consulted
whenever g_main_context_unref() or g_source_destroy() or
g_source_unref() is called to retrieve a reference to the associated
GMainContext.

There are a number of actual races that are fixed by this change.
1. Racing GSource destruction with g_main_context_unref() is solved
   by holding the global source_weak_locations lock while setting
   source->context = NULL and while g_source_destroy() attempts to
   retrieve source->context;
2. Same race as 1. but inside g_source_unref()
3. Theoretical race of double freeing the contents of
   context->pending_dispatches if both g_source_destroy() and
   g_main_context_unref() both free resources inside g_main_context_unref().

A couple of implementation notes:
1. Unlocking source_weak_locations too early in g_main_context_unref()
   (before g_source_destroy_internal() is called) may have a race of the
   G_HOOK_FLAG_ACTIVE state of the source and cause a leak of the source.
   This is why source_weak_locations is also held over the calls to
   g_source_destroy_internal() in g_main_context_unref().  So that
   either g_main_context_unref() or g_source_destroy() (but only 1) has
   the chance to free the resources associated with the GMainContext.
2. g_main_context_unref() now needs to be more of a dispose()
   implementation as it can be called multiple times with losing the
   last ref.

Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/803
2025-05-11 10:40:10 +10:00
Michael Catanzaro
93073ec317 Merge branch 'th/revert-weakref-change' into 'main'
Revert "Merge branch 'th/gobj-doc-weakref' into 'main'"

Closes #3684

See merge request GNOME/glib!4629
2025-05-09 14:05:45 -05:00
Thomas Haller
3cf6d22f76 Revert "Merge branch 'th/gobj-doc-weakref' into 'main'"
This change appears to cause crashes. Revert for now, to investigate why
exactly that happens.

This reverts commit 22f57fce78, reversing
changes made to 549a966b46.

Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/3684
See-also: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/4584#note_2436512
See-also: https://gitlab.gnome.org/GNOME/gnome-builder/-/issues/2324
2025-05-09 20:34:18 +02:00
Zander Brown
cff415f221 gallocator: mark as deprecated
These show up in various places in docgen-based docs, including in
results for fairly common searches (such as ‘alloc’). Whilst people
should have been scared off by these being undocumented, the red
deprecated tags make it clear these shouldn't be touched.
2025-05-09 15:48:40 +01:00
Michael Catanzaro
3674b4a805 Merge branch 'th/gsignalgroup-dispose' into 'main'
[th/gsignalgroup-dispose] gsignalgroup: make GSignalGroup.dispose() a bit more reentrant

See merge request GNOME/glib!4627
2025-05-08 19:51:22 +00:00
Thomas Haller
e805e8ba39 gsignalgroup: make GSignalGroup.dispose() a bit more reentrant
dispose() would previously set the "handlers" pointer to NULL. But
dispose() also calls g_signal_group_gc_handlers(), which requires this
pointer to be not NULL.

This means, dispose() could not be called multiple times. Which is a
good practice to allow, because g_object_run_dispose() and object
resurrection both requires that dispose() can be called more than once
per object.

Fix that problem, by leaving the array until finalize().

Fixes: dd43471f60 ('gobject: add GSignalGroup')
2025-05-08 19:50:46 +02:00
Michael Catanzaro
47edd6aa30 Merge branch 'th/weak-ref-release-one-by-one' into 'main'
[th/weak-ref-release-one-by-one] gobject: invoke g_object_weak_ref() one-by-one during destruction

Closes #1002

See merge request GNOME/glib!4584
2025-05-08 13:12:51 +00:00
Thomas Haller
0be672e1e0 gobject: preserve weak notifications registered during dispose
We call g_object_weak_release_all() at two places.

Once right before finalize(). At this point, the object is definitely
going to be destroyed, and the user must no longer resurrect it or
subscribe new weak notifications. In that case, we really want to
notify/release all weak notifications.

However, we also call it from g_object_real_dispose(). During dispose,
the API allows the user to resurrect an object. Granted, that is
probably not something anybody should do, but GObject makes a reasonable
attempt to support that.

A possible place to resurrect (and subscribe new weak notifications) is
when GObject calls g_object_real_dispose().

  static void
  g_object_real_dispose (GObject *object)
  {
    g_signal_handlers_destroy (object);

    /* GWeakNotify and GClosure can call into user code */
    g_object_weak_release_all (object);
    closure_array_destroy_all (object);
  }

But previously, g_object_weak_release_all() would continue iterating
until there are no more weak notifications left. So while the user can
take a strong reference and resurrect the object, their attempts to
register new weak notifications are thwarted.

Instead, when the loop in g_object_weak_release_all() starts, remember
the initial number of weak notifications, and don't release more than
that. Note that WeakRefStack preserves the order of entries, so by
maintaining the "remaining_to_notify" counter we know when to stop.

Note that this brings also an earlier behavior back, where we would call

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

This would take out the entire WeakRefStack at once and notify the weak
notifications registered at the time. But subsequent registrations would
not be released/notified yet.
2025-05-07 21:29:37 +00:00
Thomas Haller
1312ec6d0b gobject: grow buffers for weak notifications exponentially
It seems bad style, to use a naive realloc() +1 increment each time a new
element gets added. Instead, remember the allocation size and double the
buffer size on buffer grow. This way we get linear amortized runtime
complexity for buffer growth.

Well, WeakRefStack uses a flat array for tracking the entires. We anyway
need to search and memmove() the entries and are thus O(n) anyway. We do
that, because it allows for relatively simple code while being memory
efficient. Also, we do expect only a reasonably small number of weak
notifications in the first place.

I still think it makes sense to avoid the O(n) number of realloc() calls
on top of that. Note that we do this while holding the (per-object)
lock. It's one thing to do a linear search or a memmove(). It's another
to do a (more expensive) realloc().

Also, shrink the buffer during g_object_weak_unref() to get rid of
excess memory.

Also, note that the initial allocation only allocates space for the
first item. I think that makes sense, because I expect that many objects
will only get a single weak notification registered. So this allocation
should not yet have excess memory allocated.

Also, note that the "flexible" array member WeakRefStack.weak_refs has a
length of 1. Maybe we should use C99 flexible array members ([]) or the
pre-C99 workaround ([0]). Anyway. Previously, we would always allocate
space for that extra one tuple, but never use it. Fix that too.
2025-05-07 21:29:37 +00:00
Thomas Haller
dadb759c65 gobject: invoke g_object_weak_ref() one-by-one during destruction
Previously, at two places (in g_object_real_dispose() and shortly before
finalize()), we would call

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

This clears @quark_weak_notifies at once and then invokes all
notifications.

This means, if you were inside a notification callback and called
g_object_weak_unref() on the object for *another* weak-reference, then
an exception failed:

  GLib-GObject-FATAL-CRITICAL: g_object_weak_unref_cb: couldn't find weak ref 0x401320(0x16b9fe0)

Granted, maybe inside a GWeakNotify you shouldn't call much of anything
on where_the_object_was. However, unregistering things (like calling
g_object_weak_unref()) should still reasonably work.

Instead, now remove each weak notification one by one and invoke it.

As we now invoke the callbacks in a loop, if a callee registers a new
callback, then that one gets unregistered right away too.  Previously,
we would during g_object_real_dispose() only notify the notifications
that were present when the loop starts. This is similar to what happens
in closure_array_destroy_all(). This is a change in behavior, but it
will be fixed in a separate follow-up commit.

https://gitlab.gnome.org/GNOME/glib/-/issues/1002
2025-05-07 21:29:37 +00:00
Thomas Haller
af508f91b1 gobject: add internal WeakRefTuple helper structure
This is already useful and will be more useful later.
2025-05-07 21:29:37 +00:00
Thomas Haller
d2e08b7dfe gobject: preserve order of weak notifications in g_object_weak_unref()
g_object_weak_unref() would have done a fast-removal of the entry, which
messes up the order of the weak notifications.

During destruction of the object we emit the weak notifications. They
are emitted in the order in which they were registered (FIFO). Except,
when a g_object_weak_unref() messes up the order. Avoid that and
preserve the order.

Now, do a memmove(), which is O(n). But note that we already track weak
references in a flat array that requires a O(n) linear search. Thus,
g_object_weak_unref() was already O(n) and that didn't change. More
importantly, users are well advised to limit themselves to a reasonably
small number of weak notifications. And for small n, the linear search
and the memmove() is an efficient solution.
2025-05-07 21:29:37 +00:00
Thomas Haller
7743c7aaa2 gobject/tests: add test for g_object_weak_ref() 2025-05-07 21:29:37 +00:00
marklkram
fdd41f9b65 docs: Description of return value of get_native_size
Specified that if the user passes an address that is not of type AF_INET or AF_INET6 ,
the function returns -1.

closes #2377
2025-05-07 15:58:50 -04:00
Michael Catanzaro
3548c4ae53 Merge branch 'th/gobject-no-object-locks-pt1-notify' into 'main'
[th/gobject-no-object-locks-pt1-notify] use `g_datalist_id_update_atomic()` instead of OPTIONAL_BIT_LOCK_NOTIFY

See merge request GNOME/glib!4185
2025-05-06 21:24:32 +00:00
Michael Catanzaro
22f57fce78 Merge branch 'th/gobj-doc-weakref' into 'main'
[th/gobj-doc-weakref] clear #GWeakRef earlier in g_object_run_dispose() and reword docs about #GWeakRef

See merge request GNOME/glib!4586
2025-05-06 21:23:52 +00:00
Philip Withnall
549a966b46 Merge branch 'add-g-log-get-always-fatal' into 'main'
gmessages: Add 'g_log_get_always_fatal()' for use in custom log writers

Closes #2544

See merge request GNOME/glib!4546
2025-05-06 12:15:08 +00:00
Philip Withnall
a4b4fb2bea Merge branch 'fix-garray' into 'main'
garray: Fix annotations

See merge request GNOME/glib!4601
2025-05-06 12:02:30 +00:00
Philip Withnall
87f69dd5d1 Merge branch 'g_slist_doc_update' into 'main'
gslist: Improve documentation for append / prepend / insert methods

See merge request GNOME/glib!4619
2025-05-06 12:02:02 +00:00
Philip Withnall
634085c39a Merge branch 'tzset' into 'main'
gdate: Call tzset before localtime_r

Closes gnome-control-center#3363

See merge request GNOME/glib!4617
2025-05-06 11:21:15 +00:00
5dma
fef650ca86 gslist: Improve documentation for append / prepend / insert methods
The new descriptions are sourced from the parallel methods g_list_append,
g_list_prepend and g_list_insert.

Refer: https://discourse.gnome.org/t/return-value-of-g-slist-append/27921
2025-05-06 12:01:20 +01:00
Philip Withnall
c0ada13e0b docs: Fix broken links to ‘Running GLib Applications’ docs page
Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3250
2025-05-06 11:58:50 +01:00
sid
13a6f0ee51 gmessages: Add 'g_log_get_always_fatal()' for use in custom log writers
Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/2544
2025-05-06 11:58:47 +01:00