Commit Graph

8191 Commits

Author SHA1 Message Date
Marco Trevisan (Treviño)
5ff02aa5f9 gmacros: Use C++ namespaces attribute specifier sequences for msvc
As we do already for GNU compilers, when using C++ we should use
attribute sequences with msvc namespace.

This is not supported by msvc versions earlier than 2019 16.7 according
to [1], and thus we need to check if `_MSC_VER` is at least 1927 [2].

See: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2895#note_1572952

[1] https://www.codetd.com/en/article/11761480
[2] https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170&viewFallbackFrom=vs-2019#feedback
2022-11-24 17:47:56 +01:00
Marco Trevisan
ad755e7489 Merge branch 'c-cxx-std-versions' into 'main'
Expose C and C++ standard versions and add macros to check them

See merge request GNOME/glib!2895
2022-11-22 21:10:34 +00:00
Marco Trevisan (Treviño)
ff49707501 gmacros: Use G_C_STD_CHECK_VERSION to define C-std dependent items 2022-11-22 17:45:56 +01:00
Philip Withnall
e535f22b08 gmem: Fix introspection annotations for g_clear_pointer() and g_clear_fd()
They were mixing up `(optional)` and `(nullable)`, and didn’t correctly
annotate the arguments as `(inout)` or `(transfer full)`.

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

Fixes: #2793
2022-11-22 16:25:39 +00:00
Marco Trevisan (Treviño)
633561ada2 macros: Add a generic way to get and check the supported C standard
Try to get the value of __STDC_VERSION__ if supported, if not just
fallback to the oldest standard that any compiler should handle.
2022-11-22 17:23:14 +01:00
Marco Trevisan (Treviño)
14ba699508 meson: Compile some tests with multiple C standards
We need to ensure that all the expected macros and utilities are working
with all the supported C standards, so just repeat the tests with all
the ones the compiler supports.
2022-11-22 17:23:14 +01:00
Marco Trevisan (Treviño)
641256ea22 gmacros: Prioritize the usage of [[noreturn]] in C++11
We defined G_NO_RETURN as [[noreturn]] in the C++ case, but only after
trying the __attribute__ syntax, so it was never used in GNUC compatible
compilers.

Give it priority instead when supporting a C++11 compiler and onwards.

As per this we need to adapt the code in the places where it was not
properly used (leading to compilation warnings).
2022-11-22 17:23:13 +01:00
Marco Trevisan (Treviño)
372ab7a964 glib: Use G_CXX_STD_VERSION to check how to behave with C++ compilers 2022-11-22 17:23:12 +01:00
Marco Trevisan
3af38a3a1a Merge branch 'coverity-fixes' into 'main'
gthreadpool: Mark an explicit leak as to be ignored

See merge request GNOME/glib!3081
2022-11-22 16:10:01 +00:00
Philip Withnall
59825c92e0 gthreadpool: Mark an explicit leak as to be ignored
This thread is created once during the process’ lifetime and cannot be
destroyed and recreated, as the thread scheduler settings might have
changed since then.

Mark the leak as explicit, mostly for documentation purposes — but it
might quieten some static analysers.

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

Coverity CID: #1474614
2022-11-22 14:32:25 +00:00
Philip Withnall
bcd364afef gstrfuncs: Fix regression in types accepted by g_str_equal()
The new macro form of `g_str_equal()` had stricter type checking than
the original function form. That would be nice, except it causes new
compiler warnings in third party projects, which counts as an API break
for us, so unfortunately we can’t do it.

Add some tests to prevent regressions on this again.

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

Fixes: #2809
2022-11-22 13:12:05 +00:00
Marco Trevisan (Treviño)
f9845abe39 gmacros: Define G_CXX_STD_VERSION and check macros
Sadly, in C++ there's not an universal way to get what language standard
is used to compile GLib-based programs, in fact while most compilers
relies on `__cplusplus`, MSVC is defining that, but it does not use it
to expose such information (unless `/Zc:__cplusplus` arg is used).
On the other side, MSVC reports the language standard via _MSVC_LANG [1].

This complication makes us defining some macros in a very complex way
(such as glib_typeof()), because we need to perform many checks just to
understand if a C++ compiler is used and what standard is expecting.

To avoid this, define multiple macros that can be used to figure out
what C++ standard is being used.

[1] https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170
2022-11-21 20:58:54 +01:00
Philip Withnall
e5771df643 gvariant: Add an assertion to clarify some tuple logic
This should quell a scan-build error about dereferencing `member_info`
when it’s `NULL` at the end of the function, due to having zero
iterations of the `for` loop.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-11-18 17:00:42 +00:00
Philip Withnall
146a0a001d gslice: Tag mem_error() as not returning because it aborts
This should quell some scan-build warnings about code breaking after
returning from mem_error() in a weird state.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-11-18 16:36:47 +00:00
Alexander Kanavin
a9fa7589c6 tests: Work around Meson bug with using exe_wrapper
Do not use can_run_host_binaries() as it returns true even though
custom_target() does not currently correctly wrap target-built
tool binaries with exe_wrapper so they can be run on the host.

See https://github.com/mesonbuild/meson/issues/11029
2022-11-14 13:01:47 +00:00
Philip Withnall
6e57afdefd Merge branch '2782-variant-maybe-wrapper-speedup' into 'main'
gvariant-parser: Speed up maybe_wrapper() by an order of magnitude

Closes #2782

See merge request GNOME/glib!3061
2022-11-08 17:11:07 +00:00
Philip Withnall
9ae59bd647 gvariant-parser: Speed up maybe_wrapper() by an order of magnitude
This further helps with the potential denial of service problem in
issue #2782 / oss-fuzz#49462 / oss-fuzz#20177.

Instead of allocating a new `GVariant` for each nesting level of
maybe-types, allocate a single `GVariant` and give it the fully-nested
maybe type as its type. This has to be done in serialised form.

This prevents attackers from triggering O(size of container × typedecl
depth) allocations.

This is a follow up to commit 3e313438f1,
and includes a test.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
Fixes: #2782
oss-fuzz#20177
oss-fuzz#49462
2022-11-08 16:14:02 +00:00
Simon McVittie
3e1074f403 Merge branch 'misc-variant-fixes' into 'main'
tests: Fix a small leak in the GVariant tests and some minor documentation typos

See merge request GNOME/glib!3062
2022-11-08 15:43:58 +00:00
Philip Withnall
64f5e3ab2d gvariant: Fix some minor typos in documentation comments
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-11-08 15:16:50 +00:00
Philip Withnall
6db82d98d3 tests: Fix a small leak in the GVariant tests
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-11-08 15:16:50 +00:00
Philip Withnall
e2b9e28ece Merge branch 'wip/3v1n0/meson-fix-tests-binary-dependencies' into 'main'
test/meson: Add tests dependencies on built programs, libraries and modules

See merge request GNOME/glib!3015
2022-11-08 12:30:53 +00:00
Philip Withnall
4b561a908f Merge branch '2802-drop-g-os-darwin' into 'main'
Revert "meson: Define G_OS_DARWIN when compiling under OSX or iOS"

Closes #2802

See merge request GNOME/glib!3047
2022-11-07 13:03:16 +00:00
John Ralls
06650545dd [Darwin] Use the non-cancelable variant of close(). 2022-11-07 13:01:47 +00:00
Philip Withnall
ad4c59c3c6 docs: Mention __APPLE__ instead of G_OS_DARWIN for detecting Darwin
See discussion on #2802 and in the previous two commits.

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

Fixes: #2802
2022-11-07 11:30:32 +00:00
Philip Withnall
9ebb491d55 Revert "meson: Define G_OS_DARWIN when compiling under OSX or iOS"
This reverts commit e85635daa0.

See the previous revert for rationale.

Fixes: #2802
2022-11-07 11:30:32 +00:00
Philip Withnall
7b52ccbfc4 Revert "gio, glib: Use G_OS_DARWIN for code that is for such environments"
This reverts commit 476e33c3f3.

We’ve decided to remove `G_OS_DARWIN` in favour of recommending people
use `__APPLE__` instead. As per the discussion on #2802 and linked
issues,
 * Adding a new define shifts the complexity from “which of these
   platform-provided defines do I use” to “which platform-provided
   defines does G_OS_DARWIN use”
 * There should ideally be no cases where a user of GLib has to use
   their own platform-specific code, since GLib should be providing
   appropriate abstractions
 * Providing a single `G_OS_DARWIN` to cover all Apple products (macOS
   and iOS) hides the complexity of what the user is actually testing:
   are they testing for the Mach kernel, the Carbon and/or Cocoa user
   space toolkits, macOS vs iOS vs tvOS, etc

Helps: #2802
2022-11-07 11:30:32 +00:00
Ray Strode
69b410c58a tests: Test g_str_has_prefix inside G_UNLIKELY
g_str_has_prefix uses G_UNLIKELY itself, and up
until recently, G_UNLIKELY could not be nested.

This commit adds a test that nests G_UNLIKELY to
make sure it continues to work going forward.
2022-11-04 15:07:58 -04:00
Thomas Haller
73bd146409 macros: generate uniq variable name in _G_BOOLEAN_EXPR()/G_LIKELY()
This avoids a -Wshadow warning when nesting G_LIKELY() inside
each other due to _g_boolean_var_.

This can be easily encountered when using macros:

```
    #define GET_VALUE(arg) \
        ({ \
           typeof (arg) _arg = (arg); \
           \
           g_assert (_arg); \
           get_value (_arg); \
        })
    g_assert (GET_VALUE (a) > 5);
```

__COUNTER__ is a GCC extension, but the definition of _G_BOOLEAN_EXPR()
is already inside a

  #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)

block.

Closes: #1211
2022-11-04 15:07:58 -04:00
Ray Strode
48730d2b30 gstrfuncs: Add back x + !x warning workaround
We thought we could drop the x + !x workaround in
commit eea87eff3f but apparently
not.

This commit adds it back, but with an added layer of indirection,
for aesthetics.

Closes: #2807
2022-11-04 13:13:13 -04:00
Ray Strode
05750454d5 gstrfuncs: Use less tristates in g_str_has_*
tri-state conditionals are kind of hard to read, especially
when they're nested.

This commit changes g_str_has_* macros to use if statements
instead.
2022-11-04 13:02:01 -04:00
Ray Strode
eea87eff3f gstrfuncs: Make g_str_has_* more robust against non-idempotent inputs 2022-11-03 23:24:44 +00:00
Simon McVittie
1dd11faaea fileutils test: Use no-op invalid-parameter handler when exercising EBADF
This is basically !3036, but wasn't included there because !3036
and !3027 were developed in parallel.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-11-03 20:39:37 +00:00
Simon McVittie
201c920cbd Merge branch 'fix-fileutils-test' into 'main'
Clean up Windows Invalid Parameter Handler setup and fix fileutils test

Closes #2800

See merge request GNOME/glib!3036
2022-11-02 22:59:14 +00:00
Simon McVittie
f1f711dc77 tests: Test EBADF and errno handling when closing fds
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-11-02 21:26:34 +00:00
Simon McVittie
81130a6e3f gstdio: Document async-signal-safety of g_clear_fd and g_autofd
g_clear_fd wraps g_close and is async-signal-safe under essentially the
same circumstances. If fd_ptr already pointed to a negative number, then
g_clear_fd doesn't call g_close, and is still async-signal-safe.

g_autofd passes a NULL error pointer to g_clear_fd, so it is
async-signal-safe, as long as no programming error occurs.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-11-02 21:26:34 +00:00
Simon McVittie
e231132fc3 gstdio: Document errno behaviour of g_clear_fd
g_clear_fd has the same interaction with errno as g_close or most libc
functions: on success it has an unspecified effect on errno, and on
failure (other than programming error) it sets errno to indicate the
reason for failure.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-11-02 21:26:34 +00:00
Simon McVittie
088d8b4359 gstdio: Preserve errno when calling g_clear_fd() from g_autofd
g_clear_fd() can alter errno, but it's unexpected for leaving a scope
to change errno.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-11-02 21:26:34 +00:00
Chun-wei Fan
41d8ed37cd glib/tests/meson.build: Expect fileutils to pass on Windows
The underlying issue was fixed, so expect that to pass.
2022-11-03 00:11:45 +08:00
Chun-wei Fan
4f426c56d0 fileutils.c: Fix the clearfd test on Windows
...when the test program aborts while checking the FD's were indeed
closed, since we need to override the invalid parameter handler to do
such checks, if the CRT demands so, so that the test program will
proceed normally.

This will fix issue #2800.
2022-11-03 00:11:45 +08:00
Chun-wei Fan
9bcc9405d7 glib: Port to the private invalid parameter handler APIs
Use the newly-added private APIs added in the previous commit so that we
can clean up the code a bit.
2022-11-03 00:11:45 +08:00
Chun-wei Fan
08e0fef632 glib-private.c: Add private API to override invalid parameter handler
...if supported, as in the previous commit.  We will eventually use
these private API to override the invalid parameter handler as needed
in the other parts of GLib and the tests.

We also now use _set_thread_local_invalid_parameter_handler()
instead of just _set_invalid_parameter_handler() to be safer, if
that is available.

This can be expanded upon in the future if we desire to use a stricter
or more customized invalid parameter handler.
2022-11-03 00:11:45 +08:00
Chun-wei Fan
b92b17f021 build: Check for invalid parameter overriding on Windows
Allow one to override the invalid parameter handler if we have the
following items:

* _set_invalid_parameter_hander() or
  _set_thread_local_parameter_handler()
* _CrtSetReportMode() as a function or macro

Currently, we are doing this on Visual Studio to allow GSpawn to work on
Windows as well as having the log writer support color output, as we
might be passing in file descriptors that are invalid, which will cause
the CRT to abort unless the default invalid parameter handler is
overridden.
2022-11-03 00:11:45 +08:00
Ray Strode
274150a391 gspawn: Provide less brute force fdwalk implementation on macOS
It seems macOS has a way to query open fds for a process, so this
commit tries to use it, instead of just trying all fds between
0 and $BIGNUMBER.
2022-11-02 11:31:08 -04:00
Ray Strode
aaae515e13 Revert "gstdio: Temporarily disable g_close() warning on macOS"
This reverts commit 5190354ad9.

Now that g_close isn't called from gspawn anymore, we can reenable
the g_close warning on macOS.

Closes: #2785
2022-11-02 09:40:28 -04:00
Ray Strode
168fd4f2b3 gspawn: Don't use g_close when doing fdwalk to close potentially invalid fds
Some platforms (e.g., macOS) don't currently have a way
to close only open fds in preparation for exec. On these
platforms, glib just bites the bullet and calls g_close for
the whole fileno range.

g_close only allows valid fds to be given to it, though.

This commit ensures close is called instead of g_close on
those platforms by splitting the safe_fdwalk implementation
that operates on invalid fds off to its own function and
only using it as a fall back.
2022-11-02 09:36:26 -04:00
Ray Strode
ac4dbc82e1 gspawn: Make sure pipe fds end up at the right place
It's possible when gspawn sets up its pipes for standard io,
that the pipe fds themselves end up in the standard io range
reserved for stdin, stdout, stderr.

This commit protects against that problem by relocating the
fds up, outside of the range.

Closes: #16
2022-11-02 09:36:26 -04:00
Ray Strode
e3ae492550 tests: Add g_spawn_test for when stdio fd range is exposed
This commit adds a test to make sure GSpawn still works even
if fds 0, 1, and 2 are closed before it's run.
2022-11-02 09:36:26 -04:00
Ray Strode
2399585860 gspawn: Rename CHILD_DUP2_FAILED to CHILD_DUPFD_FAILED
The error code is already used for both F_DUPFD and dup2
already, and having dup2 in the name is oddly specific.

This renames the error code for clarity.
2022-11-02 09:06:34 -04:00
Ray Strode
e5782dcafc glib-unix: Add test to make sure g_unix_open_pipe will intrude standard range
Now that we know it's a bad idea to avoid the standard io fd range
when getting pipe fds for g_unix_open_pipe, we should test to make sure
we don't inadvertently try to do it again.

This commit adds that test.
2022-11-02 09:06:34 -04:00
Ray Strode
dad97d24d5 Revert "Handling collision between standard i/o file descriptors and newly created ones"
g_unix_open_pipe tries to avoid the standard io fd range
when getting pipe fds. This turns out to be a bad idea because
certain buggy programs rely on it using that range.

This reverts commit d9ba615090

Closes: #2795
Reopens: #16
2022-11-02 09:06:34 -04:00