Commit Graph

77 Commits

Author SHA1 Message Date
Philip Withnall
3e313438f1 gvariant-parser: Reject deeply-nested typedecls in text form variants
Return `G_VARIANT_PARSE_ERROR_RECURSION` from `g_variant_parse()` if a
typedecl is found within a text-form variant which would cause any part
of the variant to exceed the maximum allowed recursion/nesting depth.

This fixes an oversight when `G_VARIANT_MAX_RECURSION_DEPTH` was
implemented, which allowed typedecls to effectively multiply the size of
an array if `g_variant_parse()` was parsing a text-form variant without
a top-level concrete type specified.

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

Fixes: #2782
oss-fuzz#49462
2022-10-18 14:52:32 +01:00
Philip Withnall
70ee43f1e9 glib: Add SPDX license headers automatically
Add SPDX license (but not copyright) headers to all files which follow a
certain pattern in their existing non-machine-readable header comment.

This commit was entirely generated using the command:
```
git ls-files glib/*.[ch] | xargs perl -0777 -pi -e 's/\n \*\n \* This library is free software; you can redistribute it and\/or\n \* modify it under the terms of the GNU Lesser General Public/\n \*\n \* SPDX-License-Identifier: LGPL-2.1-or-later\n \*\n \* This library is free software; you can redistribute it and\/or\n \* modify it under the terms of the GNU Lesser General Public/igs'
```

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

Helps: #1415
2022-05-18 09:19:02 +01:00
Loic Le Page
28fa4325a4 Fix non-initialized variable in glib/gvariant-parser.c 2022-02-18 10:52:38 +01:00
Philip Withnall
1111bc4cde gvariant-parser: Shut up a scan-build warning
Change a condition from one to an equivalent one to shut up a
`scan-build` warning about potentially dereferencing a `NULL` value.
This introduces no functional changes, as it’s not actually possible to
dereference a `NULL` value here (but `scan-build` can’t link the
nullability of `error` to the nullability of `result`).

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-10-30 15:37:39 +00:00
Philip Withnall
25c2266a33 gvariant: Limit recursion in g_variant_parse()
The token parsing done by g_variant_parse() uses recursive function
calls, so at some point it will hit the stack limit. As with previous
changes to `GVariantType` parsing (commit 7c4e6e9fbe), limit the level
of nesting of containers parsed by g_variant_parse() to something
reasonable. We guarantee 64 levels of nesting, which should be enough
for anyone, and is the same as what we guarantee for types.

oss-fuzz#10286

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-10-18 13:53:18 +01:00
Philip Withnall
18a232be89 glib: Various minor scan-build fixes
These squash various warnings from `scan-build`. None of them are
legitimate bugs, but some of them do improve code readability a bit.

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

Helps: #1767
2019-09-05 13:51:27 +01:00
Дилян Палаузов
512655aa12 minor typos in the documentation (a/an) 2019-08-24 19:14:05 +00:00
Emmanuel Fleury
a7aac03ead Fixing various warnings in glib/gvariant-parser.c
glib/gvariant-parser.c: In function ‘number_get_value’:
glib/gvariant-parser.c:1924:46: error: operand of ?: changes signedness from ‘int’ to ‘guint64’ {aka ‘long unsigned int’} due to unsignedness of other operand [-Werror=sign-compare]
       return g_variant_new_int16 (negative ? -((gint16) abs_val) : abs_val);
                                             ^~~~~~~~~~~~~~~~~~~
glib/gvariant-parser.c:1934:46: error: operand of ?: changes signedness from ‘int’ to ‘guint64’ {aka ‘long unsigned int’} due to unsignedness of other operand [-Werror=sign-compare]
       return g_variant_new_int32 (negative ? -((gint32) abs_val) : abs_val);
                                              ^~~~~~~~~~~~~~~~~~~
glib/gvariant-parser.c:1944:46: error: operand of ?: changes signedness from ‘long int’ to ‘guint64’ {aka ‘long unsigned int’} due to unsignedness of other operand [-Werror=sign-compare]
       return g_variant_new_int64 (negative ? -((gint64) abs_val) : abs_val);
                                                  ^~~~~~~~~~~~~~~~~~~
glib/gvariant-parser.c:1954:47: error: operand of ?: changes signedness from ‘int’ to ‘guint64’ {aka ‘long unsigned int’} due to unsignedness of other operand [-Werror=sign-compare]
       return g_variant_new_handle (negative ? -((gint32) abs_val) : abs_val);
                                                   ^~~~~~~~~~~~~~~~~~~
glib/gvariant-parser.c: In function ‘g_variant_parse_error_print_context’:
glib/gvariant-parser.c:2785:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
       if (point >= strlen (source_str))
                 ^~
2019-03-17 19:05:35 +01:00
Philip Withnall
c561870359 Merge branch 'pattern-coalesce' into 'master'
gvariant-parser: Fix pattern coalesce of M and *

See merge request GNOME/glib!694
2019-03-15 12:31:03 +00:00
Philip Withnall
faa1d63cab glib: Fix various compiler warnings when compiling with G_DISABLE_ASSERT
Mostly unused variables which are only used in a g_assert() call
otherwise.

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

Helps: #1708
2019-03-08 19:46:21 +00:00
Tomasz Miąsko
153f63d13e gvariant-parser: Fix pattern coalesce of M and *
Previously pattern_coalesce incorrectly concluded that maybe type is not
present when one pattern starts with `M` and other pattern with anything
else than `M` or `m`. This is false when the other pattern is `*`, since
it includes the maybe type.
2019-02-26 17:27:49 +01:00
Philip Withnall
5c97cf666d Merge branch 'ossfuzz-11578-variant-text-type-detection' into 'master'
gvariant-parser: Fix error handling when type coalescing fails

See merge request GNOME/glib!639
2019-02-26 14:19:08 +00:00
Philip Withnall
c927c59a84 gvariant-parser: Fix error handling when type coalescing fails
When parsing GVariant text format strings, we do a limited form of type
inference. The algorithm for type inference for nested array child types
is not complete, however (and making it complete, at least with a naive
implementation, would make it O(N^2), which is not worth it) and so some
text format arrays were triggering an assertion failure in the error
handling code.

Fix that by making the error handling code a little more relaxed, in the
knowledge that our type inference algorithm is not complete. See the
comment added to the code.

This includes a test case, provided by oss-fuzz.

oss-fuzz#11578

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-02-05 15:50:15 +00:00
Philip Withnall
0fcd5ac89d gvariant-parser: Fix parsing of G_MININT* values in GVariant text format
And add tests.

There wasn’t actually a bug on x86_64 before, but it was making use of
undefined behaviour, and hence triggering ubsan warnings. Make the code
more explicit, and avoid undefined behaviour.

oss-fuzz#12686

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-02-05 15:02:49 +00:00
Philip Withnall
e3e4a09716 gvariant-parser: Add explicit unsigned-to-signed casts
Rather than prefixing unsigned numbers with unary minus operators and
expecting the implicit cast to carry the correct value through, add an
explicit cast to a signed type before the unary minus is applied.

In all four cases, an overflow check has already been done.

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

https://gitlab.gnome.org/GNOME/glib/issues/1655
2019-01-19 01:07:57 +00:00
Philip Withnall
d2224b475d gvariant: Fix error handling for parsing Unicode escapes
When parsing an escaped Unicode character in a text format GVariant
string, such as '\U0001F415', the code uses g_ascii_strtoull(). This,
unexpectedly, accepts minus signs, which can cause an assertion failure
when input like '\u-FF4' is presented for parsing.

Validate that there are no leading sign characters when parsing.

This shouldn’t be considered a security bug, because the GVariant text
format parser should not be used on untrusted input.

oss-fuzz#11576

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-12-04 12:55:21 +00:00
Philip Withnall
ca328615c8 gvariant: Use gsize rather than gint for array lengths
This introduces no functional changes.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-12-04 12:54:39 +00:00
Benjamin Otte
3aff811d13 Use G_GNUC_FALLTHROUGH where appropriate 2018-09-04 20:24:25 +02:00
Philip Withnall
a9108f8bfd gvariant: Fix more bounds checking in GVariant text format parser
token_stream_prepare() was over-reading at the start of bytestring
literals (`b'blah'`).

Add tests for that, and for some other situations regarding bytestring
literal parsing, in order to try and get full branch coverage of that
bit of code.

oss-fuzz#9805

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-08-10 10:30:13 +01:00
Philip Withnall
10ee7301e8 gvariant: Fix bounds checking in GVariant text format parser
The token_stream_peek() functions were not doing any bounds checking, so
could potentially read 1 byte off the end of the input blob. This was
never noticed, since the input stream is almost always a nul-terminated
string. However, g_variant_parse() does allow non-nul-terminated strings
to be used with a @limit parameter, and the bugs become apparent under
valgrind if that parameter is used.

This includes modifications to the test cases to cover the
non-nul-terminated case.

Spotted by ossfuzz.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-08-09 01:08:46 +01:00
Sanjeev
cd0bbbf1ef gvariant: Fix minor memory leak on error handling path
(Commit message by Philip Withnall.)

https://bugzilla.gnome.org/show_bug.cgi?id=760022
2017-11-08 12:36:26 +00:00
Sébastien Wilmet
f9faac7661 glib/: LGPLv2+ -> LGPLv2.1+
All glib/*.{c,h} files have been processed, as well as gtester-report.

12 of those files are not licensed under LGPL:

	gbsearcharray.h
	gconstructor.h
	glibintl.h
	gmirroringtable.h
	gscripttable.h
	gtranslit-data.h
	gunibreak.h
	gunichartables.h
	gunicomp.h
	gunidecomp.h
	valgrind.h
	win_iconv.c

Some of them are generated files, some are licensed under a BSD-style
license and win_iconv.c is in the public domain.

Sub-directories inside glib/:

	deprecated/: processed in a previous commit
	glib-mirroring-tab/: already LGPLv2.1+
	gnulib/: not modified, the code is copied from gnulib
	libcharset/: a copy
	pcre/: a copy
	tests/: processed in a previous commit

https://bugzilla.gnome.org/show_bug.cgi?id=776504
2017-05-24 11:58:19 +02:00
Christian Hergert
18a33f72db introspection: use (nullable) or (optional) instead of (allow-none)
If we have an input parameter (or return value) we need to use (nullable).
However, if it is an (inout) or (out) parameter, (optional) is sufficient.

It looks like (nullable) could be used for everything according to the
Annotation documentation, but (optional) is more specific.
2016-11-22 14:14:37 -08:00
Benjamin Otte
611b079e3d gvariant: Add suggested braces
... around empty body in an 'else' statement
2016-11-20 23:50:56 +01:00
Hanno Boeck
aead1c046d GVariant text: fix scan of positional parameters
The scanning to find the end of a positional parameter designator in
GVariant text format (e.g. '%i') is currently broken in case the 'end'
pointer is not specified.

The scan is controlled by a somewhat complicated loop that needs to deal
properly with cases like (123, %(ii)) [where '%(ii)' is to be taken
together, but the final ')' not].

This loop missed the case where a format string passed to
g_variant_new_parsed() ended immediately after such a conversion, with a
nul character.  In this case the 'end' pointer is NULL, so the only way
we can find the end is by scanning for nul in the string.

In case of g_variant_new_parsed() [which is what this code was designed
to be used for], the bug is somewhat unlikely in practice: the only way
that a valid text-form GVariant could ever contain a positional
parameter replacement at the end of the string is if this positional
parameter were the only thing being returned.  In that case, the user
would likely have opted for a more direct approach.

Unfortunately, this code is also active in the tokenisation phase of
g_variant_parse(), before positional parameters are rejected as invalid
for that case.  Anyone who calls this function with a nul-terminated
string (and no end pointer) is vulnerable to a crash from malicious user
input.  This can be seen, at the very least with many commandline tools:

  $ dconf write /x '%i'
  Segmentation fault

We fix this problem by searching for the nul character in this case, in
addition to comparing the end pointer.

This problem is almost certainly limited to being able to cause crashes.
The loop in question only performs reads and, in the security-sensitive
case, the token will be quickly rejected after the loop is finished
(since it starts with '%' and the 'app' pointer is unset).  This is
further mitigated by the fact that there are no known cases of GVariant
text format being used as part of a protocol at a privilege barrier.
2016-02-22 08:48:44 -05:00
Philip Withnall
1b3dbec065 gvariant-parser: Clarify g_variant_parse() returns a non-floating ref
Unlike, say, g_variant_new(), which returns a floating reference.
g_variant_parse() returns a non-floating one, so must always have
g_variant_unref() called on the result.
2015-04-22 00:02:06 +01:00
Matthias Clasen
ab18d71e6f Minor documentation additions and corrections
Going for 100%.
2014-05-31 10:54:02 -04:00
Matthias Clasen
e7fd3de86d Eradicate links and xrefs
These are all replaced by markdown ref links.
2014-02-08 12:26:56 -05:00
Philip Withnall
2b8edf234c gvariant: Document the need to cast varargs when constructing GVariants
Slightly expand on the documentation about casting varargs when
constructing GVariants, and link to it from all the functions where it’s
a necessary consideration.

Add an example of passing flags to a ‘t’ type variable (guint64).
Assuming the flags enum does not have many members, the flag variable
will be 32 bits wide, and needs an explicit cast to be passed into
g_variant_new() as a 64-bit value.

https://bugzilla.gnome.org/show_bug.cgi?id=712837
2014-02-05 09:40:49 +00:00
Matthias Clasen
8945da08ac Make gtk-doc find another symbol 2014-02-01 20:53:50 -05:00
Matthias Clasen
adf892e96a Annotate all examples with their language
The C ones, at least.
2014-02-01 15:11:49 -05:00
Matthias Clasen
a4c33c6f8b Docs: Don't use the code tag 2014-01-31 22:05:04 -05:00
Matthias Clasen
17f51583a8 Docs: Convert examples to |[ ]| 2014-01-31 21:56:33 -05:00
Daniel Mustieles
078dbda148 Updated FSF's address 2014-01-31 14:31:55 +01:00
Matthias Clasen
93dad4808e Trivial typo fix 2014-01-01 17:59:22 -05:00
Ryan Lortie
56fb675d86 GVariant: add way to print context of parse errors
This was a feature intended from the very beginning that somehow never
got written.  It's a way to replace these sort of error messages out of
the GVariant parser:

  1-2,10-15:unable to find a common type

with something in the style of the Vala compiler:

  unable to find a common type:
    [1, 2, 3, 'str']
     ^        ^^^^^

https://bugzilla.gnome.org/show_bug.cgi?id=715028
2013-12-22 11:41:19 -05:00
Murray Cumming
eeac91f866 GVariant: Add g_variant_parse_error_quark()
Most GErrors, such as GSomethingError, have a function to get
their quark that looks like g_something_error_quark(),
so bindings (such as gtkmm) would expect GVariantParseError
to have g_variant_parse_error_quark(). Instead this had
g_variant_parser_get_error_quark().
This deprecates the old function and adds the correct one,
making life easier for gtkmm (and maybe others).

https://bugzilla.gnome.org/show_bug.cgi?id=708212
2013-12-22 11:27:16 -05:00
Matthias Clasen
3309055878 GVariantBuilder: small documentation fixes
The examples for g_variant_builder_add and
g_variant_builder_add_parsed were leaking a heap-allocated
builder. Fix that by converting the examples to stack
allocation.

https://bugzilla.gnome.org/show_bug.cgi?id=697585
https://bugzilla.gnome.org/show_bug.cgi?id=703522
2013-11-23 13:16:06 -05:00
Dan Winship
4b94c0831e Use 'dumb quotes' rather than `really dumb quotes'
Back in the far-off twentieth century, it was normal on unix
workstations for U+0060 GRAVE ACCENT to be drawn as "‛" and for U+0027
APOSTROPHE to be drawn as "’". This led to the convention of using
them as poor-man's ‛smart quotes’ in ASCII-only text.

However, "'" is now universally drawn as a vertical line, and "`" at a
45-degree angle, making them an `odd couple' when used together.

Unfortunately, there are lots of very old strings in glib, and also
lots of new strings in which people have kept up the old tradition,
perhaps entirely unaware that it used to not look stupid.

Fix this by just using 'dumb quotes' everywhere.

https://bugzilla.gnome.org/show_bug.cgi?id=700746
2013-05-21 11:23:22 -03:00
Ryan Lortie
57cd876321 GVariant parser: tweak lexer for format strings
Tweak the lexer so that '[%s]' gets parsed as three separate tokens,
instead of the closing bracket getting sucked into the format string.
2013-04-19 11:40:04 -04:00
Ryan Lortie
1d87c6c7f8 GVariant parser: turn two asserts into soft errors
Parsing wrongly-typed GVariant text format data is a well-defined
operation and it ought to result in a GError.  We do that for most
cases, but 'v' and 'ay' were being treated differently.  Fix those as
well.
2013-04-19 11:40:04 -04:00
Henrique Dante de Almeida
c219181cb2 Add G_GNUC_PRINTF on all functions with format strings
This allows compilation with clang without errors, even when
-Wformat-nonliteral is active (as long as there are no real cases of
non literal formatting).

https://bugzilla.gnome.org/show_bug.cgi?id=691608
2013-01-13 12:32:40 -05:00
Robert Ancell
59a24ab5a3 Use "Returns:" instead of the invalid "@returns" for annotating return values.
https://bugzilla.gnome.org/show_bug.cgi?id=673229
2012-11-01 14:47:25 +13:00
Cosimo Cecchi
4b602940e2 glib: don't quote quark names for G_DEFINE_QUARK 2012-08-28 13:16:24 -04:00
Matthias Clasen
369c7689c2 Add missing includes 2012-08-28 05:57:17 -04:00
Matthias Clasen
4f12f7c029 Use G_DEFINE_QUARK for GLib's own quarks
This commit just deals with glib/.
gobject/ and gio/ will be handled in separate commits.
2012-08-28 00:08:07 -04:00
Robert Ancell
4143842eb4 Add missing allow-none annotations for function parameters.
Found using:
find . -name '*.c' | xargs grep 'or %NULL' | grep ' \* @' | grep -v '@error' | grep -v allow-none
2012-03-31 20:34:28 +11:00
Ryan Lortie
0e0554bd62 GVariant: drop use of @returns 2011-11-21 11:33:21 -05:00
Ryan Lortie
740eacbfca static and #include fixups in glib/ 2011-10-16 21:41:15 -04:00
Simon McVittie
83afcc0a29 Escape percent sign in g_variant_new_parsed documentation
Strings matching /%[a-z]/ are special syntax for gtk-doc.

Bug: https://bugzilla.gnome.org/show_bug.cgi?id=632049
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: Ryan Lortie <desrt@desrt.ca>
2011-10-04 12:38:32 +01:00