summaryrefslogtreecommitdiff
path: root/glib/gkeyfile.c
Commit message (Collapse)AuthorAgeFilesLines
* gkeyfile: Fix group comment managementGaël Bonithon2023-04-201-63/+74
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This removes the `comment` member of the GKeyFileGroup structure, which seemed intended to distinguish comments just above a group from comments above them, separated by one or more blank lines. Indeed: * This does not seem to match any specification in the documentation, where blank lines and lines starting with `#` are indiscriminately considered comments. In particular, no distinction is made between the comment above the first group and the comment at the beginning of the file. * This distinction was only half implemented, resulting in confusion between comment above a group and comment at the end of the preceding group. Instead, the same logic is used for groups as for keys: the comment above a group is simply the sequence of key-value pairs of the preceding group where the key is null, starting from the bottom. The addition of a blank line above groups when writing, involved in bugs #104 and #2927, is kept, but: * It is now added as a comment as soon as the group is added (since a blank line is considered a comment), so that `g_key_file_get_comment()` returns the correct result right away. * It is always added if comments are not kept. * Otherwise it is only added if the group is newly created (not present in the original data), in order to really keep comments (existing and not existing). Closes: #104, #2927
* gkeyfile: Replace g_slice_*() with g_new*()/g_free_sized()Gaël Bonithon2023-04-201-13/+13
|
* Use O_CLOEXEC in {g_,}open () calls for race-free setting of the ↵Maciej S. Szmigiero2023-02-211-2/+6
| | | | | | | | | close-on-exec flag The remaining call sites are either Windows-only, between fork () and exec () or in xdgmime copylib. Hope I haven't missed any site.
* Use g_string_free_and_steal () moreSergey Bugaev2023-01-271-4/+1
| | | | | | | | | | | | | Now that there is g_string_free_and_steal (), we can use it instead of the older g_string_free (_, FALSE). Make sure to use its return value while doing so, as opposed to manually accessing string->str, to avoid compiler warnings and make the intent more explicit. This is all done in preparation for making g_string_free (_, FALSE) warn on unused return value much like g_string_free_and_steal (), which will happen in the next commit. Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
* gkeyfile: Remove some unreachable codePhilip Withnall2022-07-051-1/+1
| | | | | | | | | There’s a precondition assertion on the function which checks that `group_name != NULL`. Signed-off-by: Philip Withnall <pwithnall@endlessos.org> Coverity CID: #1474780
* glib: Add SPDX license headers automaticallyPhilip Withnall2022-05-181-0/+2
| | | | | | | | | | | | | | 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
* docs: mark macros, flags, enums with percent signGabor Karsay2022-03-041-21/+21
|
* gkeyfile: Rework preconditions to avoid unreachable branchesPhilip Withnall2021-11-021-9/+7
| | | | | | | | | It’s a programmer error for `NULL` to reach `is_group_name()` or `is_key_name()`, so rework the preconditions so that all programmer error checks are wrapped in a `g_return_if_fail()`. The coverage scanning is set up to ignore anything inside that. Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Remove some redundant checksPhilip Withnall2021-11-021-3/+5
| | | | | | | `g_key_file_parse_key_value_pair()` checks that both of these are non-NULL on entry, and they can’t be modified between then and here. Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Clarify behaviour of locales and checked_localesPhilip Withnall2021-11-021-2/+3
| | | | | | | | | | This should introduce no functional changes, but will remove an unreachable branch in `g_key_file_locale_is_interesting()`: `checked_locales` indicates whether `locales` has been initialised (and it can be initialised to `NULL`), so there’s no point in checking `locales` itself if `checked_locales` is checked. Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Avoid allocating a copy of the locale for a keyPhilip Withnall2021-11-021-15/+31
| | | | | | | Instead compare it inline. This should avoid a lot of allocations in key files with lots of translations (such as desktop files). Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Move allocation of value until after locale checks have passedPhilip Withnall2021-11-021-4/+1
| | | | | | | | | | This avoids allocating a copy of the value in the case that it’s for a locale which is uninteresting. This should speed up parsing of key files with large numbers of translations, when only the translations for certain locales are wanted. Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Tidy up ownership transfer in parse_key_value_pair()Philip Withnall2021-11-021-7/+4
| | | | | | | This introduces no functional changes, but does make the ownership transfer a little clearer. Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Move allocation of value until after error checkingPhilip Withnall2021-11-021-5/+5
| | | | | | | | | This doesn’t affect performance in the normal case of a valid key file, but does improve performance when handling largely-invalid key files. oss-fuzz#31796 Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Fix off-by-one error in calculating value lengthPhilip Withnall2021-11-021-1/+1
| | | | | | | | This was harmless, as it was always +1 too long, so included the trailing nul terminator. However, upcoming changes will start to use it in a context where there is no nul terminator. Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Eliminate strcmp()s when looking up a group nodePhilip Withnall2021-11-021-9/+4
| | | | | | | | Rather than looking for the group node by comparing each name in the linked list of `GKeyFileGroup` instances, look up the `GKeyFileGroup` in the hash table, then look up its `GList` node by pointer. Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Eliminate unnecessary allocations when setting booleansPhilip Withnall2021-11-021-9/+6
| | | | Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Avoid an unnecessary strcmp()Philip Withnall2021-11-021-2/+1
| | | | | | | | | | | | | As the groups are disambiguated by name via the `groups` hash table, there is guaranteed to be at most one `GKeyFileGroup` instance per group name, which means they can be compared for equality by `GKeyFileGroup` pointer, rather than needing a `strcmp()`. This speeds up key file parsing in all cases. oss-fuzz#31796 Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Add a length argument to is_key_name()Philip Withnall2021-11-021-14/+29
| | | | | | | | | | | | | This allows it to be called on a substring of a larger string, without having to allocate a nul-terminated copy of the substring with `g_strndup()` before knowing that the key name is actually valid. This speeds up parsing of highly invalid key files, but doesn’t affect performance in the normal case of a valid key file. oss-fuzz#31796 Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Make various parsing variables constPhilip Withnall2021-11-011-16/+20
| | | | | | | | | All these `gchar *`s are used as moving pointers into strings, being incremented as the strings are parsed. They are never modified, so can be `const`. This doesn’t speed anything up, but does allow removing some casts and slightly improving type safety. Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* gkeyfile: Remove unreachable codeEgor Bychin2021-10-151-3/+0
|
* docs: Fix KeyFile annotationsEmmanuele Bassi2021-08-021-38/+39
| | | | | | | Use the right gtk-doc sigil for pre-processor symbols. Reduce the indentation in argument annotations, to avoid them being parsed as code blocks.
* gkeyfile: Fix crash when parsing translations on a second loadPhilip Withnall2021-03-231-0/+1
| | | | | | | | | | | | If the same `GKeyFile` is reused to load multiple different key files, any loads after the first which encounter translated keys will crash, because clearing the data from the first load cleared the cached language names, but didn’t clear `checked_locales`, so they were never reloaded. Signed-off-by: Philip Withnall <pwithnall@endlessos.org> Fixes: #2361
* gkeyfile: Drop a redundant checkPhilip Withnall2021-03-231-1/+1
| | | | | | | | It should not be possible for `->locales` to be set without `->checked_locales` being set, so drop the redundant check. This helps with branch code coverage. Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
* Mark g_key_file_get_comment() key parameter as nullableSebastian Dröge2021-02-021-1/+1
| | | | | It will return the comment above the group (or at the top of the file) then according to the documentation.
* keyfile: Only allocate group_hash if neededTimm Bäder2020-12-311-2/+11
| | | | | A keyfile with no groups (e.g. an empty one) does not need a hash table for the groups.
* keyfile: Don't allocate parse_buffer if we don't need itTimm Bäder2020-12-311-1/+7
| | | | | | | | | | | | | When loading a GKeyFile, the sequence is usually: keyfile = g_key_file_new(); g_key_file_load_xxx(keyfile, ...) g_key_file_new() calls g_key_file_init(), which allocates a parse_buffer for parsing. g_key_file_load_xxx() will then g_key_file_clear() the keyfile and call g_key_file_init() again. Just don't allocate a parse_buffer unless we need it for parsing.
* keyfile: Delay calling g_get_language_names() until it's neededTimm Bäder2020-12-301-1/+7
| | | | The g_get_languages() call is quite costly and often unneeded.
* glib: Drop unnecessary volatile qualifiers from internal variablesPhilip Withnall2020-11-201-1/+1
| | | | | | | | | These variables were already (correctly) accessed atomically. The `volatile` qualifier doesn’t help with that. Signed-off-by: Philip Withnall <pwithnall@endlessos.org> Helps: #600
* Add various missing nullable annotationsSebastian Dröge2020-10-191-1/+1
|
* glib: Use g_file_set_contents_full() throughout GLib and GIOPhilip Withnall2020-07-261-1/+3
| | | | | | | | | 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
* tree: Fix various typos and outdated terminologyPhilip Withnall2020-06-121-2/+2
| | | | | | | | | | | | | | | | | | | | | | | 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>
* glib: Various minor scan-build fixesPhilip Withnall2019-09-051-0/+1
| | | | | | | | | 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
* minor typos in the documentation (a/an)Дилян Палаузов2019-08-241-1/+1
|
* gkeyfile: Fix parsing of new lines in commentsPhilip Withnall2018-10-301-10/+25
| | | | | | | | | | | | | | | | | | Previously, the code which parsed comments in key files would append a line break to the comment where there was none before; this was part of the code for handling re-inserting line breaks into multi-line comments after removing the ‘#’ prefix. Now, we don’t add a terminal line break. This was slightly icky to implement because parse_value_as_comment() is called once for each line of a multi-line comment. This expands the existing test case to cover a single line comment, and also fixes the documentation to correctly state that the leading ‘#’ *is* removed and mention the new line break behaviour. Signed-off-by: Philip Withnall <withnall@endlessm.com> https://gitlab.gnome.org/GNOME/glib/issues/107
* gkeyfile: remain usable after g_key_file_free()Will Thompson2018-09-201-1/+5
| | | | | | | | | | | | | | | | | | | Previously, in the case where 'kf' has more than one ref, calling g_key_file_free(kf) would break it. For example, calling g_key_file_has_key(kf, ...) would hit the following assertion: g_hash_table_lookup: assertion 'hash_table != NULL' failed This is because g_key_file_free() calls g_key_file_clear() which sets self->groups and other fields to NULL; most lookup functions assume these fields are non-NULL. One fix would be to call g_key_file_init() right after g_key_file_clear() in g_key_file_free(). However, in the case where there are no other refs to the keyfile, this would mean allocating many new hash tables which will be immediately destroyed when g_key_file_unref() removes the last ref. Instead, inline the unref, and re-initialize the internal state when the keyfile is still alive.
* gkeyfile: Fix -Wincompatible-pointer-types warningPhilip Withnall2018-02-081-1/+4
| | | | | | | | | Introduced in commit 1574321e51dc20eb2b0fdd699966428be3cc05eb. This fix introduces no functional changes (just a cast). Signed-off-by: Philip Withnall <withnall@endlessm.com> Reviewed-by: nobody
* GKeyFile: add API for getting locale of a stringAllison Lortie2018-02-061-0/+62
| | | | | | | | | | | | | | | | g_key_file_get_locale_string() returns a translated string from the keyfile. In some cases, it may be useful to know the locale that that string came from. Add a new API, g_key_file_get_locale_for_key(), that returns the locale of the string. Include tests. (Modified by Philip Withnall to rename the API and fix some minor review issues. Squash in a separate test case commit.) https://bugzilla.gnome.org/show_bug.cgi?id=605700
* gkeyfile: Fix FD validity test to be technically correctStewart Brodie2018-02-021-1/+1
| | | | | | The fd could be valid and zero. https://bugzilla.gnome.org/show_bug.cgi?id=760324
* gkeyfile: Document need for KEEP_TRANSLATIONS with get_locale_string()Philip Withnall2018-01-081-0/+8
| | | | | | | | | | | | | When using g_key_file_get_locale_string() or get_locale_string_list(), the GKeyFile must have been loaded with G_KEY_FILE_KEEP_TRANSLATIONS if the lookup locale differs from the one which was current when the key file was loaded. Document that. Signed-off-by: Philip Withnall <withnall@endlessm.com> https://bugzilla.gnome.org/show_bug.cgi?id=792324
* docs: add missing '*' chars at start of doc-commentsStefan Sauer2017-11-121-1/+1
|
* gkeyfile: Add some examples to the documentationPhilip Withnall2017-10-261-0/+52
| | | | | | | | Add some examples of loading and saving key files. Signed-off-by: Philip Withnall <withnall@endlessm.com> https://bugzilla.gnome.org/show_bug.cgi?id=330458
* docs: Escape some backslashes for markdownVolker Sobek2017-10-051-1/+1
| | | | | | These no longer showed up correctly in the documentation. https://bugzilla.gnome.org/show_bug.cgi?id=727346
* Consistently save errno immediately after the operation setting itPhilip Withnall2017-08-031-8/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | Prevent the situation where errno is set by function A, then function B is called (which is typically _(), but could be anything else) and it overwrites errno, then errno is checked by the caller. errno is a horrific API, and we need to be careful to save its value as soon as a function call (which might set it) returns. i.e. Follow the pattern: int errsv, ret; ret = some_call_which_might_set_errno (); errsv = errno; if (ret < 0) puts (strerror (errsv)); This patch implements that pattern throughout GLib. There might be a few places in the test code which still use errno directly. They should be ported as necessary. It doesn’t modify all the call sites like this: if (some_call_which_might_set_errno () && errno == ESOMETHING) since the refactoring involved is probably more harmful than beneficial there. It does, however, refactor other call sites regardless of whether they were originally buggy. https://bugzilla.gnome.org/show_bug.cgi?id=785577
* Revert "GKeyFile – Add array length annotations to to_data(), get_keys() ↵Emmanuele Bassi2017-07-211-3/+3
| | | | | | | | | and get_groups()" This reverts commit fd329f4853f180eb92746f39fc96fd5d91394009. The commit changed the Introspection ABI, and it requires a change in any application using an introspection-based language binding.
* GKeyFile – Add array length annotations to to_data(), get_keys() and ↵Sebastian Dröge2017-06-211-3/+3
| | | | | | get_groups() https://bugzilla.gnome.org/show_bug.cgi?id=784020
* glib/: LGPLv2+ -> LGPLv2.1+Sébastien Wilmet2017-05-241-1/+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
* Make g_utf8_make_valid optionally take a lengthPaolo Borelli2017-03-021-8/+8
| | | | | | | | | | | | | g_utf8_make_valid was turned into a public API this cycle. However now that it is public we should make the API more generic, allowing the caller to specify the length. This is especially useful if the function is called with a string that has \0 in the middle or for chunks of a strings that are not nul terminated. This is also consistent with most of the other utf8 utils. Callers inside glib are updated to the new signature. https://bugzilla.gnome.org/show_bug.cgi?id=779456
* gkeyfile: Be more specific about error codes in documentationPhilip Withnall2017-02-051-5/+14
| | | | | | | | It’s hard to know whether trying to load a non-existent key file will result in G_KEY_FILE_ERROR_NOT_FOUND or G_FILE_ERROR_NOENT; try to improve the documentation to clarify that. https://bugzilla.gnome.org/show_bug.cgi?id=777135
* gkeyfile: Clarify handling of out-of-range integers in documentationPhilip Withnall2017-01-241-2/+4
| | | | | | Clarify that g_key_file_get_integer() and g_key_file_get_integer_list() both return G_KEY_FILE_ERROR_INVALID_VALUE if used to load a valid integer which is out of range for a gint.