Commit Graph

7207 Commits

Author SHA1 Message Date
Emmanuel Fleury
9464721324 Fix signedness warning in glib/tests/protocol.c
glib/tests/protocol.c: In function ‘test_error’:
glib/tests/protocol.c:252:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’
  252 |   for (i = 0; i < G_N_ELEMENTS (tests); i++)
      |                 ^
2020-11-13 10:12:16 +01:00
Emmanuel Fleury
76b84c5f66 Fix signedness warnings in glib/tests/pattern.c
glib/tests/pattern.c: In function ‘main’:
glib/tests/pattern.c:218:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’
  218 |   for (i = 0; i < G_N_ELEMENTS (compile_tests); i++)
      |                 ^
glib/tests/pattern.c:225:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’
  225 |   for (i = 0; i < G_N_ELEMENTS (match_tests); i++)
      |                 ^
glib/tests/pattern.c:232:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’
  232 |   for (i = 0; i < G_N_ELEMENTS (equal_tests); i++)
      |                 ^
2020-11-13 10:12:16 +01:00
Emmanuel Fleury
be5d48fe77 Fix various warnings in glib/tests/gvariant.c
glib/tests/gvariant.c: In function ‘append_tuple_type_string’:
glib/tests/gvariant.c:206:17: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gint’ {aka ‘int’}
  206 |   for (i = 0; i < size; i++)
      |                 ^
glib/tests/gvariant.c:210:13: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘int’
  210 |       if (i < size - 1)
      |             ^
glib/tests/gvariant.c:223:17: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gint’ {aka ‘int’}
  223 |   for (i = 0; i < size; i++)
      |                 ^
glib/tests/gvariant.c: In function ‘describe_type’:
glib/tests/gvariant.c:386:29: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gint’ {aka ‘int’}
  386 |               for (i = 0; i < length; i++)
      |                             ^
glib/tests/gvariant.c: In function ‘describe_info’:
glib/tests/gvariant.c:882:23: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gint’ {aka ‘int’}
  882 |         for (i = 0; i < length; i++)
      |                       ^
glib/tests/gvariant.c: In function ‘check_offsets’:
glib/tests/gvariant.c:962:21: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gint’ {aka ‘int’}
  962 |       for (i = 0; i < length; i++)
      |                     ^
glib/tests/gvariant.c: In function ‘tree_instance_check_gvariant’:
glib/tests/gvariant.c:2636:44: error: comparison of integer expressions of different signedness: ‘gboolean’ {aka ‘int’} and ‘guint64’ {aka ‘long unsigned int’}
 2636 |       return g_variant_get_boolean (value) == tree->data.integer;
      |                                            ^~
glib/tests/gvariant.c: In function ‘test_varargs’:
glib/tests/gvariant.c:3090:26: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
 3090 |       g_assert_true (val == i++ || val == 0);
      |                          ^~
2020-11-13 10:12:16 +01:00
Emmanuel Fleury
700a9d4042 Fix signedness warnings in glib/tests/once.c
glib/tests/once.c: In function ‘test_once_init_multi_threaded’:
glib/tests/once.c:183:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’
  183 |   for (i = 0; i < G_N_ELEMENTS (threads); i++)
      |                 ^
glib/tests/once.c:186:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’
  186 |   for (i = 0; i < G_N_ELEMENTS (threads); i++)
      |                 ^
2020-11-13 10:12:16 +01:00
Emmanuel Fleury
d15260ee69 Fix signedness warnings in glib/tests/markup-escape.c
glib/tests/markup-escape.c: In function ‘main’:
glib/tests/markup-escape.c:152:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’
  152 |   for (i = 0; i < G_N_ELEMENTS (escape_tests); i++)
      |                 ^
../glib.git/glib/tests/markup-escape.c:159:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’
  159 |   for (i = 0; i < G_N_ELEMENTS (unichar_tests); i++)
      |                 ^
2020-11-13 10:12:16 +01:00
Emmanuel Fleury
fbb492b3eb Fix signedness warning in glib/tests/node.c
glib/tests/node.c: In function ‘traversal_test’:
glib/tests/node.c:214:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’
  214 |   for (i = 0; i < G_N_ELEMENTS (orders); i++)
      |                 ^
2020-11-13 10:12:16 +01:00
Emmanuel Fleury
8fc0080b5e Fix signedness warnings in glib/tests/logging.c
glib/tests/logging.c: In function ‘compare_fields’:
glib/tests/logging.c:403:17: error: comparison of integer expressions of different signedness: ‘int’ and ‘gsize’ {aka ‘long unsigned int’}
  403 |   for (i = 0; i < n1; i++)
      |                 ^
glib/tests/logging.c:405:21: error: comparison of integer expressions of different signedness: ‘int’ and ‘gsize’ {aka ‘long unsigned int’}
  405 |       for (j = 0; j < n2; j++)
      |                     ^
glib/tests/logging.c:410:13: error: comparison of integer expressions of different signedness: ‘int’ and ‘gsize’ {aka ‘long unsigned int’}
  410 |       if (j == n2)
      |             ^~
2020-11-13 10:12:16 +01:00
Emmanuel Fleury
b3244bee0c Fix signedness warning in glib/tests/hash.c
glib/tests/hash.c: In function ‘test_hash_misc’:
glib/tests/hash.c:616:43: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
  616 |   if (values_len != keys_len &&  keys_len != g_hash_table_size (hash_table))
      |                                           ^~
2020-11-13 10:12:16 +01:00
Emmanuel Fleury
e801143110 Fix signedness warning in glib/test/tree.c
glib/tests/tree.c: In function ‘test_tree_traverse’:
glib/tests/tree.c:394:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’
  394 |   for (i = 0; i < G_N_ELEMENTS (orders); i++)
      |                 ^
2020-11-13 10:12:16 +01:00
Michael McClurg
234a8cdea0 Fix broken link syntax in g_vasprintf 2020-11-12 03:25:58 +00:00
Patrick Griffis
9da213ea34 docs: Add note about uri normalization for equality 2020-11-06 15:32:17 -06:00
Patrick Griffis
64f478dca3 guri: Add G_URI_FLAGS_SCHEME_NORMALIZE
This flag enables optional scheme-defined normalization
during parsing of a URI.
2020-11-06 15:32:17 -06:00
Patrick Griffis
482e10d3bb guri: Normalize uri segments if they are encoded
This changes it so when a segment is encoded it will be
normalized at parse time which ensures its valid and
it can more easily be compared with other uris.
2020-11-04 10:55:04 -06:00
Philip Withnall
add3be2086 gscanner: Avoid undefined behaviour copying between union members
It’s technically undefined behaviour in C to copy between two
potentially-overlapping regions of memory (just like it is when calling
`memcpy()`). This can easily happen with union members; and the ones in
`GScanner` in particular.

Fix that by copying through an intermediate variable.

Coverity CID: #1427317, 1427340

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-11-03 16:59:36 +00:00
Simon McVittie
087fdf86a6 glib/tests/fileutils: Fix expectations when running as root
This test asserts that g_file_set_contents_full() will fail when used
on a read-only file, but processes with CAP_DAC_OVERRIDE on Linux or
uid 0 on traditional Unix can and do ignore DAC permissions.
See also #2027, #2028.

Bug-Debian: https://bugs.debian.org/973271
Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-10-31 12:26:44 +00:00
Simon McVittie
c63da52bec glib/tests/fileutils: Make more use of g_assert_no_errno()
Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-10-31 12:20:32 +00:00
Philip Withnall
159a9c215a gio: Fix various typos of the name ‘D-Bus’
This introduces no functional changes.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-26 14:28:15 +00:00
Claudio Saavedra
96ccf06d3d gmain: g_main_context_check() can skip updating polled FD sources
If there is a file descriptor source that has a lower priority
than the one for sources that are going to be dispatched,
all subsequent file descriptor sources (internally sorted by
file descriptor identifier) do not get an update in their GPollRec
and later on wrong sources can be dispatched.

Fix this by first finding the first GPollRec that matches the current
GPollFD, instead of relying on it to be the current one. At
the same time, document the assumptions about the ordering of the
file descriptor records and array and make explicit in the documentation
that the array needs to be passed to g_main_context_check() as it was
received from g_main_context_query().

Added a new test that reproduces the bug by creating two file
descriptor sources and an idle one. Since the first
file descriptor created has a lower identifier and a low priority,
the second one is not dispatched even when it has the same, higher,
priority as the idle source. After fixing this bug, both
higher priority sources are dispatched as expected.

While this patch was written independently, a similar fix for this
bug was first submitted by Eugene M in GNOME/glib!562. Having a
second fix that basically does the same is a reassurance that we
are in the right here.

Fixes #1592
2020-10-26 13:08:01 +00:00
Sebastian Dröge
5339082bbb Merge branch 'improve-g_strrstr_len-docstring' into 'master'
Improve docstrings of 'g_strstr_len' and 'g_strrstr_len'

Closes #2223

See merge request GNOME/glib!1697
2020-10-26 10:51:21 +00:00
Reuben Thomas
3b10a07126 Improve docstrings of 'g_strstr_len' and 'g_strrstr_len' (fixes: #2223)
glib/gstrfuncs.c: clarify the functions’ ability to process
non-nul-terminated strings with a negative 'haystack_length' argument.
2020-10-26 09:26:03 +00:00
Emmanuele Bassi
2d812f1ee1 Merge branch 'clang-fixes' into 'master'
gtrace: Add G_GNUC_PRINTF annotation

See merge request GNOME/glib!1718
2020-10-23 14:42:49 +00:00
Philip Withnall
30782c4c3c gtrace: Add G_GNUC_PRINTF annotation
This allows compilers to check the format placeholders properly. It
fixes compilation on clang, which gives a warning about untrusted
strings being passed on to subsequent functions which require format
placeholders.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-23 12:07:02 +01:00
Philip Withnall
dce24dc449 gmacros: Use __typeof__ when compiling with Clang
Just like gcc, clang has supported `__typeof__` for a long time, so
allow it to be used. This fixes compilation of `gio/gcredentials.c` on
macOS (which uses clang by default).

I don’t know which version clang started supporting `__typeof__` in, so
there’s no version check. One can be added in future if there are
problems.

This fixes commit 5b2bee3f53.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-23 12:04:04 +01:00
Philip Withnall
1655dc988c Revert "Use C++11 decltype where possible"
This reverts commit 0d81443ec0.

It breaks WebKit and GJS, so should be reverted for now. These changes
may be reintroduced if a solution can be found for the breakage.

See https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1575#note_940048
2020-10-21 10:51:14 +01:00
Philip Withnall
da2be2e201 Merge branch 'missing-nullable' into 'master'
Add various missing nullable annotations

See merge request GNOME/glib!1706
2020-10-19 11:47:49 +00:00
Sebastian Dröge
c686e1a048 Add various missing nullable annotations 2020-10-19 13:28:46 +03:00
Sebastian Dröge
7b73546ae5 Merge branch 'scan-build-warnings' into 'master'
Fix various minor scan build warnings

See merge request GNOME/glib!1696
2020-10-19 10:26:12 +00:00
Emmanuele Bassi
b5429b7e30 Merge branch 'six-days-to-eom' into 'master'
Fix the 6-days-until-the-end-of-the-month bug

Closes #2215

See merge request GNOME/glib!1683
2020-10-16 13:47:22 +00:00
Руслан Ижбулатов
da00779093 Fix the 6-days-until-the-end-of-the-month bug
The addition causes the date to shift
forward into 1st of the next month, because a 0-based offset
is compared to be "more than" the days in the month instead of "more than
or equal to".

This is triggered by corner-cases where transition date is 6 days
off the end of the month and our calculations put it at N+1th day of the
month (where N is the number of days in the month). The subtraction should
be triggered to move the date back a week, putting it 6 days off the end;
for example, October 25 for CET DST transition; but due to incorrect comparison
the date isn't shifted back, we add 31 days to October 1st and end up
at November 1st).

Fixes issue #2215.
2020-10-16 13:00:49 +00:00
Руслан Ижбулатов
411aa46401 Add a test for the 6-days-until-EOM bug 2020-10-16 13:00:38 +00:00
Philip Withnall
e591d2e4cc Merge branch '2225-slice-getenv-win32' into 'master'
gslice: Inline win32 implementation of g_getenv() to avoid deadlock

Closes #2225

See merge request GNOME/glib!1698
2020-10-16 11:22:02 +00:00
Philip Withnall
ed00ee3c9e Merge branch 'wip/antoniof/fallback-timezone-cache-lookup' into 'master'
Lookup fallback time zones in the cache to improve performance

Closes #2224 and #2204

See merge request GNOME/glib!1661
2020-10-15 14:54:04 +00:00
Philip Withnall
59add5ecc1 tests: Add a basic test for GTimeZone caching
This tests the previous few commits.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-15 14:43:46 +01:00
António Fernandes
36e7b48ad5 Revert "gtimezone: Cache timezones based on the identifier they were created by"
This reverts commit 851241f19a.

That commit avoids a performance regression but introduces a behavior regression:
changes to /etc/localtime have no effect for the remaining of the application's
runtime.

With the optimization introduced by the previous commit, we can pass NULL to
g_time_zone_new() repeatedly with no performance drawback, so we no longer have
to workaround this case.

Fixes: #2224
2020-10-15 14:43:46 +01:00
António Fernandes
25e634b26b gtimezone: Cache default timezone indefinitely
We cache GTimeZone instances to avoid expensive construction when the
same id is requested again.

However, if the NULL id is passed to g_time_zone_new(), we always
construct a new instance for the default/fallback timezone.

With the recent introduction of some heavy calculations[1], repeated
instance construction in such cases has visible performance impact in
nautilus list view and other such GtkTreeView consumers.

To avoid this, cache reference to a constructed default timezone and
use it the next time g_time_zone_new() is called with NULL argument,
as long as the default identifier doesn't change. We already did the
same for the local timezone[2].

Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/2204

Based on idea proposed by Sebastian Keller <skeller@gnome.org>.

[1] 25d950b61f
[2] 551e83662d
2020-10-15 14:43:46 +01:00
Philip Withnall
b538cb0c8c gslice: Inline win32 implementation of g_getenv() to avoid deadlock
The win32 implementation of `g_getenv()` uses GSlice (from within
GQuark), which results in a deadlock when examining the `G_SLICE`
environment variable.

Fix that by inlining a basic implementation of `g_getenv()` at that call
site.

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

Fixes: #2225
2020-10-15 10:43:28 +01:00
Philip Withnall
e2e8339e0a Merge branch 'typeof' into 'master'
Use C++11 decltype where possible

See merge request GNOME/glib!1575
2020-10-15 08:52:47 +00:00
Xavier Claessens
0d81443ec0 Use C++11 decltype where possible
There are various places glib uses __typeof__ for type safety, but
that's a GNUC extension. C++11 has standard decltype() that does a
similar job, at least for cases we care about.

This avoids C++ code to always have to cast return value of
g_object_ref() which was causing type kind of error:

error: invalid conversion from ‘gpointer’ {aka ‘void*’} to
‘GstElementFactory*’ {aka ‘_GstElementFactory*’} [-fpermissive]
2020-10-14 14:52:41 -04:00
Xavier Claessens
3028e6a967 gmem.h: Simplify condition where typeof is available 2020-10-14 14:52:32 -04:00
Xavier Claessens
a1847d0d03 gatomic: Check if glib_typeof() is defined before using it 2020-10-14 14:48:40 -04:00
Xavier Claessens
5b2bee3f53 Replace __typeof__ with glib_typeof macro
g_has_typeof macro is wrongly in the public g_ namespace, internaly
symbols are usually in the glib_ namespace. This will also allow to
define glib_typeof differently on non-GNUC compilers (e.g. c++11
decltype).
2020-10-14 14:48:36 -04:00
Emmanuel Fleury
dfa4907072 Fix signedness warning in glib/tests/fileutils.c
glib/gtestutils.h:134:96: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘GFileError’
  134 |                                                if (!err || (err)->domain != dom || (err)->code != c) \
      |                                                                                                ^~
glib/tests/fileutils.c:1072:15: note: in expansion of macro ‘g_assert_error’
 1072 |               g_assert_error (error, G_FILE_ERROR, tests[i].expected_error);
      |               ^~~~~~~~~~~~~~
2020-10-14 14:17:09 +02:00
Emmanuel Fleury
019c6746db Fix several signedness warnings in glib/tests/array-test.c
glib/tests/array-test.c: In function ‘array_remove_index’:
glib/tests/array-test.c:388:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
  388 |   for (i = 0; i < garray->len; i++)
      |                 ^
glib/tests/array-test.c: In function ‘array_remove_index_fast’:
glib/tests/array-test.c:425:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
  425 |   for (i = 0; i < garray->len; i++)
      |                 ^
glib/tests/array-test.c: In function ‘array_remove_range’:
glib/tests/array-test.c:462:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
  462 |   for (i = 0; i < garray->len; i++)
      |                 ^
glib/tests/array-test.c: In function ‘array_sort’:
glib/tests/array-test.c:604:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
  604 |   for (i = 0; i < garray->len; i++)
      |                 ^
glib/tests/array-test.c: In function ‘array_sort_with_data’:
glib/tests/array-test.c:636:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
  636 |   for (i = 0; i < garray->len; i++)
      |                 ^
glib/tests/array-test.c: In function ‘byte_array_sort’:
glib/tests/array-test.c:1876:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
 1876 |   for (i = 0; i < gbarray->len; i++)
      |                 ^
glib/tests/array-test.c: In function ‘byte_array_sort_with_data’:
glib/tests/array-test.c:1904:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
 1904 |   for (i = 0; i < gbarray->len; i++)
      |                 ^
2020-10-14 14:17:09 +02:00
Emmanuel Fleury
d6eaa742e7 Fix signedness warnings in glib/tests/convert.c
glib/tests/convert.c:168:16: error: comparison of integer expressions
of different signedness: ‘glong’ {aka ‘long int’} and ‘size_t’
{aka ‘long unsigned int’}

  168 |   if (utf8_len == strlen (utf8))
      |                ^~

glib/tests/convert.c:309:16: error: comparison of integer expressions
of different signedness: ‘glong’ {aka ‘long int’} and ‘size_t’
{aka ‘long unsigned int’}

  309 |   if (utf8_len == strlen (utf8))
      |                ^~
2020-10-14 14:17:09 +02:00
Emmanuel Fleury
fd7f2e6c8a Fix several signedness problems in glib/tests/checksum.c
glib/tests/checksum.c:1079:27: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’}
 1079 |   for (length = 0; length <= FIXED_LEN; length++)
      |                           ^~

glib/tests/checksum.c:1103:27: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’}
 1103 |   for (length = 0; length <= FIXED_LEN; length++)
      |                           ^~
glib/tests/checksum.c:1187:27: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’}
 1187 |   for (length = 0; length <= FIXED_LEN; length++)
      |                           ^~
glib/tests/checksum.c:1192:27: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’}
 1192 |   for (length = 0; length <= FIXED_LEN; length++)
      |                           ^~
glib/tests/checksum.c:1197:27: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’}
 1197 |   for (length = 0; length <= FIXED_LEN; length++)
      |                           ^~
glib/tests/checksum.c:1202:27: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’}
 1202 |   for (length = 0; length <= FIXED_LEN; length++)
      |                           ^~
glib/tests/checksum.c:1207:27: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’}
 1207 |   for (length = 0; length <= FIXED_LEN; length++)
      |                           ^~
2020-10-14 14:17:09 +02:00
Emmanuel Fleury
5a361aeeaa Fix signedness warning in glib/tests/base64.c
glib/tests/base64.c:28:20: error: comparison of integer expressions of
different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gint’ {aka ‘int’}

   28 |   while (input_len < length)
      |                    ^
2020-10-14 14:17:09 +02:00
Philip Withnall
e9c4e19950 tests: Add additional keyfile assertions
These slightly improve the tests but, more importantly, squash a
scan-build warning about assigning to a variable which is never
subsequently used:
```
../../../glib/tests/keyfile.c:1150:3: warning: Value stored to 'value' is never read
  value = g_key_file_get_string (keyfile, "a", "key=", &error);
  ^       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../glib/tests/keyfile.c:1159:3: warning: Value stored to 'value' is never read
  value = g_key_file_get_string (keyfile, "a", "key[", &error);
  ^       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../glib/tests/keyfile.c:1176:3: warning: Value stored to 'value' is never read
  value = g_key_file_get_string (keyfile, "a", " key", &error);
  ^       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 warnings generated.
```

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-14 13:15:14 +01:00
Philip Withnall
0544efcbb4 tests: Improve signed int handling to silence scan-build warnings
This should silence the following warning:
```
../../../glib/tests/mutex.c:206:5: warning: 1st function call argument is an uninitialized value
    g_thread_join (threads[i]);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~
```

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-14 13:15:09 +01:00
Philip Withnall
12f8708928 tests: Add an assertion to avoid a scan-build warning
This should avoid the warning:
```
../../../glib/tests/mainloop.c:119:3: warning: Value stored to 'id' is never read
  id = g_source_attach (source, ctx);
  ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-14 13:06:34 +01:00
Philip Withnall
2e7d3dcf70 tests: Add some additional assertions to avoid scan-build warnings
This should avoid warnings like:
```
../../../glib/tests/regex.c:715:7: warning: Array access (from variable 'matches') results in a null pointer dereference
      g_assert_cmpstr (l_exp->data, ==, matches[i]);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../glib/gtestutils.h:43:79: note: expanded from macro 'g_assert_cmpstr'
                                             const char *__s1 = (s1), *__s2 = (s2); \
                                                                              ^~~~
../../../glib/tests/regex.c:803:7: warning: Array access (from variable 'tokens') results in a null pointer dereference
      g_assert_cmpstr (l_exp->data, ==, tokens[i]);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../glib/gtestutils.h:43:79: note: expanded from macro 'g_assert_cmpstr'
                                             const char *__s1 = (s1), *__s2 = (s2); \
                                                                              ^~~~
../../../glib/tests/regex.c:886:7: warning: Array access (from variable 'tokens') results in a null pointer dereference
      g_assert_cmpstr (l_exp->data, ==, tokens[i]);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../glib/gtestutils.h:43:79: note: expanded from macro 'g_assert_cmpstr'
                                             const char *__s1 = (s1), *__s2 = (s2); \
                                                                              ^~~~
../../../glib/tests/regex.c:918:7: warning: Array access (from variable 'tokens') results in a null pointer dereference
      g_assert_cmpstr (l_exp->data, ==, tokens[i]);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../glib/gtestutils.h:43:79: note: expanded from macro 'g_assert_cmpstr'
                                             const char *__s1 = (s1), *__s2 = (s2); \
                                                                              ^~~~
```

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-14 13:04:44 +01:00
Philip Withnall
271db1f409 ghash: Move initialisation to declaration
This introduces no functional changes, but should squash a warning from
`scan-build`:
```
../../../glib/ghash.c:575:3: warning: Value stored to 'small' is never read
  small = FALSE;
```

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-14 12:58:25 +01:00
Robert Ancell
b2cc8d1740 gstrvbuilder: Add a new object to make NULL-terminated string arrays.
GLib uses NULL-terminated string arrays (GStrv) in a number of places, however
these are quite hard to construct in C when the number of elements is not known
in advance. GStrvBuilder wraps GPtrArray to make these easy to create with
type safety and does the memory management for you.
2020-10-14 11:01:24 +00:00
Benjamin Berg
bf814a9aa3 gmain: Fix possible locking issue in source unref
When unref'ing child sources, the lock is already held. But instead of
passing TRUE to g_source_unref_internal it currently passes whether the
lock was already held outside of the current invocation. Just pass TRUE
to fix this possible issue.
2020-10-13 15:12:37 +02:00
Sebastian Dröge
88deca52c0 Merge branch 'uri-nullable' into 'master'
uri: add missing (not)nullable annotations

See merge request GNOME/glib!1687
2020-10-13 07:39:44 +00:00
Marc-André Lureau
2306f96fb0 uri: add missing (not)nullable annotations
As suggested by Sebastian Dröge:
https://github.com/gtk-rs/glib/pull/697#pullrequestreview-505797722

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-10-13 09:59:28 +03:00
Philip Withnall
9f8ccee65f gspawn: Use close_range() if available to close FDs between fork/exec
It’s landed in kernel 5.9: http://lkml.iu.edu/hypermail/linux/kernel/2008.0/02649.html

Note, this is untested because I currently don’t have kernel 5.9. We can
fix anything up if it breaks once the new syscall is wrapped in glibc.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-12 18:10:45 +01:00
Sebastian Dröge
6cf381ee75 Merge branch 'spawn-error-handling' into 'master'
gspawn: Handle error opening /dev/null

See merge request GNOME/glib!1681
2020-10-07 13:48:36 +00:00
Simon McVittie
b17b537a7d gmessages: Expose our default filtering as API
This allows programs that want to change how log messages are printed,
such as gnome-terminal (gnome-terminal#42) and Flatpak, to override
the log-writer or the legacy log-handler without having to reimplement
the G_MESSAGES_DEBUG filtering logic.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-10-07 14:04:08 +01:00
Simon McVittie
10b0ece9d8 gmessages: Add API to move info and debug messages to stderr
GLib code normally prints info and debug messages to stdout,
but that interferes with programs that are documented to produce
machine-readable output such as JSON or XML on stdout. In particular,
if such a program uses a GLib-based library, setting G_MESSAGES_DEBUG
will typically result in that library's debug messages going to the
program's stdout and corrupting the machine-readable output.

Unix programs can avoid this by using dup2() to move the original stdout
to another fd, then dup2() again to make the new stdout a copy of stderr,
but it's easier if we provide a way to not write debug messages to
stdout in the first place. Calling
g_log_writer_default_set_use_stderr (TRUE) results in behaviour
resembling Python's logging.basicConfig(), with all diagnostics going
to stderr.

Suggested by Allison Karlitskaya on glib#2087.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-10-07 14:03:50 +01:00
Philip Withnall
7bfd6278b9 Merge branch 'gtree-add-iterators' into 'master'
GTree: add an ability to iterate over a tree and a node-based API

See merge request GNOME/glib!1509
2020-10-06 13:43:18 +00:00
Philip Withnall
78e1431816 Merge branch 'log-fatal' into 'master'
gmessages: Document that using a custom log writer basically disables fatal handling

See merge request GNOME/glib!1678
2020-10-06 10:23:24 +00:00
Maciej S. Szmigiero
2e7931c760 GTree: add an ability to iterate over a tree and a node-based API
The basic API that this commit adds allows in-order iterating over a GTree.

For this the following API were implemented or exported:
1) Returning the first or the last node in the tree,

2) Taking a pointer to a node in the tree and returning the previous or the
next in-order node,

3) Allowing to do a binary search for a particular key value and returning
the pointer to its node,

4) Returning the newly inserted or set node from both insert and replace
functions, so this node is immediately available and does not have to be
looked up,

5) Traversing the tree in-order providing a node pointer to the
caller-provided traversal function.

Most of the above functions were already present in the code, but they
returned the value that is stored at a particular node instead of the
pointer to the node itself.

So most of the code for these new API calls is shared with these existing
ones, just adapted to return the pointer to the node.

Additionally, the so called "lower bound" and "upper bound" operations
were implemented.

The first one returns the first element that is greater than or equal to
the searched key, while the second returns the first element that is
strictly greater than the searched key.

Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com>
2020-10-06 11:07:11 +01:00
Maciej S. Szmigiero
260e80f1df GTree: debugging improvements
* Add a debug assert checking whether we have really removed all nodes in
g_tree_remove_all(),

* Print a "LEFT" and "RIGHT" headers before printing a particular tree
branch in g_tree_node_dump(),

* Make the whole thing actually buildable again in the debug mode by
conditionally providing g_tree_dump() declaration in glib/gtree.h.

Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com>
2020-10-06 11:03:18 +01:00
Guido Günther
b4023163ee gmessages: Document that using a custom log writer disables fatal handling
Document that structured logging interwines fatal handling and logging
so replacing the log handler can have undesired effects.
2020-10-06 11:50:41 +02:00
Philip Withnall
f53842a9c0 guri: Add additional tests for scope ID parsing
These bump up the code coverage.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-05 13:53:02 +01:00
Philip Withnall
a4cba75581 guri: Fix UTF-8 validation when escaping URI components
The return value from `g_utf8_get_char_validated()` is a `gunichar`,
which is unsigned, so comparing it with `> 0` is always going to return
true, even for return values `(gunichar) -1` and `(gunichar) -2`, which
indicate errors.

Handle them more explicitly.

oss-fuzz#26083

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-05 13:53:02 +01:00
Philip Withnall
4d00344e1f tests: Refactor g_uri_escape_string() tests
This will allow more tests to be added easily in future. It introduces
no functional changes.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-05 13:53:02 +01:00
Philip Withnall
f0e74a97e7 gspawn: Handle error opening /dev/null
This is very unlikely to happen, but add error handling to mirror the
other calls to `safe_open()`, and shut Coverity up.

Coverity CID: #1430611

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-05 12:31:28 +01:00
Philip Withnall
a82556c98d gmain: Fix minor typo in documentation
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-05 11:41:40 +01:00
Simon McVittie
fca9824978 glib-unix: Assert that our portable types correspond to ssize_t and pid_t
If this fails to compile on some particularly bizarre Unix platform,
we can relax these assertions; but our expectation is that gssize is
POSIX ssize_t, and that on Unix, GPid is POSIX pid_t.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-10-02 15:45:44 +01:00
Simon McVittie
bd1e2a984e glib-init: Statically assert more facts about standard types
This is a step towards glib#1484. We officially require a C99 toolchain,
so we can statically assert that our artisanal hand-crafted integer
types are compatible with the ones we would like to recommend people
use instead.

If there are *still* platforms where <stdint.h> is problematic, these
static assertions can act as an early-warning that future GLib releases
will make a C99-compliant <stdint.h> a hard requirement, in ways that
are less straightforward to avoid (see glib#1484 and glib!1300).

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-10-02 15:45:40 +01:00
António Fernandes
7e59a4c0d5 gtimezone: Set resolved_identifier earlier
We have been passing a &resolved_identifier address around for multiple
functions to set it. Each function may either:

    1.  leaving it for the next function to set, if returning early;
    2.  set it to a duplicate of the passed identifier, if not NULL;
    3.  get a fallback value and set it, otherwise.

This can be simplified by setting it early to either:

    1.  a duplicate of the passed identifier, if not NULL;
    2.  a fallback value, otherwise.

This way we can avoid some unnecessary string duplication and freeing.
Also, on Windows, we avoid calling windows_default_tzname() twice.

But the main motivation for this change is enabling the performance
optimization in the next commit.
2020-10-02 00:02:57 +01:00
António Fernandes
b4138bd4ac gtimezone: Split out fallback timezone identification for unix
When the TZ environment variable is not set, we get the local timezone
identifier by reading specific files.

We are going to need these identifiers earlier, so split this logic into
its own function, in preparation for the next commit.

Based on idea proposed by Sebastian Keller <skeller@gnome.org>.
2020-10-01 21:46:44 +01:00
Emmanuele Bassi
51a135a272 Merge branch 'version-bump-2.68' into 'master'
Add version macros for 2.68

See merge request GNOME/glib!1674
2020-10-01 15:30:47 +00:00
Philip Withnall
06dabe05b1 Merge branch '2210-private-replace-destroy' into 'master'
gthread: Destroy value after replacing it in g_private_replace()

Closes #2210

See merge request GNOME/glib!1667
2020-10-01 13:43:52 +00:00
Philip Withnall
c51a8ce8c8 gversionmacros: Add version macros for GLib 2.68
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-01 14:33:21 +01:00
Philip Withnall
281f6697c1 tests: Test date overflow failure with bookmark file parsing
This is exactly the test case from oss-fuzz which triggers a negative
overflow when constructing dates.

oss-fuzz#22758

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-01 11:46:23 +01:00
Philip Withnall
b5656d2524 gdatetime: Avoid integer overflow creating dates too far in the past
oss-fuzz#22758

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-10-01 11:46:15 +01:00
Philip Withnall
6762312ff2 tests: Add additional URI scope parsing tests
This bumps the coverage of `parse_host()` and `parse_ip_literal()` up to
100% of lines and branches.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-09-30 19:42:26 +01:00
Philip Withnall
d2f324545b tests: Rework test_uri_parsing_absolute to support failure tests
This introduces no tests for failed parsing *yet*, but will allow them
to be added in future.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-09-30 19:39:30 +01:00
Philip Withnall
b43fb9cbfb guri: Fix URI scope parsing
The previous parsing code could read off the end of a URI if it had an
incorrect %-escaped character in.

Fix that, and more closely implement parsing for the syntax defined in
RFC 6874, which is the amendment to RFC 3986 which specifies zone ID
syntax.

This requires reworking some network-address tests, which were
previously treating zone IDs incorrectly.

oss-fuzz#23816

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-09-30 19:39:30 +01:00
Philip Withnall
c363c3a9a5 tests: Remove duplicate IPv6 zone ID URI parsing tests
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-09-30 19:39:09 +01:00
Philip Withnall
7e400a886e guri: Refactor error handling in parse_ip_literal()
Having the goto labels at the bottom of a function makes things a little
more readable. This introduces no functional changes.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-09-30 19:39:09 +01:00
Philip Withnall
17a53f2fc7 guri: Simplify memory management in parse_host()
This introduces no functional changes, but makes the memory ownership a
little clearer and reduces the length of the code.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-09-30 17:46:27 +01:00
Philip Withnall
58fce4b92b guri: Move IP-literal parsing out into a separate function
This introduces no functional changes, but will make future changes to
the code a little cleaner.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-09-30 17:45:19 +01:00
Philip Withnall
8c76bec779 gthread: Destroy value after replacing it in g_private_replace()
If the old value is destroyed before updating the TLS value in pthreads
(or the Windows equivalent) then there’s a risk of infinite recursion if
`g_private_replace()` is called from within the `GDestroyNotify`.

Avoid that by destroying the old value after doing the TLS update.

Thanks to Matthias Clasen for diagnosing the issue.

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

Fixes: #2210
2020-09-30 16:16:11 +01:00
Philip Withnall
53819d8a37 Merge branch 'list-free-docs' into 'master'
glist: Clarify that g_list_free() and friends only free an entire list

See merge request GNOME/glib!1657
2020-09-23 12:42:07 +00:00
Sebastian Dröge
45758e04b7 Merge branch '2204-timezone-caching' into 'master'
gtimezone: Cache timezones based on the identifier they were created by

Closes #2204

See merge request GNOME/glib!1660
2020-09-23 10:50:11 +00:00
Philip Withnall
851241f19a gtimezone: Cache timezones based on the identifier they were created by
Rather than invalidating the cache by comparing `TZ` to the cached
timezone identifier, key entirely off the value of `TZ` (and a cached
copy of it).

This fixes the timezone cache being constantly invalidated if `TZ` is
`NULL` (which will always differ from the identifier of the default
local timezone which is constructed from `g_time_zone_new (NULL)`.

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

Fixes: #2204
2020-09-23 11:28:32 +01:00
Peter Bloomfield
e64293d56c utils: Limit the scope of the variable max
in g_get_host_name(), and avoid an unused-variable warning when
_SC_HOST_NAME_MAX is not defined.
2020-09-22 16:14:21 -04:00
Philip Withnall
eb19c328f3 glist: Clarify that g_list_free() and friends only free an entire list
They shouldn’t be used to free a list from part-way through, as that
is confusing and will leave a dangling pointer from the previous list
element.

Spotted by Gary Kramlich in !1653.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-09-22 16:41:55 +01:00
Wolfgang Stöggl
7e27162874 Define G_MSVC_SYMBOL_PREFIX correctly for ARM32
Symbols on x86 are prefixed with an underscore
but symbols on x64/ARM/ARM64 are not.

Relevant information concerning the prefixes for the architectures
can be found in the vcpkg project [1,2], where arm and arm64 builds
are part of the CI.

Specifically, _WIN64 is defined on Windows ARM64, so this issue is
only visible when building on ARM32.

[1] 08e74979df/ports/glib/fix-arm-builds.patch
[2] https://github.com/microsoft/vcpkg/pull/6116
2020-09-18 23:50:09 +05:30
Emmanuel Fleury
16773d2702 Fix a signedness problem in glib/tests/date.c
glib/tests/date.c:778:17: error: comparison of integer expressions of
  different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’

  778 |   for (i = 0; i < G_N_ELEMENTS (check_years); i++)
      |                 ^
2020-09-14 10:11:44 +02:00
Emmanuel Fleury
33148e6bc3 Fix a signedness problem in glib/tests/collate.c
glib/tests/collate.c:300:17: error: comparison of integer expressions
  of different signedness: ‘gint’ {aka ‘int’} and ‘long unsigned int’

  300 |   for (i = 0; i < G_N_ELEMENTS (test); i++)
      |                 ^
2020-09-14 10:11:44 +02:00
Emmanuel Fleury
d49586cfa3 Fix signedness error in glib/gspawn.c
glib/gspawn.c:2252:16: error: comparison of integer expressions of
  different signedness: ‘int’ and ‘gsize’ {aka ‘long unsigned int’}

 2252 |   if (argc + 2 > argv_buffer_len)
      |                ^
2020-09-14 10:11:44 +02:00
Emmanuel Fleury
33bfae70ac Fix signedness problem in glib/gutils.c
glib/gutils.c:998:26: error: comparison of integer expressions of
 different signedness: ‘glong’ {aka ‘long int’} and ‘long unsigned int’
  998 |       if (max > 0 && max <= G_MAXSIZE - 1)
      |                          ^~
2020-09-14 10:11:44 +02:00
Philip Withnall
c3eda78ada Merge branch '2194-winhttp-file-display-name' into 'master'
gwinhttpfile: Set display-name attribute on file info

Closes #2194

See merge request GNOME/glib!1644
2020-09-11 09:49:04 +00:00
Philip Withnall
9d859f001d gutf8: Fix a typo in the docs for g_utf16_to_utf8()
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-09-10 14:14:32 +01:00
Sebastian Dröge
b7c75f78ab Merge branch '2077-pointer-arithmetic' into 'master'
gfileutils: Correct operator precedence to avoid undefined pointer maths

Closes #2077

See merge request GNOME/glib!1526
2020-09-10 11:22:44 +00:00
Philip Withnall
f19cb44b98 guri: Remove unnecessary NULL pointer check
`uri` is always non-`NULL` by the time the `fail` label is reached, so
drop the `NULL` pointer check. Inline the `fail` code since it’s only
used from two places.

Coverity CID: #1430970
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-09-08 09:59:24 +01:00
Patrick Griffis
8b319a687b guri: Fix user passed to g_uri_split_with_user() not being NULL'd 2020-09-02 15:43:58 -05:00
Sebastian Dröge
054ef4ddf7 Merge branch '2191-scheduler-warnings' into 'master'
gthread: Only print scheduler setting warnings once

Closes #2191

See merge request GNOME/glib!1637
2020-09-02 15:28:17 +00:00
Philip Withnall
c8840ff9a8 gthread: Only print scheduler setting warnings once
If one thread pool thread fails to set its scheduler settings, it’s
likely that all the rest of them will fail for the same reason. Avoid
printing duplicate critical warnings in that case.

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

Fixes: #2191
2020-09-02 16:09:12 +01:00
Philip Withnall
eba91d5c87 Merge branch 'big_O_notations' into 'master'
Big o notations

See merge request GNOME/glib!1025
2020-09-02 13:09:52 +00:00
Emmanuel Fleury
d27549b0f4 Add some notes on complexity in glib/gtree.c
Related to issue #3
2020-09-02 14:38:15 +02:00
Emmanuel Fleury
54c20c8532 Add some notes on complexity in glib/gsequence.c
Related to issue #3
2020-09-02 14:38:15 +02:00
Emmanuel Fleury
a62fcb9af1 Add some notes on complexity in glib/gqueue.c
Related to issue #3
2020-09-02 14:38:15 +02:00
Emmanuel Fleury
69e5e12c95 Add some notes on complexity in glib/gslist.c
Related to issue #3
2020-09-02 14:38:15 +02:00
Emmanuel Fleury
4d9cd832d1 Add some notes on complexity in glib/glist.c
Related to issue #3
2020-09-02 14:38:15 +02:00
Emmanuel Fleury
cbae555a98 Add some notes on complexity in glib/ghash.c
Related to issue #3
2020-09-02 14:38:10 +02:00
Emmanuel Fleury
34f03f01c8 Add some notes on complexity in glib/garray.c
Related to issue #3
2020-09-02 14:36:22 +02:00
Philip Withnall
63c5b62f0a goption: Add a precondition to avoid GOptionEntry list overflow
If the calling code adds more option entries than `G_MAXSIZE` then
there’ll be an integer overflow. This seems vanishingly unlikely (given
that all callers use static option entry lists), but add a precondition
anyway.

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

Fixes: #2197
2020-09-02 12:48:37 +01:00
Sebastian Dröge
dae128e6bb Don't pass more than G_MAXSSIZE bytes at once to write() in glib/gfileutils.c
Behaviour in that case is implementation-defined and how many bytes were
actually written can't be expressed by the return value anymore.

Instead do a short write of G_MAXSSIZE bytes and let the existing loop
for handling short writes takes care of the remaining length.
2020-08-27 19:32:14 +03:00
Emmanuel Fleury
856265fe66 Fixing signedness warning in glib/gfileutils.c
glib/gfileutils.c: In function ‘write_to_file’:
glib/gfileutils.c:1176:19: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare]
 1176 |       g_assert (s <= length);
      |                   ^~
glib/gmacros.h:939:25: note: in definition of macro ‘G_LIKELY’
  939 | #define G_LIKELY(expr) (expr)
      |                         ^~~~
glib/gfileutils.c:1176:7: note: in expansion of macro ‘g_assert’
 1176 |       g_assert (s <= length);
      |       ^~~~~~~~

Related to issue #1735 (Get back to a -werror build)
2020-08-27 19:32:11 +03:00
Philip Withnall
153ec62e7e Merge branch 'wip/baedert/fallthrough' into 'master'
Replace a bunch of fallthrough comments with an attribute

See merge request GNOME/glib!1628
2020-08-21 08:49:11 +00:00
Timm Bäder
aeed676c08 Replace a bunch of fallthrough comments with an attribute
Parsing comments is hack and clang doesn't do it, so switch to
G_GNUC_FALLTHROUGH, which exists for exactly this case.
2020-08-21 05:50:02 +02:00
Matthias Clasen
5620828f47 gthread: Add a sysprof mark for thread creation
sysprof already shows forks in the waterfall. The
main benefit of adding a mark is that it makes the
thread name show up in the trace, next to the fork.
2020-08-20 15:23:05 -04:00
Philip Withnall
b0be67cc3f gdatetime: Widen a variable before multiplication
Otherwise it could possibly overflow on 32-bit machines if `year` is
high enough, although I don’t think that’s possible because of limits
applied on it by callers. This should shut Coverity up though.

The limits applied by callers could be circumvented by calling (say)
`g_date_time_add_years()` multiple times. That’s a bug, but not one I’m
going to fix today.

Coverity CID: #1159479
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-18 10:23:43 +01:00
Sebastian Dröge
c50fb4f317 Merge branch 'variant-depth-init' into 'master'
gvariant: Ensure GVS.depth is initialised

See merge request GNOME/glib!1620
2020-08-18 09:11:34 +00:00
Philip Withnall
a5d34a7c78 gvariant: Ensure GVS.depth is initialised
When byteswapping the depth was accidentally left uninitialised.

Coverity CID: #1430636
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-18 09:46:12 +01:00
Philip Withnall
0caa00b349 guri: Add an assertion to help static analysis
This might eliminate some false positives being thrown by Coverity to
do with the return value of `uri_decoder()` and whether it’s allocated
anything.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-18 09:33:42 +01:00
Philip Withnall
b639687b60 gtestutils: Fix a minor memory leak
Coverity CID: #1430603
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-18 09:33:06 +01:00
Sebastian Dröge
fb6da5677b Merge branch 'wip/pwithnall/coverity' into 'master'
CI support for Coverity Scan

See merge request GNOME/glib!1593
2020-08-17 11:57:24 +00:00
Philip Withnall
056aaf36f8 Merge branch 'master' into 'master'
gdatetime: Add %f format specifier and microsecond precision to g_date_time_format_iso8601

See merge request GNOME/glib!1605
2020-08-12 13:37:06 +00:00
Philip Withnall
d31c771185 Merge branch 'wip/alloc-memset-inlining' into 'master'
gslice: Allow inlining of memset() in g_slice_new0() macro

Closes #5

See merge request GNOME/glib!1597
2020-08-12 13:29:27 +00:00
Johan Bjäreholt
8e13683f70 gdatetime: Format iso8601 strings with microsecond precision 2020-08-12 15:07:40 +02:00
Johan Bjäreholt
bcb48a18e3 gdatetime: Fix alphabetical order of format specifiers 2020-08-12 15:07:40 +02:00
Johan Bjäreholt
3b0eca5be6 gdatetime: Add %f format specifier for microseconds
Same specifier used as in pythons datetime.strptime

Change-Id: Ic15a63e3c83674b2e8a05bc79ef2665738e71a5a
2020-08-12 15:07:38 +02:00
Philip Withnall
cacf9c2926 guri: Add various small new tests to increase branch coverage
Nothing particularly interesting or major.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
b654eb1846 guri: Make G_URI_FLAGS_PARSE_STRICT the default
Make `G_URI_FLAGS_PARSE_RELAXED` available instead, for the
implementations which need to handle user-provided or incorrect URIs.
The default should nudge people towards being compliant with RFC 3986.

This required also adding a new `G_URI_PARAMS_PARSE_RELAXED` flag, as
previously parsing param strings *always* used relaxed mode and there
was no way to control it. Now it defaults to using strict mode, and the
new flag allows for relaxed mode to be enabled if needed.

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

Fixes: #2149
2020-08-07 14:02:18 +01:00
Philip Withnall
943b1e45ab guri: Don’t fail g_uri_is_valid() if URI is missing a hostname
According to my reading of
https://tools.ietf.org/html/rfc3986#section-4, the only requirement for
a URI to be ‘absolute’ (actually, not a relative reference) is for the
scheme to be specified. A hostname doesn’t have to be specified: see any
of the options in the `hier-part` production in
https://tools.ietf.org/html/rfc3986#appendix-A which don’t include
`authority`.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
b5c59cc3fc guri: Use gssize for array/string lengths
This reduces the possibility for overflow, and makes the code a little
more conventional to read.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
e446c3487b guri: Change type of g_uri_escape_bytes() to use guint8
`guint8` is the conventional way in modern GLib APIs to represent ‘a byte
which could contain arbitrary binary’. `guchar` is not advised for that
(even though it’s equivalent) because it could be misread as `gchar`.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
ceda9755de guri: Clear return values on error from g_uri_params_iter_next()
This reduces the chance of the caller accidentally double-freeing or
use-after-free-ing something.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
83597b9e57 guri: Use NONE values of flags rather than 0
This introduces no functional changes, but makes the code a little
easier to read.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
41a21c3566 guri: Add links to RFC 3986 in code comments
This should make the RFC easier to refer to in future.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
f873b88f89 guri: Add G_URI_HIDE_QUERY
Sometimes there are sensitive details in URI query components, so we
should provide the option for hiding them too.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
ae6a0ef8b8 guri: Tweak quotes in error strings
Use nice curly Unicode quotes.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
40873f8452 guri: Use g_steal_pointer() to make ownership transfer clearer
This introduces no functional changes, just makes the code a bit easier
to read.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
de0ebf8a5f guri: Minor code formatting fixes
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
1f1efbbb05 guri: Rename G_URI_ERROR_MISC to G_URI_ERROR_FAILED
This brings its naming in line with the ‘generic’ error codes in other
error domains.

This is not an API break since `GUriError` hasn’t been in a release yet.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:18 +01:00
Philip Withnall
ff60d2ebf5 guri: Various minor documentation tweaks and improvements
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 14:02:17 +01:00
Sebastian Dröge
602b7cca33 Merge branch 'uri-path-slashes' into 'master'
guri: Document and check restrictions on path prefixes

See merge request GNOME/glib!1612
2020-08-07 12:57:17 +00:00
Philip Withnall
89cf298b19 guri: Document and check restrictions on path prefixes
RFC 3986, section 3 says:
> The scheme and path components are required, though the path may be
> empty (no characters).  When authority is present, the path must
> either be empty or begin with a slash ("/") character.  When
> authority is not present, the path cannot begin with two slash
> characters ("//").  These restrictions result in five different ABNF
> rules for a path (Section 3.3), only one of which will match any
> given URI reference.

(See https://tools.ietf.org/html/rfc3986#section-3.)

Given that those conditions are almost always going to be true, and that
typically the number and form of arguments passed to g_uri_join() will
be known at compile time, it would be unnecessarily awkward to add a
`GError` argument to g_uri_join() to detect these situations.

Instead, add precondition checks and document the restrictions.
Developers are responsible for ensuring their paths are in the right
format themselves.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 13:24:12 +01:00
Philip Withnall
623cb10f55 guri: Always prepend // to the host when building a URI
This is needed to distinguish the host (‘authority’ in the terms of RFC
3986) from a path if a scheme is not present.

It can be seen from the grammar in RFC 3986
(https://tools.ietf.org/html/rfc3986#appendix-A) that `authority` only
ever appears after `"//"`.

Spotted by Simon McVittie in
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1606#note_884893.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-07 12:51:31 +01:00
Felix Yan
161168c672 Fix multiple typos in guri.c 2020-08-06 14:18:01 +00:00
Marc-André Lureau
0ba7ebfda9 uri: allow to join a partial URI, without scheme
Fixes: #2166

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-08-05 22:40:08 +04:00
Philip Withnall
bb1df0e515 Merge branch 'uri-params-iter' into 'master'
Add GUriParamsIter

See merge request GNOME/glib!1572
2020-08-05 16:07:42 +00:00
Philip Withnall
df8dc7fc38 Merge branch 'guri-gio' into 'master'
Replace _g_uri_parse_authority() with GUri

Closes #2156

See merge request GNOME/glib!1567
2020-08-05 16:06:02 +00:00
Marc-André Lureau
3bcc6fd39f guri: add a test to check ipv6 with zoneid URI to string
The test was failing since commit 20ae4b46d ("uri: do not add ipv6
brackets on non-ip host").

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-08-05 18:50:13 +04:00
Marc-André Lureau
03550ec097 hostutils: accept zoneid in IPv6 addresses
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-08-05 18:50:13 +04:00
Marc-André Lureau
5767eef895 uri: add GUriParamsIter
See also:
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1328#note_863735
2020-08-04 20:10:57 +04:00
Sebastian Dröge
50343afb6e Merge branch 'uri-userinfo-enc' into 'master'
uri: do not encode ':' and ';' from userinfo

See merge request GNOME/glib!1600
2020-08-04 13:33:18 +00:00
Marc-André Lureau
ef173e2e75 uri: do not encode ':' and ';' from userinfo
The g_uri_join_internal() function was making a simplification that
userinfo can be encoded with the same restricted character set as the
user field alone, fix this by allowing the correct character set.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-08-04 17:10:06 +04:00
Marc-André Lureau
c354c404c6 tests/uri: check user field is correctly encoded
Suggested-by: Dan Winship <danw@gnome.org>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-08-04 17:10:06 +04:00
Marc-André Lureau
4c20ea477c uri: always add G_URI_FLAGS_HAS_PASSWORD with build_with_user()
Otherwise, the to_string() encoding will not be reversible. Furthermore,
if no distinction is needed in the first place, g_uri_build() with
userinfo should be used instead.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-08-04 17:10:06 +04:00
Marc-André Lureau
b0f9af0e1d uri: do not encode userinfo fields
g_uri_build_with_user() builds a userinfo, but it shouldn't encode it
itself, but let the user flags declare what's there. Otherwise,
to_string() code paths may encode a second time.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-08-04 17:03:30 +04:00
Marc-André Lureau
c9c349aeaa uri: add ENCODED_PATH & ENCODED_FRAGMENT flags
Add encoded flags, similar to what was done in commit 7bee36b4 ("uri:
add G_FLAGS_ENCODED_QUERY").

SoupURI has manual handling of encoded path & fragment, but it can rely
on GUri decoding for the rest.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-08-04 16:56:19 +04:00
Sebastian Dröge
a97fe4e863 Merge branch 'uri-ipv6-bkt' into 'master'
uri: do not add ipv6 brackets on non-ip host

See merge request GNOME/glib!1599
2020-08-04 12:44:12 +00:00
Matthias Clasen
0992978202 timezone: Fix an uninitialized use
GTK ci rightly complains about this when ti builds
GLib as a subproject with -Werror=maybe-uninitialized.

subkey_dynamic_w will be freed without being initialized
when the first goto is taken.
2020-07-31 14:04:21 -04:00
Marc-André Lureau
20ae4b46d4 uri: do not add ipv6 brackets on non-ip host
The heuristic is a bit too agressive, as we may have hostname with
%-encoded ':' (as shown in GVfs URI tests).

Add an extra test to check :-decoding as well.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-30 21:37:34 +04:00
Hans Petter Jansson
4c5ccd3e8f gslice: Allow inlining of memset() in g_slice_new0() macro
Inlining the memset() is beneficial since the size here is constant
and usually small.

Closes #5
2020-07-28 18:22:21 +02:00
Marc-André Lureau
0fea3d71e4 guri: add some IPv6 scope-id tests
Add a few ipv6 scope parsing corner test cases.

- checking incorrect scoped IPv6 ending with only %25 isn't decoded.
- checking valid scoped IPv6 is passing g_uri_is_valid()

As discussed in
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1567#note_860499,
for historical reasons, GUri accepts the % preceding the zone-id in the
unescaped form as well.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-28 16:48:01 +04:00
Marc-André Lureau
82ad7853ba uri: change g_uri_is_valid() to check absolute URI
g_uri_is_valid() should check the given URI is valid following RFC-3986,
and reject relative references.

Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/2169

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-28 15:44:43 +04:00
Marc-André Lureau
44d4640c47 uri: rename absolute & relative uri_string to uri_ref
Let's reserve the term URI for absolute URIs, following rfc3986
terminology.

See:
https://gitlab.gnome.org/GNOME/glib/-/issues/2169

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-28 15:07:08 +04:00
Marc-André Lureau
d625a29b28 uri: add a comment about temporary GUri construction
As pointed out in the discussion of:
https://gitlab.gnome.org/GNOME/glib/-/issues/2169

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-28 14:54:55 +04:00
Marc-André Lureau
19c0db3185 uri: improve some documentation about absolute URIs
As pointed out in the discussion
https://gitlab.gnome.org/GNOME/glib/-/issues/2169.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-28 14:54:55 +04:00
Marc-André Lureau
3521763532 uri: add some note about the API scope
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-28 14:54:42 +04:00
Sebastian Dröge
9577616622 Merge branch '272-thread-pool-docs' into 'master'
gthreadpool: Mention g_get_num_processors() in the thread pool docs

Closes #272

See merge request GNOME/glib!1588
2020-07-27 19:04:11 +00:00
Philip Withnall
1203e80f5f Merge branch '250-testcase-docs' into 'master'
gtestutils: Clarify the docs for g_test_create_case()

Closes #250

See merge request GNOME/glib!1587
2020-07-27 17:01:16 +00:00
Philip Withnall
b3c0c4804b gthreadpool: Mention g_get_num_processors() in the thread pool docs
Just to make it a little more obvious that a thread pool can be
initialised with one thread per logical CPU.

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

Fixes: #272
2020-07-27 17:28:43 +01:00
Philip Withnall
07278e73d4 gtestutils: Clarify the docs for g_test_create_case()
Improve formatting, and clarify that the same *type* of test fixture can
be reused, not the same specific instance of a test fixture.

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

Fixes: #250
2020-07-27 17:26:56 +01:00
Philip Withnall
648986a290 glib: Avoid redefining GLIB_DISABLE_DEPRECATION_WARNINGS
It may be defined by the environment (we document that as being allowed)
— if so, individual files should not try to redefine it, as that causes
a preprocessor warning.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-27 15:41:09 +01:00
Philip Withnall
9c6c546236 gosxutils: Include Foundation rather than Cocoa
Apparently, the function calls added in 11729cdc use Foundation, not
Cocoa. Cocoa is a massive superset of Foundation, and is not available
on iOS.

Patch contributed by Jay Freeman, but without providing an e-mail
address. So the git commit cannot be attributed to him.

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

Fixes: #1869
2020-07-27 13:21:20 +01:00
Sebastian Dröge
4bd74ac582 Merge branch 'wip/pwithnall/bsd-nofollow' into 'master'
gfileutils: Fix O_NOFOLLOW handling on BSD systems

See merge request GNOME/glib!1591
2020-07-27 12:01:19 +00:00
Philip Withnall
b4aa9d7858 gfileutils: Fix O_NOFOLLOW handling on BSD systems
Various different BSD systems use a different errno from `E_LOOP` (as
defined by POSIX and used on Linux) to indicate that a file is a symlink
when you try to `open()` it with `O_NOFOLLOW`.

Fix the code which detects this. This is a follow-up to #1302.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-27 12:24:20 +01:00
Philip Withnall
1ef6e84012 tests: Add some tests for expanding invalid escapes with g_strcompress()
This brings the branch coverage of `g_strcompress()` up to 100%. Yay.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-26 23:22:06 +01:00
Philip Withnall
59eb3c5bdb tests: Add missing cast to URI tests
This fixes a compiler warning.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-26 23:05:31 +01:00
Philip Withnall
437526675a tests: Test that UTF-8 is escaped correctly by g_strescape()
`g_strescape()` is documented as escaping UTF-8 characters, so test that
it does.

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

Fixes: #4
2020-07-26 23:04:37 +01:00
Philip Withnall
5bf3849099 tests: Add tests for g_file_set_contents_full()
These exercise all the code paths I can manage without adding a load of
machinery to inject faults into `write()`.

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

Helps: #1302
2020-07-26 21:38:17 +01:00
Philip Withnall
f35f9e918a tests: Use G_TEST_OPTION_ISOLATE_DIRS for fileutils tests
They all still pass.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-26 21:38:17 +01:00
Philip Withnall
cd02eac2d4 gstdio: Add macOS support to g_fsync()
Apparently, `fsync()` doesn’t actually sync to the spinning disk on
macOS. You need an `fcntl()` for that.

See: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fsync.2.html

Spotted by Christoph Reiter in a comment on !369.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-26 21:38:17 +01:00
Philip Withnall
926dfe5925 gstdio: Handle EINTR in g_fsync()
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-26 21:38:17 +01:00
Philip Withnall
84dd89242a gfileutils: Use g_fsync() for platform-independence
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-26 21:38:17 +01:00
Philip Withnall
3b6460b94c glib: Use g_file_set_contents_full() throughout GLib and GIO
Where applicable. Where the current use of `g_file_set_contents()` seems
the most appropriate, leave that in place.

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

Helps: #1302
2020-07-26 21:37:46 +01:00
Philip Withnall
24ed91ce33 gfileutils: Add a mode argument to g_file_set_contents_full()
This is used when creating the temporary file, or new file from scratch.

I wondered about also allowing the file owner and group to be set, but
that’s not as generally applicable — if your process is operating across
multiple user IDs then it likely has some fairly OS-specific
requirements and will need tighter control of its syscalls anyway.

(Eventually, support for setting the file owner and group atomically
could be added by writing out a file using `O_TMPFILE` so it’s not
addressable, and then linking it into the file system in place of the
old file using something like `renameat2(AT_EMPTY_PATH)` or `linkat()`.
That’s currently not possible without patching the kernel with
https://marc.info/?l=linux-fsdevel&m=152472898003523&w=2, as far as I
know at the moment.)

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

Fixes: #1203
2020-07-26 21:37:46 +01:00
Philip Withnall
f3cea1c464 gfileutils: Implement GFileSetContentsFlags
This moves `write_to_temp_file()` into `g_file_set_contents_full()` and
coalesces its handling of `do_fsync` with the `rename_file()` call. It
adds support for `G_FILE_SET_CONTENTS_DURABLE` and
`G_FILE_SET_CONTENTS_NONE` — previously only
`G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING` was
supported.

In the case that `G_FILE_SET_CONTENTS_CONSISTENT |
G_FILE_SET_CONTENTS_DURABLE` is set, an additional `fsync()` is now done
on the directory after renaming the temporary file.

In the case that `G_FILE_SET_CONTENTS_ONLY_EXISTING` isn’t set, the
`fsync()` after writing the temporary file will always be done (unless
the file system guarantees it never needs to be done).

In the case that only `G_FILE_SET_CONTENTS_DURABLE` is set, the
destination file will be written to directly (using this mode is not
really advised).

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

Fixes: #1302
2020-07-26 21:37:46 +01:00
Philip Withnall
387c159862 gfileutils: Tidy up types of length arguments in helper functions
We can guarantee that they’re non-negative.

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

Helps: #1302
2020-07-26 21:37:46 +01:00
Philip Withnall
e4e618c604 gfileutils: Factor out fsync calculation
This introduces no functional changes, just makes the code a bit more
modular and reusable.

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

Helps: #1302
2020-07-26 21:37:46 +01:00
Philip Withnall
00c17de72e gfileutils: Split out write_to_file() helper function
This introduces no functional changes.

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

Helps: #1302
2020-07-26 21:37:46 +01:00
Philip Withnall
554107c23c gfileutils: Add g_file_set_contents_full() and GFileSetContentsFlags
This is a new version of the g_file_set_contents() API which will allow
its safety to be controlled by some flags, allowing the user to choose
their preferred tradeoff between safety (`fsync()` calls) and speed.

Currently, the flags do nothing and the new API behaves like the old
API. This will change in the following commits.

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

Helps: #1302
2020-07-26 21:37:46 +01:00
Marc-André Lureau
5b49df3b9f Merge branch 'uri-non-utf8' into 'master'
Fix non-utf8 URI pct-decoding / unescape regression

Closes #2165

See merge request GNOME/glib!1569
2020-07-26 14:24:53 +00:00
Philip Withnall
c60d6599c9 Merge branch 'mach_monotonic_time_use_timebase_struct' into 'master'
gmain: fix fetching timebase on non-Intel Darwin

Closes #858

See merge request GNOME/glib!1566
2020-07-26 13:22:48 +00:00
Marc-André Lureau
22fe3b0224 uri: fix regression in g_uri_unescape_segment/string
The previous implementation of g_uri_unescape_segment() allowed non-utf8
decoded characters. uri_decoder() allows it too with FLAGS_ENCODED (I
think it's abusing a bit the user-facing flags for some internal
decoding behaviour)

However, it didn't allow \0 in the decoded string. Let's have an extra
check for that, outside of uri_decoder().

Fixes: d83d68d64c
Reported-by: Matthias Clasen <mclasen@redhat.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-26 17:19:43 +04:00
Marc-André Lureau
ea395e3fdd uri: add a GError to the new g_uri_unescape_bytes()
Suggested-by: Matthias Clasen <mclasen@redhat.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-26 17:19:37 +04:00
Philip Withnall
0b75a92823 Merge branch '2129-tz-slim-tests' into 'master'
tests: Add tests for RFC 8536 v3 parsing of time zones

See merge request GNOME/glib!1578
2020-07-25 18:44:29 +00:00
Philip Withnall
c05252b4f1 Merge branch 'uri-illegal' into 'master'
uri: add illegal_characters argument to unescape_bytes

See merge request GNOME/glib!1577
2020-07-25 14:49:28 +00:00
Nirbheek Chauhan
c3c2c31335 glib: Use g_getenv everywhere instead of getenv
`getenv()` doesn't work well on Windows, f.ex., it can't fetch env
vars set with `SetEnvironmentVariable()`. This also means that it
doesn't work at all when targeting UWP since that's the only way to
set env vars in that case.
2020-07-23 13:57:59 +05:30
Philip Withnall
98a7d6e389 tests: Add tests for RFC 8536 v3 parsing of time zones
This adds really basic validation that `GTimeZone` can successfully
parse a ‘slim’ format timezone file.

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

Helps: #2129
2020-07-17 15:08:59 +01:00
Philip Withnall
e18a6a28db tests: Incorporate bug base into g_test_bug() calls in gdatetime tests
This will allow the following commit to refer to GitLab in its
`g_test_bug()` call.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-17 15:08:59 +01:00
Philip Withnall
f5b4a6b9cc Merge branch 'GDateTime-TZif' into 'master'
GDateTime fixes to handle TZif footers

Closes #2129

See merge request GNOME/glib!1533
2020-07-17 13:54:48 +00:00
Paul Eggert
4408e69a0f gtimezone: add FIXME comments 2020-07-16 12:44:25 -07:00
Paul Eggert
7414dc2cb3 Clarify memset in set_tz_name
* glib/gtimezone.c (set_tz_name): Use size, not NAME_SIZE,
to clear the buffer.  Suggested by Philip Withnall in:
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1533#note_867859
2020-07-16 12:44:25 -07:00
Paul Eggert
25d950b61f gtimezone: support footers in TZif files
Since tzcode95f (1995), TZif files have had a trailing
TZ string, used for timestamps after the last transition.
This string is specified in Internet RFC 8536 section 3.3.
init_zone_from_iana_info has ignored this string, causing it
to mishandle timestamps past the year 2038.  With zic's new -b
slim flag, init_zone_from_iana_info would even mishandle current
timestamps.  Fix this by parsing the trailing TZ string and adding
its transitions.

Closes #2129
2020-07-16 12:44:25 -07:00
Paul Eggert
68978631e5 gtimezone: add support for RFC 8536 time zone transitions
Time zone transition times can range from -167:59:59 through
+167:59:59, according to Internet RFC 8536 section 3.3.1;
this is an extension to POSIX.  It is needed for proper
support of TZif version 3 files.
2020-07-16 12:44:25 -07:00
Paul Eggert
325d17c49f gtimezone: allow hh to be 24, as per POSIX
POSIX allows hh to be 24; see
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03
2020-07-16 12:44:25 -07:00
Paul Eggert
bc78f0ad27 gtimezone: support POSIX 1003.1-2001 quoted TZ abbreviations
TZ strings like '<-03>3' were introduced in POSIX 1003.1-2001 and
are currently specified in:
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03
2020-07-16 12:44:25 -07:00
Paul Eggert
21b000356e gtimezone: get 64-bit data from version-3 TZif files
Version 3 was introduced in tzdb 2013e (2013).
See Internet RFC 8536 section 3.1 under "ver(sion)".
2020-07-16 12:44:25 -07:00
Marc-André Lureau
4c6654dcd4 uri: add illegal_characters argument to unescape_bytes
It's not clear to me why this argument was excluded in the first place,
and Dan doesn't remember either. At least for consistency with
unescape_string, add it.

See also:
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1574#note_867283

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-15 22:03:28 +04:00
Marc-André Lureau
4433a46e06 uri: fix g_uri_unescape_string() regression
The illegal character set used to be applied only to the decoded
characters.

Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/2160

Fixes: d83d68d64c ("guri: new URI parsing and generating functions")
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-15 19:13:52 +04:00
Philip Withnall
1135ca25fd Merge branch 'analyzer-noreturn' into 'master'
gtestutils: Mark that g_assert_whatever() macros do not normally return

Closes #1200 and #1288

See merge request GNOME/glib!1553
2020-07-15 12:31:56 +00:00
Philip Chimento
6bdf640f51 gtestutils: Mark that g_assert_whatever() macros do not normally return
g_assert_true(), g_assert_cmpint(), and friends, can return to the
caller if test_nonfatal_assertions is set, but this is normally not the
case. In particular, for the purposes of static analysis, we
specifically don't want to assume that they might return. Clang has an
analyzer_noreturn attribute for this exact purpose, which conveniently
already has a macro in gmacros.h.

Fixes: #1288
Fixes: #1200
2020-07-15 12:45:56 +01:00
holin
eb3409a9c9
gmain: fix fetching timebase on non-Intel Darwin
Co-authored-by: Misty De Meo <mistydemeo@gmail.com>
2020-07-13 10:25:20 -07:00
Philip Withnall
735c80dc5b Merge branch 'uri-parse-params' into 'master'
A few g_uri_parse_params() improvements

See merge request GNOME/glib!1557
2020-07-08 10:53:11 +00:00
Harald van Dijk
05d65aec12
tests: limit number of threads to something reasonable.
Creating 1000 threads with the default stack size of 8 MiB will fail on
architectures with a 32-bit address space. Move up the existing THREADS
macro and use that instead, but change its definition to 1000 if
pointers are larger than 32 bits.

Signed-off-by: Harald van Dijk <harald@gigawatt.nl>
2020-07-07 13:37:30 +01:00
Marc-André Lureau
3f72a95925 uri: make g_uri_parse_params() take an error
This should be more future-proof.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-07 15:20:57 +04:00
Marc-André Lureau
7bee36b4ff uri: add G_FLAGS_ENCODED_QUERY
A query string may have some '=' characters '%'-encoded that could be
split by g_uri_parse_params() incorrectly. Instead, callers should leave
the query part encoded, and let g_uri_parse_params() do the decoding.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-07 15:16:44 +04:00
Marc-André Lureau
30ad9c6711 uri: teach g_uri_parse_params() to decode www-form query
This is a minor convenience, to avoid caller to do further '+' decoding.

According to the W3C HTML specification, space characters are replaced
by '+': https://url.spec.whatwg.org/#urlencoded-parsing

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-07 15:16:33 +04:00
Marc-André Lureau
e2d3349c56 tests/uri: add g_uri_parse_params() corner-cases
Add a test for empty key & empty value, and a case for missing =.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-07 15:16:30 +04:00
Marc-André Lureau
591d8676ee uri: modify g_uri_parse_params() to take flags
This will allow to further enhance the parsing, without breaking API,
and also makes argument on call side a bit clearer than just TRUE/FALSE.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-07 15:16:22 +04:00
Marc-André Lureau
d022b7199b uri: change parse_params() to take a separator set
This should give a bit more flexibility, without drawbacks.

Many URI encoding accept either '&' or ';' as separators.

Change the documentation to reflect that '&' is probably more
common (http query string).

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-07 15:16:12 +04:00
Marc-André Lureau
db9987d269 strfuncs: a few g_strsplit_set() improvements
gboolean is secretly actually typedef gint gboolean, so the delim_table
is going to take 1KB of stack all by itself. That’s fine, but it could
be smaller.

This strnpbrk()-like block could do with a comment to make it a bit
clearer what it’s doing.

Suggested-by: Philip Withnall <philip@tecnocode.co.uk>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-07-07 14:18:48 +04:00
Philip Withnall
7cd67c935f gspawn: Add sysprof trace support for spawning
Use this to replace the much-hated `g_debug()` which told people that
`posix_spawn()` (the fast path) wasn’t being used for various reasons.

If people want to make their process spawning faster now, they’ll have
to use a profiling tool like sysprof to check their program’s
performance. Shocking.

I think I was wrong to put this `g_debug()` in there in the first place
— it hasn’t served its purpose of making people speed up their spawn
paths to use `posix_spawn()`, it’s only cluttered up logs and frustrated
people.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-07 11:17:10 +01:00
Philip Withnall
bfe161742c gmain: Add sysprof tracing to GMainContext and GSource
This allows you to see how long each `GMainContext` iteration and each
`GSource` `check`/`prepare`/`dispatch` takes. It provides more detail
than sysprof’s speedtrack plugin can provide, since it has access to
more internal GLib data.

Use it with `sysprof-cli`, for example:
```
sysprof-cli --use-trace-fd -- my-test-program
```

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-07 11:17:10 +01:00
Philip Withnall
fa13c41da7 gtrace: Add sysprof tracing support infrastructure
Add some internal wrappers around sysprof tracing, so that it can be
used throughout GLib without exposing all the details of sysprof
internally.

This adds an optional dependency on `libsysprof-capture-4`. sysprof
support is disabled without it.

This depends on the GLib dependency of `libsysprof-capture` being
dropped in https://gitlab.gnome.org/GNOME/sysprof/-/merge_requests/30,
which has bumped the soname of `libsysprof-capture` and added subproject
support.

The next few commits will add marks that trace out each `GMainContext`
iteration and each `GSource` `check`/`prepare`/`dispatch` call.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-07 11:17:10 +01:00
Peter Bloomfield
3ef229fd03 gconvert: Use a pointer array in extract_uris
Use a GPtrArray instead of a GSList in g_uri_list_extract_uris().
2020-07-03 15:48:15 -04:00
Sebastian Dröge
15bf2ddaf5 Merge branch 'ossfuzz-23815-23818-uri-overflows' into 'master'
Fix buffer read overflows in GUri

See merge request GNOME/glib!1559
2020-07-01 12:59:19 +00:00
Philip Withnall
f9d165add1 guri: Fix buffer overrun when decoding %-encoded URI components
There is a limited (1 or 2 byte) read off the end of the buffer if its
final or penultimate byte is `%` and it’s not nul-terminated after that.
If the buffer *is* nul-terminated then the first `g_ascii_isxdigit()`
call safely returns `FALSE` and the code moves on.

Fix it by adding an additional check, and some unit tests to catch the
behaviour.

This bug is present in libsoup, which `GUri` is based on, but not
exploitable due to how the external API only exposes nul-terminated
strings. See https://gitlab.gnome.org/GNOME/libsoup/-/merge_requests/126
for the fix there.

oss-fuzz#23815
oss-fuzz#23818

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-01 13:28:09 +01:00
Philip Withnall
0b198104e5 tests: Test the length argument of g_uri_unescaped_bytes()
Modify the existing test function to run each test twice: once
nul-terminated and once with a length specified.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-01 13:28:09 +01:00
Philip Withnall
7656399cf9 tests: Rewrite another URI test to use an array of test strings
This introduces no functional changes, but will make it easier to add
more tests in future.

It splits the unescaping tests out so the different types of unescaping
(string, bytes, segment) are tested separately, since they have
different limitations.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-01 13:28:09 +01:00
Philip Withnall
836fee7a08 tests: Test the length argument of g_uri_parse_params()
Modify the existing test function to run each test twice: once
nul-terminated and once with a length specified.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-01 12:25:49 +01:00
Philip Withnall
67c9d34d71 guri: Allow params to be NULL if length is 0
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-01 12:25:49 +01:00
Philip Withnall
ab33896bcc tests: Rewrite a URI test to use an array of test strings
This introduces no functional changes, but will make it easier to add
more tests in future.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-07-01 12:25:49 +01:00
Yuri Chornoivan
4fe91b63a3 Remove c-format from the string that is not c-formatted 2020-06-29 13:02:44 +00:00
Philip Withnall
da948f7218 gmain: Fix use of atomic primitives with sig_atomic_t
It seems that `sig_atomic_t` is not the same width as `int` on FreeBSD,
which is causing CI failures:
```
 ../glib/gmain.c:5206:3: error: '_GStaticAssertCompileTimeAssertion_73' declared as an array with a negative size
  g_atomic_int_set (&any_unix_signal_pending, 0);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../glib/gatomic.h💯5: note: expanded from macro 'g_atomic_int_set'
    G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

Fix that by only using `sig_atomic_t` if the code is *not* using atomic
primitives (i.e. in the fallback case). `sig_atomic_t` is only a typedef
around an integer type and is not magic. Its typedef is chosen by the
platform to be async-signal-safe (i.e. read or written in one instruction),
but not necessarily thread-safe.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-06-29 10:42:00 +01:00
Philip Withnall
6801e06d83 Merge branch 'uri' into 'master'
guri: new URI parsing and generating functions

See merge request GNOME/glib!1328
2020-06-25 11:52:07 +00:00
Dan Winship
d83d68d64c guri: new URI parsing and generating functions
Add a set of new URI parsing and generating functions, including a new
parsed-URI type GUri. Move all the code from gurifuncs.c into guri.c,
reimplementing some of those functions (and
g_string_append_uri_encoded()) in terms of the new code.

Fixes:
https://gitlab.gnome.org/GNOME/glib/issues/110

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-06-25 13:57:35 +04:00
Philip Withnall
0859a8904a Merge branch '1670-unix-signal-handling-safety' into 'master'
gmain: Access Unix signal handler state atomically

Closes #1670

See merge request GNOME/glib!1297
2020-06-25 09:30:58 +00:00
Marc-André Lureau
167b950122 misc: code style
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2020-06-25 12:08:25 +04:00
Philip Withnall
dd36248f9e gspawn: Don’t use malloc() when running a binary under /bin/sh
Allocate a working buffer before calling `fork()` to avoid calling
`malloc()` in the async-signal-safe context between `fork()` and
`exec()`, where it’s not safe to use.

In this case, the buffer is used to assemble a wrapper around `argv` so
it can be run under `/bin/sh`.

See `man 7 signal-safety`.

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

Fixes: #2140
2020-06-23 12:03:30 +01:00
Philip Withnall
cf5af28169 gspawn: Don’t use malloc() when searching for a binary
Allocate a working buffer before calling `fork()` to avoid calling
`malloc()` in the async-signal-safe context between `fork()` and
`exec()`, where it’s not safe to use.

In this case, the buffer is used to assemble elements from `PATH` with
the binary from `argv[0]` to try executing them.

See `man 7 signal-safety`.

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

Helps: #2140
2020-06-23 12:03:30 +01:00
Philip Withnall
62ce66d4e7 gspawn: Don’t use getenv() in async-signal-safe context
Query the environment before calling `fork()` so that it doesn’t have to
be called in the async-signal-safe context between `fork()` and
`exec()`.

See `man 7 signal-safety`.

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

Helps: #2140
2020-06-23 12:03:30 +01:00
Philip Withnall
84f188ae24 gspawn: Don’t use getrlimit() or sysconf() in async-signal-safe context
They’re not safe to call in an async-signal-safe context on Linux.
`sysconf()` is safe to call on FreeBSD and OpenBSD (at least), so
continue doing that.

This will reduce performance in the (already low performance) fallback
case where `/proc` is inaccessible to a forked process on Linux, while
spawning a subprocess.

See `man 7 signal-safety`.

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

Helps: #2140
2020-06-23 12:03:30 +01:00
Philip Withnall
1051bfe11e gspawn: Don’t use g_assert() in async-signal-safe context
Use the error handling infrastructure which already exists for other
failures in the async-signal-safe context.

`g_assert()` is unlikely to have caused problems in practice because it
is only async-signal-unsafe when the assertion condition fails.

See `man 7 signal-safety`.

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

Helps: #2140
2020-06-23 12:03:30 +01:00
Philip Withnall
33948929df gspawn: Don’t use g_ascii_isdigit() in async-signal-safe context
While `g_ascii_isdigit()` *is* currently async-signal-safe, it’s going
to be hard to remember to keep it that way if the implementation changes
in future.

It seems more robust to just reimplement it here, given that it’s not
much code.

See `man 7 signal-safety`.

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

Helps: #2140
2020-06-23 12:03:30 +01:00
Philip Withnall
6f46294227 gspawn: Don’t use g_close() in async-signal-safe context
Use normal `close()` instead, which is guaranteed to be
async-signal-safe.

See `man 7 signal-safety`.

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

Helps: #2140
2020-06-23 12:03:30 +01:00
Philip Withnall
0e05ef7750 gspawn: Audit for async-signal-safety
Functions called between `fork()` and `exec()` have to be
async-signal-safe.

Add a comment to each function which is called in that context, and
`FIXME` comments to the non-async-signal-safe functions which end up
being called as leaves of the call graph.

The following commits will fix those `FIXME`s.

See `man 7 signal-safety`.

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

Helps: #2140
2020-06-23 12:02:13 +01: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
253f5cda82 gmain: Access Unix signal handler state atomically
There are two variables which are used to pass state from the Unix
signal handler interrupt function to the rest of `gmain.c`. They are
currently defined as `sig_atomic_t`, which means that they are
guaranteed to be interrupt safe. However, it does not guarantee they are
thread-safe, and GLib attaches its signal handler interrupt function to
a worker thread.

Make them thread-safe using atomics. It’s not possible to use locks, as
pthread mutex functions are not signal-handler-safe. In particular, this
means we have to be careful not to end up using GLib’s fallback atomics
implementation, as that secretly uses a mutex. Better to be unsafe than
have a re-entrant call into `pthread_mutex_lock()` from a nested signal
handler.

This commit solves two problems:
 1. Writes to `any_unix_signal_pending` and `unix_signal_pending` could
    be delivered out of order to the worker thread which calls
    `dispatch_unix_signals()`, resulting in signals not being handled
    until the next iteration of that worker thread. This is a
    performance problem but not a correctness problem.
 2. Setting an element of `unix_signal_pending` from
    `g_unix_signal_handler()` and clearing it from
    `dispatch_unix_signals_unlocked()` (in the worker thread) could
    race, resulting in a signal emission being cleared without being
    handled. That’s a correctness problem.

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

Fixes: #1670
2020-06-19 10:49:14 +01:00
Emmanuele Bassi
b79635b391 Merge branch 'more-memcheck' into 'master'
glib.supp improvements

Closes #2134 and #2132

See merge request GNOME/glib!1535
2020-06-17 11:21:39 +00:00