Commit Graph

8199 Commits

Author SHA1 Message Date
Philip Withnall
6c39f08f37 gstrfuncs: Fix regression in C++ types accepted by g_str_equal()
Further to commit bcd364afef, fix the types accepted by the
`g_str_equal()` macro for C++ too. C++ is more restrictive about
const-correctness.

Add unit tests.

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

Fixes: #2820
2022-11-28 12:58:05 +00:00
Philip Withnall
2ee44ba1d5 Merge branch 'msvc-cxx-use-native-attribute-specifiers' into 'main'
gmacros: Use C++ namespaces attribute specifier sequences for msvc

See merge request GNOME/glib!3086
2022-11-24 18:12:29 +00:00
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
Philip Withnall
66499d2199 Revert "build: Don’t define redundant built-in variables in pkgconfig calls"
This reverts commit 7e3e591d43.

The freedesktop SDK, which is used by gnome-build-meta, only has Meson
0.63. Bumping GLib’s Meson dependency to 0.64 means that, at the moment,
GLib is not buildable in gnome-build-meta and hence can’t be tested in
nightly pipelines against other projects, etc.

That’s bad for testing GLib.

It’s arguably bad that we’re restricted to using an older version of
Meson than shipped by Debian Testing, but that’s a separate discussion
to be had.

Revert the Meson 0.64 dependency until the freedesktop SDK ships Meson ≥
0.64. This also means reverting the simplifications to use of
`gnome.mkenum_simple()`.

See https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3077#note_1601064
2022-11-24 12:10:03 +00:00
Simon McVittie
78d0ac4237 Merge branch 'valgrind-fd-fun' into 'main'
gspawn: Ignore invalid FDs when using safe_fdwalk()

See merge request GNOME/glib!3076
2022-11-23 17:34:05 +00:00
Xavier Claessens
6dd5c5002a Merge branch 'wip/pwithnall/meson-0.64' into 'main'
build: Bump Meson dependency to 0.64.0

See merge request GNOME/glib!3077
2022-11-23 11:48:51 +00:00
Philip Withnall
7e3e591d43 build: Don’t define redundant built-in variables in pkgconfig calls
This is deprecated since Meson 0.62.0, since Meson does this
automatically for us.

This fixes a Meson configure warning.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-11-23 10:39:58 +00: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
3ee8dafb27 gspawn: Fold in safe_fdwalk_with_invalid_fds() helper function
Since it’s now always called the same way after safe_fdwalk() has been
called. This introduces no functional changes.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-11-22 14:35:12 +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
f0e08e3488 gspawn: Ignore invalid FDs when using safe_fdwalk()
In `safe_closefrom()`, we thought it would be OK to assert that an FD
being closed is valid, when using `safe_fdwalk()`, as it only walks over
known-valid FDs.

However, there is a situation where that might not be true: the program
is being run under valgrind. Valgrind opens some FDs for its own use
which are ≥1024, and it emulates a lowered soft limit on FDs. So if we
were to use `safe_fdwalk_with_invalid_fds()` it would see the lowered
soft limit and not try to close valgrind’s internal FDs.

However, `safe_fdwalk()` looks at `/proc`, which valgrind does not emulate,
so it sees the secret valgrind internal FDs, and then tries to close them.
Valgrind doesn’t like this, prints ‘Warning: invalid file descriptor
1024 in syscall close()’ and returns `EBADF`. That return value
causes `g_close()` to warn about faulty FD refcounting, and that causes
unit test failures.

Fix that by relaxing our assumptions about FD validity: use
the `close_func_with_invalid_fds()` call back for closing FDs
from `safe_fdwalk()`, rather than using `close_func()`. That will
ignore `EBADF` return values.

This should fix valgrind failures like this one:
https://gitlab.gnome.org/GNOME/glib/-/jobs/2389977

Related prior art: https://bugs.freedesktop.org/show_bug.cgi?id=99839

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-11-22 13:52:01 +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