From fb9411ff76c3f3ef57cd19eed06cf21b89db2e95 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 2 Jun 2017 20:15:31 +0200 Subject: [PATCH] settings: refactor nm_settings_connection_read_and_fill_timestamp() Coverity complains about not checking the return value: src/settings/nm-settings-connection.c:2329: check_return: Calling "g_key_file_load_from_file" without checking return value (as is done elsewhere 6 out of 7 times). While at it, refactor the code and check whether the timestamp is valid. (cherry picked from commit 238efbbb12d32b4d4b7827509b4f21af9d159617) --- src/settings/nm-settings-connection.c | 40 ++++++++++++++------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/settings/nm-settings-connection.c b/src/settings/nm-settings-connection.c index 0cb2920c58..bc6dcfce13 100644 --- a/src/settings/nm-settings-connection.c +++ b/src/settings/nm-settings-connection.c @@ -2316,33 +2316,35 @@ void nm_settings_connection_read_and_fill_timestamp (NMSettingsConnection *self) { NMSettingsConnectionPrivate *priv = NM_SETTINGS_CONNECTION_GET_PRIVATE (self); + gs_unref_keyfile GKeyFile *timestamps_file = NULL; + gs_free_error GError *error = NULL; + gs_free char *tmp_str = NULL; const char *connection_uuid; - guint64 timestamp = 0; - GKeyFile *timestamps_file; - GError *err = NULL; - char *tmp_str; + gint64 timestamp; g_return_if_fail (NM_IS_SETTINGS_CONNECTION (self)); - /* Get timestamp from database file */ timestamps_file = g_key_file_new (); - g_key_file_load_from_file (timestamps_file, SETTINGS_TIMESTAMPS_FILE, G_KEY_FILE_KEEP_COMMENTS, NULL); - connection_uuid = nm_settings_connection_get_uuid (self); - tmp_str = g_key_file_get_value (timestamps_file, "timestamps", connection_uuid, &err); - if (tmp_str) { - timestamp = g_ascii_strtoull (tmp_str, NULL, 10); - g_free (tmp_str); + if (!g_key_file_load_from_file (timestamps_file, SETTINGS_TIMESTAMPS_FILE, G_KEY_FILE_KEEP_COMMENTS, &error)) { + _LOGD ("failed to read connection timestamp: %s", error->message); + return; } - /* Update connection's timestamp */ - if (!err) { - priv->timestamp = timestamp; - priv->timestamp_set = TRUE; - } else { - _LOGD ("failed to read connection timestamp: %s", err->message); - g_clear_error (&err); + connection_uuid = nm_settings_connection_get_uuid (self); + tmp_str = g_key_file_get_value (timestamps_file, "timestamps", connection_uuid, &error); + if (!tmp_str) { + _LOGD ("failed to read connection timestamp: %s", error->message); + return; } - g_key_file_free (timestamps_file); + + timestamp = _nm_utils_ascii_str_to_int64 (tmp_str, 10, 0, G_MAXINT64, -1); + if (timestamp < 0) { + _LOGD ("failed to read connection timestamp: %s", "invalid number"); + return; + } + + priv->timestamp = timestamp; + priv->timestamp_set = TRUE; } /**