From cc7b2cde95bb45072e51bdadd2d9760aec61fe58 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 2 Aug 2019 23:13:33 +0200 Subject: [PATCH] libnm: set errno in nm_key_file_get_boolean() to distinguish between missing key and error This is also what nm_keyfile_plugin_kf_get_int64() does. It's useful to know whether a value was missing or invalid. --- libnm-core/nm-keyfile-utils.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/libnm-core/nm-keyfile-utils.c b/libnm-core/nm-keyfile-utils.c index 522d5b71fa..acc3458d85 100644 --- a/libnm-core/nm-keyfile-utils.c +++ b/libnm-core/nm-keyfile-utils.c @@ -49,18 +49,28 @@ * to detect parsing failures. * * Returns: either %TRUE or %FALSE if the key exists and is parsable as a boolean. - * Otherwise, @default_value. + * Otherwise, @default_value. Sets errno to ENODATA, EINVAL or 0, depending on whether + * the key exists, whether the value is invalid, or success. */ int nm_key_file_get_boolean (GKeyFile *kf, const char *group, const char *key, int default_value) { + int v; gs_free char *value = NULL; value = g_key_file_get_value (kf, group, key, NULL); - if (!value) + if (!value) { + errno = ENODATA; return default_value; - return _nm_utils_ascii_str_to_bool (value, default_value); + } + v = _nm_utils_ascii_str_to_bool (value, -1); + if (v != -1) { + errno = 0; + return v; + } + errno = EINVAL; + return default_value; } /*****************************************************************************/