diff options
author | Matthew Leeds <matthew.leeds@endlessm.com> | 2018-10-09 17:40:30 -0700 |
---|---|---|
committer | Alexander Larsson <alexander.larsson@gmail.com> | 2018-10-12 10:07:32 +0200 |
commit | 35a4efcb0b60dd45da8ca44eecaf81c935af603b (patch) | |
tree | 37cb1b3bbf170503fbc2e19bf0c3ddc0cca460fc | |
parent | 9ced088fb552e4460e5c5f06762576ba094f7303 (diff) | |
download | flatpak-35a4efcb0b60dd45da8ca44eecaf81c935af603b.tar.gz |
common: Don't seg fault if a ref doesn't exist
Currently, if flatpak_installed_ref_get_latest_commit() returns NULL
(which means the ref doesn't exist in the local repo) we assume any
remote commit could be an update in
flatpak_installation_list_installed_refs_for_update() when a collection
ID is not configured on the remote. When a collection ID is configured,
if get_latest_commit() returns NULL it causes a crash in
ostree_repo_load_commit(). So this commit prevents the crash and makes
the behavior in the post-collection-id world consistent with the
behavior in the pre-collection-id world.
It's difficult to write a test for this that's not contrived, without
knowing what circumstances led to the disappearance of the ref from the
repo.
Fixes https://github.com/flatpak/flatpak/issues/2216
Closes: #2229
Approved by: alexlarsson
(cherry picked from commit 6b4402b60eb9b24c60cf24f5fcddc78d3dd04ab1)
-rw-r--r-- | common/flatpak-installation.c | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/common/flatpak-installation.c b/common/flatpak-installation.c index 4ed28bdc..ef2dfb8b 100644 --- a/common/flatpak-installation.c +++ b/common/flatpak-installation.c @@ -996,6 +996,7 @@ flatpak_installation_list_installed_refs_for_update (FlatpakInstallation *self, const char *remote_commit = g_hash_table_lookup (remote_commits, key); const char *local_commit = flatpak_installed_ref_get_latest_commit (installed_ref); + /* Note: local_commit may be NULL here */ if (remote_commit != NULL && g_strcmp0 (remote_commit, local_commit) != 0) g_ptr_array_add (updates, g_object_ref (installed_ref)); @@ -1076,28 +1077,30 @@ flatpak_installation_list_installed_refs_for_update (FlatpakInstallation *self, /* The ref_to_checksum map only tells us if this remote is offering * the latest commit of the available remotes; we have to check * ref_to_timestamp to know if the commit is an update or a - * downgrade. + * downgrade. If local_commit is NULL assume it's an update until + * proven otherwise. */ - { - guint64 local_timestamp = 0; - guint64 *remote_timestamp; - g_autoptr(GVariant) commit_v = NULL; - - if (ostree_repo_load_commit (flatpak_dir_get_repo (dir), local_commit, &commit_v, NULL, NULL)) - local_timestamp = ostree_commit_get_timestamp (commit_v); - - remote_timestamp = g_hash_table_lookup (results[j]->ref_to_timestamp, collection_ref); - *remote_timestamp = GUINT64_FROM_BE (*remote_timestamp); - - g_debug ("%s: Comparing local timestamp %" G_GUINT64_FORMAT " to remote timestamp %" - G_GUINT64_FORMAT " on ref (%s, %s)", G_STRFUNC, local_timestamp, *remote_timestamp, - collection_ref->collection_id, collection_ref->ref_name); - - /* The timestamp could be 0 due to an error reading it. Assume - * it's an update until proven otherwise. */ - if (*remote_timestamp != 0 && *remote_timestamp <= local_timestamp) - continue; - } + if (local_commit != NULL) + { + guint64 local_timestamp = 0; + guint64 *remote_timestamp; + g_autoptr(GVariant) commit_v = NULL; + + if (ostree_repo_load_commit (flatpak_dir_get_repo (dir), local_commit, &commit_v, NULL, NULL)) + local_timestamp = ostree_commit_get_timestamp (commit_v); + + remote_timestamp = g_hash_table_lookup (results[j]->ref_to_timestamp, collection_ref); + *remote_timestamp = GUINT64_FROM_BE (*remote_timestamp); + + g_debug ("%s: Comparing local timestamp %" G_GUINT64_FORMAT " to remote timestamp %" + G_GUINT64_FORMAT " on ref (%s, %s)", G_STRFUNC, local_timestamp, *remote_timestamp, + collection_ref->collection_id, collection_ref->ref_name); + + /* The timestamp could be 0 due to an error reading it. Assume + * it's an update until proven otherwise. */ + if (*remote_timestamp != 0 && *remote_timestamp <= local_timestamp) + continue; + } g_ptr_array_add (updates, g_object_ref (installed_ref)); |