Commit Graph

255 Commits

Author SHA1 Message Date
Philip Withnall
feff097f27 gstrfuncs: Deprecate g_memdup() in favour of g_memdup2()
Unfortunately, `g_memdup()` accepts its size argument as a `guint`,
unlike most other functions which deal with memory sizes — they all use
`gsize`. `gsize` is 64 bits on 64-bit machines, while `guint` is only 32
bits. This can lead to a silent (with default compiler warnings)
truncation of the value provided by the caller. For large values, this
will result in the returned heap allocation being significantly smaller
than the caller expects, which will then lead to buffer overflow
reads/writes.

Any code using `g_memdup()` should immediately port to `g_memdup2()` and
check the pointer arithmetic around their call site to ensure there
aren’t other overflows.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
Fixes: #2319
2021-02-04 17:34:03 +00:00
Philip Withnall
f8cf0b8672 gstrfuncs: Add g_memdup2() function
This will replace the existing `g_memdup()` function, which has an
unavoidable security flaw of taking its `byte_size` argument as a
`guint` rather than as a `gsize`. Most callers will expect it to be a
`gsize`, and may pass in large values which could silently be truncated,
resulting in an undersize allocation compared to what the caller
expects.

This could lead to a classic buffer overflow vulnerability for many
callers of `g_memdup()`.

`g_memdup2()`, in comparison, takes its `byte_size` as a `gsize`.

Spotted by Kevin Backhouse of GHSL.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
Helps: GHSL-2021-045
Helps: #2319
2021-02-04 14:13:03 +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
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
00bfb3ab44 tree: Fix various typos and outdated terminology
This was mostly machine generated with the following command:
```
codespell \
    --builtin clear,rare,usage \
    --skip './po/*' --skip './.git/*' --skip './NEWS*' \
    --write-changes .
```
using the latest git version of `codespell` as per [these
instructions](https://github.com/codespell-project/codespell#user-content-updating).

Then I manually checked each change using `git add -p`, made a few
manual fixups and dropped a load of incorrect changes.

There are still some outdated or loaded terms used in GLib, mostly to do
with git branch terminology. They will need to be changed later as part
of a wider migration of git terminology.

If I’ve missed anything, please file an issue!

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-06-12 15:01:08 +01:00
Timm Bäder
a2e715a4fe strfuncs: Use a GPtrArray in strsplit()
This is more efficient and also much easier since we already have the
memory allocated that we're going to return from the function. No need
to do that ourselves or reverse a list.
2020-06-05 08:59:15 +02:00
Thomas Haller
c8194ee3ec gstrfuncs: use gsize type internally for strv functions
In C, the proper type for a heap allocate structure is size_t/gsize.
That means, no valid (heap allocated) pointer will ever contain more
bytes than size_t can represent.

Hence, this integer type should also be used when operating on
data like a strv array. Adjust some internal uses to use gsize
instead of gint/guint.

Note that g_strv_length() returns a value of type guint. So this
API cannot be used on string arrays longer of arbitrary size. But
that is not fixable.
2019-11-21 10:44:48 +01:00
Daniel P. Berrangé
109be1e90d glib: add parameter annotations for g_vasprintf and callers
Document that g_vasprintf and g_strdup_printf are guaranteed to return a
non-NULL string, unless the format string contains the locale sensitive
conversions %lc or %ls.

Further annotate that the output parameter for g_vasprintf and the
format string for all functions must be non-NULL.

Fixes #1622

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-10-07 17:29:34 +01:00
Дилян Палаузов
512655aa12 minor typos in the documentation (a/an) 2019-08-24 19:14:05 +00:00
Patrick Storz
56149722ae Add g_get_console_charset
Queries the charset used by the associated console, which does not
necessarily match the charset of the current locale as returned by
g_get_charset.

Fixes https://gitlab.gnome.org/GNOME/glib/issues/1270
2019-05-27 17:51:40 +00:00
Emmanuel Fleury
95a5f63775 Fix some documentation issue in glib/gstrfuncs.c
Apparently, the documentation of g_strcanon() was not really cristal
clear, so this new code sample try to make it clear the fact that we
are working on the given string and not a copy. Moreover, it provides
a way to keep the original string at once.

Fix #29
2019-04-30 09:43:01 +02:00
Emmanuel Fleury
592d4369d4 Fixing signedness problem in glib/gstrfuncs.c
glib/gstrfuncs.c: In function ‘g_strstr_len’:
glib/gstrfuncs.c:2709:24: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare]
       if (haystack_len < needle_len)
                        ^
2019-03-15 21:30:22 +01:00
Philip Withnall
a67eadbdc3 gstrfuncs: Add g_strv_equal()
This is a utility function which I find myself writing in a number of
places. Mostly in unit tests.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-12-17 17:16:03 +00:00
Philip Withnall
96acb49eb1 gstrfuncs: Clarify that g_ascii_string_to_unsigned() rejects signs
Unlike g_ascii_strtoull(), g_ascii_string_to_unsigned() does not permit
leading signs (`+` or `-`). Document that.

It’s already in the unit tests.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-12-04 12:53:52 +00:00
Philip Withnall
8d0a163000 gstrfuncs: Clarify that g_ascii_strtoull() accepts signed numbers
It’s perverse, but explicitly documented that strtoull() accepts numbers
with a leading minus sign (`-`) and explicitly casts them to signed
output.

g_ascii_strtoull() is documented to do what strtoull() does (but locale
independently), and its behaviour is correct. However, the documentation
could be a lot clearer about this unexpected behaviour.

Add a unit test for it too.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-12-04 12:51:09 +00:00
Philip Withnall
b806df0ef1 gstrfuncs: Clarify that g_strv_length() does not accept NULL
https://bugzilla.gnome.org/show_bug.cgi?id=795026

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Reviewed-by: nobody
2018-04-10 11:04:07 +01:00
Philip Withnall
880f07f94c gstrfuncs: Use curly quotes in a documentation comment
Nobody can argue with this: the documentation comment is about, and
contains, Unicode accents.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-11-13 10:37:11 +00:00
Philip Withnall
e61c3c628d gstrfuncs: Fix a typo in a documentation comment
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-11-13 10:35:41 +00:00
Andrew Borodin
7895706c82 Type accuracy for result of strlen() in string utilities
https://bugzilla.gnome.org/show_bug.cgi?id=630983
2017-10-06 12:32:10 +01:00
Martin Blanchard
c443adeffb gprintf: Fix documentation regarding <glib/gprintf.h>
g_snprintf() and g_vsnprintf() declarations were moved and
don't require gprintf.h to be included anymore but g_vasprintf()
is and requires gprintf.h to be explicitly included.

https://bugzilla.gnome.org/show_bug.cgi?id=760716
2017-10-05 15:01:28 +01:00
Philip Withnall
f2b6c11629 gstrfuncs: Expand documentation for errno functions
Mention that it really is a good idea to save errno before doing
literally anything else after calling a function which could set it.

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

https://bugzilla.gnome.org/show_bug.cgi?id=785577
2017-08-03 10:21:13 +01:00
Igor Pashev
c8e268bbce Fix detection and usage of strerror_r()
autoconf provides a macro for this situation, which saves us having to
manually work out whether strerror_r() returns a char* or an int.

https://bugzilla.gnome.org/show_bug.cgi?id=784000
2017-06-20 14:59:27 +01: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
Krzesimir Nowak
e8222c3343 gstrfuncs: Fix translation issues
The tool that extracts the translatable strings to .po files does not
cope with the G_GUINTX_FORMAT macros, so we preformat the numbers to
strings and use the strings in the translatable error messages.
2017-05-10 16:11:02 +02:00
Krzesimir Nowak
4fe89b0437 gstrfuncs: Add replacement for string-to-number functions
Very often when we want to convert a string to number, we assume that
the string contains only a number. We have g_ascii_strto* family of
functions to do the conversion but they are awkward to use - one has
to check if errno is zero, end_ptr is not NULL and *end_ptr points to
the terminating nul and then do the bounds checking. Many projects
need this kind of functionality, so it gets reimplemented all the
time.

This commit adds some replacement functions that convert a string to a
signed or unsigned number that also follows the usual way of error
reporting - returning FALSE on failure and filling an error output
parameter.
2017-05-10 12:04:03 +02:00
Simon McVittie
e5ed410c8c Avoid calling Standard C string/array functions with NULL arguments
glibc string.h declares memcpy() with attribute(nonnull(1,2)), causing
calls with NULL arguments to be treated as undefined behaviour.
This is consistent with ISO C99 and C11, which state that passing 0
to string functions as an array length does not remove the requirement
that the pointer to the array is a valid pointer.
gcc -fsanitize=undefined catches this while running OSTree's test suite.

Similarly, running the GLib test suite reports similar issues for
qsort(), memmove(), memcmp().

Signed-off-by: Simon McVittie <smcv@debian.org>
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=775510
Reviewed-by: Colin Walters
2016-12-02 19:10:39 +00: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
Phillip Wood
38c4e31c8a Fix documentation typos
Character entities are not supposed to be supported by gtk-doc¹ and
fix the spelling of ‘optional’

¹https://bugzilla.gnome.org/show_bug.cgi?id=758137

https://bugzilla.gnome.org/show_bug.cgi?id=758174
2016-04-11 23:31:38 -04:00
Bastien Nocera
283c565af6 gstrfuncs: Document the behaviour of g_strjoinv()
The behaviour of g_strjoinv() isn't explicitely explained when the array
contains less than 2 items. This removes the guesswork.

https://bugzilla.gnome.org/show_bug.cgi?id=764092
2016-04-04 15:06:57 +02:00
Dan Winship
f87e002313 Fix g_strerror() on non-glibc
When using one of the codepaths that copies the error string into buf,
make sure the string gets strdup() afterward.

https://bugzilla.gnome.org/show_bug.cgi?id=758194
2015-11-16 16:57:38 -05:00
Philip Withnall
25a7c817d3 glib: Add missing (nullable) and (optional) annotations
Add various (nullable) and (optional) annotations which were missing
from a variety of functions. Also port a couple of existing (allow-none)
annotations in the same files to use (nullable) and (optional) as
appropriate instead.

Secondly, add various (not nullable) annotations as needed by the new
default in gobject-introspection of marking gpointers as (nullable). See
https://bugzilla.gnome.org/show_bug.cgi?id=729660.

This includes adding some stub documentation comments for the
assertion macro error functions, which weren’t previously documented.
The new comments are purely to allow for annotations, and hence are
marked as (skip) to prevent the symbols appearing in the GIR file.

https://bugzilla.gnome.org/show_bug.cgi?id=719966
2015-11-07 10:48:32 +01:00
Philip Withnall
2e078f1fc0 gstrfuncs: Add missing annotations to g_[ascii_]strto*() functions
Add missing (out) (transfer none) (optional) annotations to g_strtod(),
g_ascii_strtod(), g_ascii_strtoull() and g_ascii_strtoll().
2015-10-08 11:10:39 +01:00
Xavier Claessens
b81f3ced71 Move GStrv typedef to glib.h instead of gobject.h
GStrv was historically only needed for the boxed G_TYPE_STRV,
but it is now useful for g_auto(GStrv) as well. This is not
an ABI change.

https://bugzilla.gnome.org/show_bug.cgi?id=755355
2015-09-22 11:18:30 -04:00
Matthias Clasen
b9a27679ec Revert "Cleanups after we dropped mem vtables"
This reverts commit 627854fee1.

It has been argued that not aborting on malloc() failure is
an incompatible change.
2015-09-12 12:05:31 -04:00
Matthias Clasen
627854fee1 Cleanups after we dropped mem vtables
Since g_malloc is now always malloc, we can just use
strdup and strndup directly.
2015-09-12 11:13:45 -04:00
Dan Winship
96675446c5 Make g_strerror() do less work
Store the (translated, UTF-8-encoded) error strings in a hash table to
avoid doing translation and (possibly) g_locale_to_utf8() in every
g_strerror() call.

https://bugzilla.gnome.org/show_bug.cgi?id=754788
2015-09-11 12:39:44 -04:00
Dan Winship
19eb511ba4 More g_strerror() fixes
Add a check to configure.ac for strerror_r, since we don't currently
require POSIX.1-2001 conformance in general. Add back a
plain-strerror() case as a fallback, and rearrange the glibc-vs-POSIX
strerror_r() branches.

Update the docs to not claim that "not all platforms support the
strerror() function" (we require C90), but still mention the UTF-8 and
always-valid-string benefits. (And make test_strerror() check that
last part.)

https://bugzilla.gnome.org/show_bug.cgi?id=754788
2015-09-11 12:38:18 -04:00
Ting-Wei Lan
ebf961a58d Make g_strerror work with non-glibc POSIX systems
We should only use GNU-specific strerror_r on glibc. On other systems,
we should use the XSI-compliant version.

https://bugzilla.gnome.org/show_bug.cgi?id=754601
2015-09-07 15:18:01 -04:00
Chun-wei Fan
4cad3f5e1b glib/strfuncs.c: Fix Build on Windows
Windows does not have strerror_r(), but does have strerror_s(), which is
threadsafe, and does more or less the same thing, so use it on Windows to
fix the build.

https://bugzilla.gnome.org/show_bug.cgi?id=754431
2015-09-02 17:03:44 +08:00
Matthias Clasen
36fac0849c Make g_strerror threadsafe
We need to use strerror_r here, in order to be threadsafe.
2015-08-28 15:54:46 -04:00
Philip Withnall
5ee333e4cb gstrfuncs: Add a string formatting note about using G_GUINT64_FORMAT
…and friends. The ‘String precision pitfalls’ section is already linked
to from all the relevant printf()-style functions, so this documentation
should hopefully be easy to find.

https://bugzilla.gnome.org/show_bug.cgi?id=741779
2015-08-19 11:36:43 +01:00
Philip Withnall
92041f4b3b gstrfuncs: Document that g_ascii_dtostr() writes a nul terminator
And g_ascii_formatd().

Reviewed-by: Ryan Lortie <desrt@desrt.ca>
2015-01-25 16:22:43 +00:00
Xavier Claessens
71944b1bfd gstrfuncs: Add g_strv_contains()
Includes unit tests.

https://bugzilla.gnome.org/show_bug.cgi?id=685880
2014-11-25 12:51:36 +00:00
Alberto Ruiz
6c080721fc glib: Improve documentation for g_strfreev()
Fixes #740309.
2014-11-18 14:43:41 +00:00
Sébastien Wilmet
97f34bacce doc: small improvement and fixes
- Add an example to g_strsplit(), like it is done for g_strsplit_set().

- GTK-Doc generates a list if a "1." is at the beginning of a line.

https://bugzilla.gnome.org/show_bug.cgi?id=732704
2014-07-04 17:03:50 +02:00
Ryan Lortie
dce88768dc all: remove use of 'register' keyword
We should have done this a decade ago...

https://bugzilla.gnome.org/show_bug.cgi?id=730293
2014-06-28 13:07:52 -04:00
Philip Withnall
11297fd183 gstrfuncs: Add missing preconditions to g_str_match_string()
https://bugzilla.gnome.org/show_bug.cgi?id=113075
2014-05-04 18:21:20 +01:00
Dan Winship
eec507c159 g_str_has_prefix: don't call strlen(str)
There's no reason to check the length of @str in g_str_has_prefix(),
since if it's shorter than @prefix, the strncmp() will fail anyway.
And besides making the function less efficient, it also breaks code
like:

    if (buf->len >=3 && g_str_has_prefix (buf->data, "foo"))
      ...

which really looks like it ought to work whether buf->data is
nul-terminated or not.

https://bugzilla.gnome.org/show_bug.cgi?id=727890
2014-04-10 10:10:24 -04:00
Ryan Lortie
a8ea3dc03b g_str_tokenize_and_fold: do proper transliteration
g_str_tokenize_and_fold() can now do proper locale-sensitive
transliteration for ascii alternatives.

https://bugzilla.gnome.org/show_bug.cgi?id=710142
2014-02-20 18:27:48 -05:00
William Jon McCann
20f4d1820b docs: use "Returns:" consistently
Instead of "Return value:".
2014-02-19 19:41:52 -05:00