Commit Graph

31956 Commits

Author SHA1 Message Date
Philip Withnall
17e8f653aa Merge branch 'copy_capacity' into 'main'
garray: Set capacity in terminated take functions

See merge request GNOME/glib!4699
2025-07-21 12:31:14 +00:00
Philip Withnall
1ffa751730 Merge branch 'copy_len' into 'main'
garray: Avoid exponential growth in g_array_copy

See merge request GNOME/glib!4698
2025-07-21 12:01:54 +00:00
Philip Withnall
cc65d91559 tests: Add a regression test for GApplication command line handling
This will catch regressions like
fc030b2b64 if they happen again in future,
by testing that fallback argument parsing code path in
`g_application_run()`.

Heavily based on the PyGObject `test_local_and_remote_command_line` unit
test at
578a55982a/tests/test_gio.py (L289).

Thanks to Arjan Molenaar for investigating the failure and writing it
up in !4703.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-07-21 12:52:20 +01:00
Philip Withnall
b3d09306d1 Merge branch 'build_path_oob' into 'main'
gfileutils: Fix OOB read in g_build_path(name)_va

See merge request GNOME/glib!4700
2025-07-21 11:41:25 +00:00
Philip Withnall
e673eb5444 Merge branch 'backtrace_oob' into 'main'
gbacktrace: Fix OOB write in stack_trace

See merge request GNOME/glib!4701
2025-07-21 11:36:47 +00:00
Philip Withnall
96c9b5e0cc Merge branch 'fix-command-line-options-ref' into 'main'
application: NULL check for options

See merge request GNOME/glib!4703
2025-07-21 11:20:39 +00:00
Philip Withnall
bef060824c Merge branch 'wip/kabus/fix-leaks' into 'main'
gio/filenamecompleter: Fix leaks

See merge request GNOME/glib!4702
2025-07-21 11:19:30 +00:00
Arjan Molenaar
fc030b2b64 application: NULL check for options
Do an extra check if the options argument is NULL,
This will avoid unnessecary (critical warning).

`g_application_run` calls the code with options == NULL.
2025-07-20 16:40:13 +02:00
Khalid Abu Shawarib
3d13946c3b gio/filenamecompleter: Fix leaks 2025-07-20 04:57:01 +03:00
Tobias Stoeckmann
183d71d215 gbacktrace: Fix OOB write in stack_trace
The array buffer is of size BUFSIZE. The if-check correctly avoids
writing characters into the buffer, but the ending newline may still
overflow buffer. Keep space for the EOL character.
2025-07-18 23:29:54 +02:00
Tobias Stoeckmann
23b70b0d36 gfileutils: Fix OOB read in g_build_path(name)_va
If an array with more than INT_MAX elements is passed to functions
internally calling g_build_path_va or g_build_pathname_va, then a
signed integer overflow and eventual out of boundary read access can
occur.

Use size_t instead of gint for lengths and array sizes.
2025-07-18 22:50:27 +02:00
Michael Catanzaro
03664fe237 Merge branch 'bsd-gio-tool-fix' into 'main'
tests: Loosen string comparison assertion in gio-tool.py

See merge request GNOME/glib!4693
2025-07-18 13:39:54 -05:00
Tobias Stoeckmann
d90352a43d garray: Avoid exponential growth in g_array_copy
The g_array_copy function uses elt_capacity as length argument for
g_array_sized_new. With a zero terminated array, this effectively
means that the next allocation is doubled in size.

Avoid this by doing the same as g_ptr_array_copy, i.e. use the length.
This makes sure that elt_capacity is roughly the same (only differs
if the copied array has unallocated data in it).
2025-07-18 20:23:10 +02:00
Tobias Stoeckmann
5c15ecb0c5 garray: Set capacity in terminated take functions
If supplied data argument is not NULL, then add the actually existing
null/zero terminated element to alloc/elt_capacity. Otherwise the
termination element is not taken into account, because the length only
counts the non-termination elements.

Purely defensive measurement. I don't think that this triggered any
bug (only one needless realloc call if set_size functions are called
with the current length).
2025-07-18 19:52:20 +02:00
Philip Withnall
2bba358f0b Merge branch '3712-thread-pool' into 'main'
gthreadpool: Catch pool_spawner creation failure

Closes #3712

See merge request GNOME/glib!4696
2025-07-17 22:46:50 +00:00
Peter Bloomfield
3bee5d4aaa gthreadpool: Catch pool_spawner creation failure 2025-07-17 22:46:50 +00:00
Philip Withnall
16d170cdf6 Merge branch 'size_width_skip' into 'main'
tests: Do not always skip array overflow checks

See merge request GNOME/glib!4694
2025-07-17 08:50:46 +00:00
Philip Withnall
03c720df7d Merge branch 'array_elt_size_zero' into 'main'
garray: Add more element_size > 0 checks

See merge request GNOME/glib!4695
2025-07-17 08:48:25 +00:00
Philip Withnall
299b235b90 Merge branch 'array-test-leak-fix' into 'main'
tests: Fix a minor leak in array-test

See merge request GNOME/glib!4692
2025-07-17 08:43:46 +00:00
Tobias Stoeckmann
932ef1765c tests: Do not always skip array overflow checks
The preprocessor check for SIZE_WIDTH fails if stdint.h is not included.
Also, adjust the error messages for clarity.
2025-07-15 22:15:42 +02:00
Tobias Stoeckmann
b0860ae9d4 garray: Add more element_size > 0 checks
The functions g_array_new and g_array_sized_new already protect
themselves against a zero element size.

Do the same in g_array_new_take and g_array_new_take_zero_terminated
to avoid a NULL pointer dereference and an endless loop.
2025-07-15 22:14:35 +02:00
Philip Withnall
1eabe6c6b1 Merge branch 'null_terminated_check' into 'main'
garray: Fix out of boundary write in g_ptr_array_copy

See merge request GNOME/glib!4690
2025-07-15 12:40:56 +00:00
Philip Withnall
41fe7c3797 tests: Loosen string comparison assertion in gio-tool.py
On BSD, the subprocess being spawned by `gio launch` ends up emitting
debug output onto stdout, which confuses the strict string assertion in
the test.

Instead use a ‘contains’ assertion.

Fixes this failure:
```
348/385 glib:gio+no-valgrind / gio-tool.py                                     ERROR            0.39s   exit status 1
――――――――――――――――――――――――――――――――――――― ✀  ―――――――――――――――――――――――――――――――――――――
stderr:
(test program exited with status code 1)
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
stdout:  12: UNKNOWN:   ---
stdout:  13: UNKNOWN:     message: |
stdout:  14: UNKNOWN:       Traceback (most recent call last):
stdout:  15: UNKNOWN:         File "/var/tmp/gitlab_runner/builds/Ff4WDDRj/0/GNOME/glib/_build/../gio/tests/gio-tool.py", line 130, in test_absolute_from_folder
stdout:  16: UNKNOWN:           self.launchAndCheck(self.entry, cwd=self.folder)
stdout:  17: UNKNOWN:         File "/var/tmp/gitlab_runner/builds/Ff4WDDRj/0/GNOME/glib/_build/../gio/tests/gio-tool.py", line 126, in launchAndCheck
stdout:  18: UNKNOWN:           self.assertEqual(result.out, str(self.entry))
stdout:  19: UNKNOWN:       AssertionError: '(gio launch:35500): GLib-GIO-DEBUG: 04:55[130 chars]ntry' != '/tmp/tmpa8oxxwvv/folder/desktop.entry'
stdout:  20: UNKNOWN:       - (gio launch:35500): GLib-GIO-DEBUG: 04:55:51.790: _g_io_module_get_default: Found default implementation local (GLocalVfs) for ‘gio-vfs’
stdout:  21: UNKNOWN:         /tmp/tmpa8oxxwvv/folder/desktop.entry
stdout:  22: UNKNOWN:   ...
```

seen here: https://gitlab.gnome.org/GNOME/glib/-/jobs/5301812

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-07-15 12:26:43 +01:00
Philip Withnall
582cf1da92 Merge branch 'docs-module-constructor-locks' into 'main'
docs: Calling gmodule funcs in constructors may deadlock

Closes #1443

See merge request GNOME/glib!4691
2025-07-15 11:18:27 +00:00
Nick Steel
267b1246d7 docs: Calling gmodule funcs in constructors may deadlock
Fixes #1443.
2025-07-15 11:18:27 +00:00
Philip Withnall
a9c542fd62 tests: Fix a minor leak in array-test
Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2025-07-15 12:16:57 +01:00
Tobias Stoeckmann
42cd9d7efa garray: Simplify null termination handling
Apply GArray's g_array_maybe_expand overflow checking logic to
GPtrArray's g_ptr_array_maybe_expand function:

Let g_ptr_array_maybe_expand handle the null_terminated flag internally
to check if an overflow occurs instead of letting callers do these
check on their own.

The g_ptr_array_copy function lacked this check.

Having a centralized position for this check simplifies the code and
further code auditings.
2025-07-14 18:19:54 +02:00
Tobias Stoeckmann
eaffbef760 garray: Fix size checks when taking arrays
The functions g_array_new_take_zero_terminated and
g_ptr_array_new_take_null_terminated must take into account that the
last element will be the terminating element (zero filled or NULL).

Iterating through all elements must not reach G_MAXUINT, because in
that case no space is left for the terminating element.
2025-07-14 17:40:02 +02:00
Philip Withnall
6e2a31f97a Merge branch 'vprintf_int' into 'main'
gstring: Fix g_string_append_vprintf overflow

See merge request GNOME/glib!4687
2025-07-12 15:04:08 +00:00
Philip Withnall
457ba21d28 Merge branch 'binary_search' into 'main'
garray: Use g_array_elt_len/pos where appropriate

See merge request GNOME/glib!4685
2025-07-12 15:01:15 +00:00
Philip Withnall
ec1fb920bd Merge branch 'zero_terminate_null' into 'main'
garray: Support unallocated zero terminated arrays

See merge request GNOME/glib!4684
2025-07-12 14:56:48 +00:00
Philip Withnall
e43ebf8e61 Merge branch 'gapplication-fixes' into 'main'
gapplication: Call before/after_emit consistently

Closes #3726

See merge request GNOME/glib!4688
2025-07-11 11:51:42 +00:00
Matthias Clasen
1bb48a76a6 application: Call before/after_emit consistently
Call these vfuncs also for cases where the launching instance
is the primary one. This is what the docs suggest, and it makes
before/after_emit much more useful.

Fixes: #3726
2025-07-11 07:33:02 -04:00
Matthias Clasen
182185e31e application: Clarify documentation
Existing uses of before_emit in GTK will break if an app overrides
before/after_emit without chaining up. Clarify in the documentation
that these vfuncs need to chain up.
2025-07-11 07:33:02 -04:00
Matthias Clasen
de85736791 tests: Remove an overzealous assertion
The dbus-appinfo test was asserting that before_emit only happens
when we haven't seen a startup ID yet. But the docs imply that it
gets emitted for every activate/open/commandline, which may well
happen repeatedly. So drop this assertion.
2025-07-11 07:33:02 -04:00
Tobias Stoeckmann
591e39dbe9 garray: Support unallocated zero terminated arrays
The g_array_new_take_zero_terminated function could lead to NULL
data pointer if it is called with (NULL, FALSE, x), i.e. with a NULL
pointer and no clear request.

This in turn means that g_array_steal could behave like
g_ptr_array_steal, i.e. it would return NULL instead of a zero
terminated array, which does not match its description.

Also, g_array_remove_range and g_array_set_size could lead to NULL
pointer dereferences with such arrays.

Support all these cases and adjust the API description to reflect
current behavior. It brings GArray and GPtrArray functionality closer
to each other without breaking existing API/ABI for programs.
2025-07-10 17:00:12 +02:00
Tobias Stoeckmann
1f06e086f3 gstring: Fix g_string_append_vprintf overflow
The g_string_append_vprintf function could overflow with strings
which are INT_MAX bytes long. The eventual memcpy call copies INT_MAX
plus additional nul byte into newly allocated memory. This means
that due to signed integer overflow more bytes are copied than
could ever fit.
2025-07-09 22:37:48 +02:00
Tobias Stoeckmann
ca1b2028fc garray: Use g_array_elt_len/pos where appropriate
The multiplication of two guint values could overflow with large
arrays. Use g_array_elt_len and g_array_elt_pos to avoid explicit
elt_size multiplications.
2025-07-09 20:28:21 +02:00
Philip Withnall
5da569a425 Merge branch 'pgriffis/ipv6-scope-id' into 'main'
Fix IPv6 scope-id from DNS responses being lost

See merge request GNOME/glib!4676
2025-07-09 14:00:18 +00:00
Philip Withnall
f560c7b792 Merge branch 'fix-k-field-code-expansion' into 'main'
gio-tool-launch: fix %k field code expansion

See merge request GNOME/glib!4682
2025-07-09 13:10:28 +00:00
Philip Withnall
5cff35b41e Merge branch 'source-destroy-while-callback-indirect-unref' into 'main'
gmain: move source destroy write unlock slightly earlier

Closes #3725

See merge request GNOME/glib!4686
2025-07-09 12:50:09 +00:00
Christoph Martin
e2970dad72 tests: skip gio launch tests on darwin
Tests currently fail under macOS because the tool claims not to work
on apple devices. Since I cannot disprove this myself, I'm disabling the
tests on Darwin.
2025-07-09 13:39:34 +01:00
Christoph Martin
c924de69f0 gio-tool-launch: fix %k field code expansion
As per the desktop entry specification, the `%k` field code should be
expanded to the location of the desktop entry file being processed. This
is only possible if the constructor-only filename property is populated,
which does not happen when using g_desktop_app_info_new_from_keyfile().

Moreover, since the Path directive in a desktop entry can be used to
set the working directory for the program to be launched, the location
passed as argument to the program must be modified such that it points
at the correct file when interpreted by the launched program. The
simplest way to achieve this consistently is to pass an absolute path.

However, g_desktop_app_info_new_from_keyfile() does not indicate why it
fails when it does. Because the tool aims to indicate whether launching
failed due to a missing file or a malformed one we first check this with
g_key_file_load_from_file().
2025-07-09 13:39:34 +01:00
Christoph Martin
c2debf4fa9 tests: ensure gio launch expands field code %k
This introduces an integration test that executes gio launch from a
variety of working directories, and checks that %k is expanded to a
location that makes sense in the context of the executed program, i.e.
an absolute path.
2025-07-09 13:39:34 +01:00
Matthew Waters
fe1c7dfdc6 gmain: move source destroy write unlock slightly earlier
If a source is using g_source_set_callback_indirect(), then performing
GSource operations within GSourceCallbackFuncs::unref should not cause a
deadlock.

Fixes https://gitlab.gnome.org/GNOME/glib/-/issues/3725
2025-07-09 11:59:17 +10:00
Christoph Martin
032aea93d2 testprogramrunner: enable setting cwd
Add a mechanism to change the current working directory the program
under test is being run from.
2025-07-08 22:59:56 +02:00
Michael Catanzaro
25a176869d Merge branch 'gio-launch-quotes' into 'main'
gio-tool-launch: Fix mismatched curly quotes in translatable strings

See merge request GNOME/glib!4683
2025-07-08 08:05:40 -05:00
Philip Withnall
e6b4c28bba Merge branch 'string_expand_oflow' into 'main'
gstring: Improve g_string_expand/g_string_append_len_inline checks

See merge request GNOME/glib!4681
2025-07-08 13:01:05 +00:00
Tobias Stoeckmann
3752760c50 gstring: Improve g_string_append_len_inline checks
Use the same style for the G_LIKELY check here as in g_string_sized_new.
The check could overflow on 32 bit systems.

Also improve the memcpy/memmove check to use memcpy if val itself is
adjacent to end + len_unsigned, which means that no overlapping exists.
2025-07-08 14:30:38 +02:00
Tobias Stoeckmann
6aa97beda3 gstring: Fix g_string_sized_new segmentation fault
If glib is compiled with -Dglib_assert=false, i.e. no asserts
enabled, then g_string_sized_new(G_MAXSIZE) leads to a segmentation
fault due to an out of boundary write.

This happens because the overflow check was moved into
g_string_maybe_expand which is not called by g_string_sized_new.

By assuming that string->allocated_len is always larger than
string->len (and the code would be in huge trouble if that is not true),
the G_UNLIKELY check in g_string_maybe_expand can be rephrased to
avoid a potential G_MAXSIZE overflow.

This in turn leads to 150-200 bytes smaller compiled library
depending on gcc and clang versions, and one less check for the most
common code paths.

Reverts https://gitlab.gnome.org/GNOME/glib/-/merge_requests/4655 and
reorders internal g_string_maybe_expand check to still fix
CVE-2025-6052.
2025-07-08 14:30:26 +02:00