Commit Graph

212 Commits

Author SHA1 Message Date
gwillems
d6e0cf9884 gobject: fix broken links to parameters and signals naming rules 2024-05-21 22:32:20 +00:00
Philip Withnall
a60e6bedae docs: Document that signal connection functions cannot fail
The documentation previously implied that they could. That’s not really
true though: they can only fail if preconditions fail, i.e. they’re
passed invalid input. That’s a programmer error, which is not something
we want to encourage people to check for at runtime (e.g. by dynamically
checking for a 0 return value).

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2024-03-07 13:12:07 +00:00
Alex Richardson
9769cd0d24 gsignal.c: drop an optimization that is undefined behaviour
Comparing reallocated pointers is UB, but this happens to work for now
on most compilers. However, for CHERI systems if g_bsearch_array_insert()
reallocs in-place then the new `hlbsa` pointer may have larger bounds
than `o` and using the old pointer with the smaller bounds can result
in a bounds error. I don't think this code is performance critical, so
removing the optimization and inserting unconditionally should be fine.

Currently, this realloc() UB rarely causes issues, but newer versions of
GCC with _FORTIFY_SOURCE=3 might also be able to observe the valid
memory range (assuming sufficient inlining).
See https://developers.redhat.com/articles/2022/09/17/gccs-new-fortification-level
2024-01-05 16:46:29 -08:00
Philip Withnall
266f570f3c gsignal: Add missing (scope forever) annotations
Signals (as opposed to signal connections) can never be unregistered, so
these closures have to be around forever.

Fixes some g-ir-scanner warnings.

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

Helps: #3037
2023-11-29 12:04:59 +00:00
Alex Richardson
a121b1bf71 Change GType print placeholders to G_GUINTPTR_FORMAT
GType is not guaranteed to be the same as gsize, but it is representable
as guintptr.
2023-10-25 12:01:02 +01:00
Emmanuele Bassi
0f8aaee2bf Add missing annotation separator 2023-10-23 10:25:30 +01:00
Matthias Clasen
95717eacea docs: Move the signal SECTION
Move the content to the new signals.md file.

Helps: #3037
2023-10-16 23:34:35 +01:00
Marco Trevisan (Treviño)
2e1ceb9563 gsignals: Do not zero two times the instance GValue's GType
We already use g_new0 to create such arrays and nothing writes in the first
pointer till this point, so no need to zero it again.
2023-05-30 13:52:08 +01:00
Marco Trevisan (Treviño)
76ed55f3a7 gsignal: Use static allocation if we've just few emission hooks
Normally we don't really have emission hooks around, so try to allocate
only tiny array to contain a few of them and in case we exceed that limit,
we go back to use allocated ones.
2023-05-30 13:52:08 +01:00
Marco Trevisan (Treviño)
bfc599b8a2 gsignal: Reduce lock/unlock operations when calling signal_emit_unlocked_R
We used to call this function as unlocked, with a node value that
could be invalid at the point of the call, so let's ensure that when
we call such function it's defined, and then reduce the access to the
signal node members when we're unlocked or after a lock/unlock operation
that may have changed it.

As per this, add more tests handling multiple signal hooks cases that we
did not cover before.
2023-05-30 13:52:08 +01:00
Marco Trevisan (Treviño)
e5ee6e141d gsignal: Cleanup g_signal_emitv splitting it in locked and unlocked paths
It just makes code easier to maintain and more clearly scoped.
2023-05-30 13:52:08 +01:00
Marco Trevisan (Treviño)
f011910395 gsignal: Do not try to access to node members when unlocked
In g_signal_emit_valist() we used to access to param types array and
n_params values after unlocking the mutex, and this might have lead to
making such values unreliable for the current call.

So let's keep them around until we're done with the function call
2023-05-30 13:52:07 +01:00
Marco Trevisan (Treviño)
2368187eb9 gsignal: Avoid possible race in g_signal_emit_by_name
Since we're locking and unlocking once we've found the signal ID, we
might have performed calls to g_signal_emit_valist() with a signal id
that was already been removed, and thus failing later.

This is not really an issue as inside g_signal_emit_valist() we were
re-checking for the signal id, but we can make this more reliable so
that the first thread that acquires the lock can also be sure to emit.
2023-05-30 13:52:07 +01:00
Przemyslaw Gorszkowski
b264585f3c gsignal: Support G_SIGNAL_MATCH_ID in g_signal_handlers_block/unblock/disconnect_matched()
Calling g_signal_handlers_block/unblock/disconnect_matched with only G_SIGNAL_MATCH_ID
do not match any handlers and return 0.

Fixes: #2980

Signed-off-by: Przemyslaw Gorszkowski <pgorszkowski@igalia.com>
2023-04-14 15:27:11 +01:00
Philip Withnall
2d8e38c00d gsignal: Clarify documentation for GSignalMatchType matching
The use of ‘OR’ in the existing documentation suggests that the matching
is disjunctive, but it’s actually conjunctive. Clarify that in the
documentation and add a test.

Spotted while reviewing
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3376.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-04-14 13:55:20 +01:00
Philip Withnall
3df45aba20 gsignal: Document that floating GClosures are sunk
It’s not clear about that otherwise.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-12-02 11:59:43 +00:00
Marco Trevisan
22f0522c10 Merge branch 'mcatanzaro/gtype-criticals' into 'main'
Replace most GObject warnings with criticals

See merge request GNOME/glib!2852
2022-09-19 14:35:12 +00:00
Tom Levy
eeaef8b950 docs: Improve wording in documentation of g_unix_signal_source_new()
- Insert missing word "from".

- Remove space between function name and "()" so syntax highlighting
  can recognise it as a function.

- Avoid "you"/"your" when discussing the reentrancy issues of regular
  UNIX signal handlers, because it gives the false impression that
  these issues are applicable to g_unix_signal_source_new().

Unrelated:

- Fix missing space in documentation of g_signal_new_class_handler().
2022-09-16 08:25:46 +00:00
Michael Catanzaro
0ffe86a1f7 Replace most GObject warnings with criticals
All of these warnings indicate programmer error, so critical is most
appropriate here.

Exceptions: deprecation warnings are just warnings. Also, warnings that
are worded with uncertainty can remain warnings rather than criticals.
2022-08-09 13:18:47 -05:00
Marco Trevisan (Treviño)
42d52033db gsignal: Do not try to lookup the signal id and work on unlocked node
In g_signal_parse_name we were looking up for the signal from the name
keeping the mutex locked, but we then retrieved and checked the node
data without keeping the lock, so with another thread potentially
changing that.
2022-07-15 16:09:14 +02:00
Marco Trevisan (Treviño)
ae14f3219a gsignal: Perform signal unlocked handlers block, unblock and disconnect ops
We used to perform unneeded lock/unlock dances to perform block, unblock
and disconnect actions, and these were potentially unsafe because we
might have looped in data that could be potentially be changed by other
threads.

We could have also done the same by saving the handlers ids in a
temporary array and eventually remove them, but I don't see a reason for
that since we can just keep all locked without the risk of creating
deadlocks.

Coverity CID: #1474757, #1474771, #1474429
2022-07-15 16:05:35 +02:00
Philip Withnall
41691cc4c8 Merge branch 'more-spdx' into 'main'
Add more SPDX license headers

See merge request GNOME/glib!2706
2022-07-05 11:06:49 +00:00
Philip Withnall
95d548ab03 gsignal: Change signal instance ref semantics under static analysis
As with commit 0932f71460, which did this for refs/unrefs of the
object in `g_object_notify()`, we need to do a similar thing for
refs/unrefs of the instance with `g_signal_emit()`, for all the same
reasons.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-06-21 13:27:52 +01:00
Matthias Clasen
8b328f1d6d Keep a separate flag for notify handlers
We currently keep a flag for whether an object has
ever had any signal handlers. But even if it had signal
handlers, it may not have any notify handlers. Keep that
information separately, so we can speed up property setting.
2022-06-06 14:25:41 -04:00
Philip Withnall
26409f19cd Add SPDX license headers for LGPL-2.1-or-later to various files
These have all been added manually, as I’ve finished all the files which
I can automatically detect.

All the license headers in this commit are for LGPL-2.1-or-later, and
all have been double-checked against the license paragraph in the file
header.

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

Helps: #1415
2022-06-01 12:44:23 +01:00
Gabor Karsay
7e64004db0 docs: mark macros, flags, enums with percent sign 2022-03-04 16:21:55 +00:00
Loic Le Page
208593acc6 Fix redefinition of local variable and non-initialized variable in gobject/gsignal.c 2022-02-18 10:49:00 +01:00
Alexander Schwinn
28d833a075 Make clear in doc that signals are emitted synchroniously 2022-01-18 08:53:20 +00:00
Nishal Kulkarni
34cf69ef17 gsignal: Use new g_newa0() function
Replace old `g_alloca()` and `memset()` with `g_newa0()`
2021-11-26 12:24:23 +00:00
DarkTrick
3c459b7fd6 doc: Explicitly said, that no null term. is needed 2021-09-06 11:23:14 +00:00
Philip Withnall
f10101b909 gobject: Use g_memdup2() instead of g_memdup() in obvious places
Convert all the call sites which use `g_memdup()`’s length argument
trivially (for example, by passing a `sizeof()`), so that they use
`g_memdup2()` instead.

In almost all of these cases the use of `g_memdup()` would not have
caused problems, but it will soon be deprecated, so best port away from
it.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
Helps: #2319
2021-02-04 14:13:21 +00:00
Thomas Haller
8416211231 gsignal: use g_clear_signal_handler() macro to implement g_clear_signal_handler() function
We have a "good" implementation of g_clear_signal_handler() in
form of a macro. Use it, and don't duplicate the code.

Also add a comment to the documentation that "instance" in fact must
not point to a valid GObject instance -- if the handler ID is unset.

Also reword the documentation about the reasoning for why a macro
version exists. The reason is not to use the function "without
pointer cast". I don't think the non-macro version requires any
pointer cast, since "instance" is a void pointer. Was this referring
to the handler_id_ptr? That doesn't seem right either, because the
caller should always provide a "gulong *" pointer and nothing else.
2021-02-01 09:45:46 +01:00
Thomas Haller
cc4ffe4742 gsignal: use stack allocate temporary buffer in g_signal_new_valist()
g_signal_new_valist() is called by g_signal_new(), which is probably
the most common way to create a signal.

Also, in almost all cases is the number of signal parameters small.

Let's optimize for that by using a stack allocated buffer if we have
few parameters.
2021-02-01 09:43:29 +01:00
Emmanuele Bassi
8199fd65a9 Merge branch '2227-gobject-annotation-fixes' into 'master'
Add more GIR annotations to gparam.c and gsignal.c

See merge request GNOME/glib!1804
2021-01-19 11:35:15 +00:00
Sebastian Dröge
3581eda9a2 gsignal: Allow return types for RUN_FIRST-only signals too
Also adds a test that checks that the G_SIGNAL_RUN flags are handled
correctly and the class signal handler is called at the right times.

Fixes https://gitlab.gnome.org/GNOME/glib/issues/513
2020-12-21 17:22:51 +00:00
Sebastian Dröge
9d1455444c gsignal: Add a new GSignalFlag to mark the first run of an accumulator function
Also add a test for signal accumulators. There was none before, and this
one now also covers the new flag.

Fixes https://gitlab.gnome.org/GNOME/glib/issues/514
2020-12-21 17:10:19 +00:00
Philip Withnall
7ad0c3a0f4 gsignal: Add (closure) and (destroy) annotations
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-12-12 18:08:40 +00:00
Philip Withnall
181a12c393 gsignal: Add missing (transfer) and (nullable) return annotations
This commit only looks at the `Returns:` lines in the documentation, and
has examined all of them in the file. Function arguments have not been
checked.

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

Helps: #2227
2020-12-11 23:49:30 +00:00
Ray Strode
d691c2ebd2 gsignal: Plug g_signal_connect_object leak
commit 916297be79 added a hash table
to provide constant time lookups of signal handlers.

Unfortunately, that commit neglected to remove handlers from
g_signal_connect_object calls from the hash table that are
disconnected implicitly when the associated object goes away.

This commit addresses that bug by changing the closure invalidate
handler associated with the signal connection to properly remove the
handler from the hash table.
2020-10-13 09:49:29 -04:00
Philip Withnall
a63efa4291 tree: Fix various ableist language
In almost all cases, rewording the documentation/comments made things
more specific and a little clearer.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

See: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1544#note_846645
2020-06-23 10:49:44 +01:00
Philip Withnall
00bfb3ab44 tree: Fix various typos and outdated terminology
This was mostly machine generated with the following command:
```
codespell \
    --builtin clear,rare,usage \
    --skip './po/*' --skip './.git/*' --skip './NEWS*' \
    --write-changes .
```
using the latest git version of `codespell` as per [these
instructions](https://github.com/codespell-project/codespell#user-content-updating).

Then I manually checked each change using `git add -p`, made a few
manual fixups and dropped a load of incorrect changes.

There are still some outdated or loaded terms used in GLib, mostly to do
with git branch terminology. They will need to be changed later as part
of a wider migration of git terminology.

If I’ve missed anything, please file an issue!

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-06-12 15:01:08 +01:00
Jehan
13d1697b67 gobject: Add g_{param_spec,signal}_is_valid_name() functions
Making this validation code public allows projects to validate a
GParamSpec name before creating it. While hard-coded GParamSpec don't
need this, we can't afford crashing the main program for dynamically
generated GParamSpec from user-created data.

In such case, we will need to validate the param names and return errors
instead of trying to create a GParamSpec with invalid names.

Includes modifications from Philip Withnall and Emmanuele Bassi to
rearrange the new function addition and split it into one function for
GParamSpecs and one for GSignals.
2020-03-04 14:46:28 +00:00
Philip Withnall
a07bc20557 gsignal: Drop unnecessary warnings from g_signal_lookup()
The `G_TYPE_IS_INSTANTIATABLE()` check is redundant with a precondition
on the function.

The `g_type_class_peek()` check seems like a pointless restriction: it
should be possible to check for a signal from a class init function.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Fixes: #650
2019-12-12 13:00:54 +00:00
Emmanuele Bassi
ec3589b93f Merge branch 'signal-underscores' into 'master'
Signal name handling improvements

See merge request GNOME/glib!1224
2019-12-12 12:10:55 +00:00
Bastien Nocera
6fd2ea1dc6 docs: Fix "occurred" typos in API documentation 2019-11-21 13:07:08 +01:00
Philip Withnall
c577bc89cd gsignal: Warn if g_signal_lookup() is called on an invalid signal name
And add a test for it.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
5e89ba5a74 gsignal: Clarify signal detail format
The format has never previously been specified. It can be anything, but
for sanity’s sake disallow empty strings.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #358
2019-11-15 11:06:14 +00:00
Philip Withnall
89f955db2d gsignal: Canonicalise signal names at installation time
Rather than adding a canonicalised and non-canonicalised version of the
signal to `g_signal_key_bsa`, just add the canonicalised version. Signal
lookups always use the canonicalised key (since the previous commit).

This saves space in `g_signal_key_bsa`, which should speed up lookups;
and it saves significant space in the global `GQuark` table (a 9.6%
reduction in entries in that table, by a rough test using
gnome-software).

We have to be a little more relaxed on the signal name validation than
we are for property name validation, as GTK installs a
`-gtk-private-changed` signal which violates the signal naming rules.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
875e2afa55 gsignal: Canonicalise signal name when looking it up in an object
Previously, we’d look up the signal name as passed to (for example)
`g_signal_lookup()`, and rely on the fact that signals are inserted
twice into `g_signal_key_bsa`; once in canonical form and once not.

In preparation for only inserting signals into `g_signal_key_bsa` once,
we now try looking up a signal with the given signal name and, if that
fails, try canonicalising the name and trying again.

This is a performance hit on lookups for non-canonical names, but
shouldn’t affect the performance of lookups for canonical names. If
people want performance, they should use canonical names.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #358
2019-11-15 11:06:14 +00:00
Philip Withnall
90b51805b7 gsignal: Fold g_quark_try_string() call into signal_id_lookup()
This eliminates a call from every call site of signal_id_lookup(). It
introduces no functional changes, but allows subsequent refactoring.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00