diff options
author | Thomas Haller <thaller@redhat.com> | 2017-06-02 20:15:31 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-06-02 20:17:30 +0200 |
commit | 238efbbb12d32b4d4b7827509b4f21af9d159617 (patch) | |
tree | 1b6f234731bed85289cf979ddf9862590ef5c07f | |
parent | c7c47575ce57d37037d20756cba7ed4af2f6c6bd (diff) | |
download | NetworkManager-238efbbb12d32b4d4b7827509b4f21af9d159617.tar.gz |
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.
-rw-r--r-- | src/settings/nm-settings-connection.c | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/src/settings/nm-settings-connection.c b/src/settings/nm-settings-connection.c index 76b0ff5a85..ce0091f67a 100644 --- a/src/settings/nm-settings-connection.c +++ b/src/settings/nm-settings-connection.c @@ -2341,33 +2341,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); + 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; + } + 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); + 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; } - /* 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); + 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; } - g_key_file_free (timestamps_file); + + priv->timestamp = timestamp; + priv->timestamp_set = TRUE; } /** |