]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sysupdate: Fix resource_find_instance
authorAdrian Vovk <adrianvovk@gmail.com>
Sat, 22 Jun 2024 00:03:12 +0000 (20:03 -0400)
committerAdrian Vovk <adrianvovk@gmail.com>
Thu, 22 Aug 2024 20:00:43 +0000 (16:00 -0400)
The current implementation will never find a match, because in the event
of a match instance_cmp falls through to comparing paths and the key
we're matching against will always have a path of NULL.

So let's just use a separate compare function, just to make sure future
updates to instance_cmp don't break resource_find_instance again.

src/sysupdate/sysupdate-resource.c

index 5b7aee2b54c29b3dd63c18b6042c21d07cb73951..94308998971221822dddb22d6b1cad2231ec199a 100644 (file)
@@ -527,13 +527,25 @@ int resource_load_instances(Resource *rr, bool verify, Hashmap **web_cache) {
         return 0;
 }
 
+static int instance_version_match(Instance *const*a, Instance *const*b) {
+        assert(a);
+        assert(b);
+        assert(*a);
+        assert(*b);
+        assert((*a)->metadata.version);
+        assert((*b)->metadata.version);
+
+        /* List is sorted newest-to-oldest */
+        return -strverscmp_improved((*a)->metadata.version, (*b)->metadata.version);
+}
+
 Instance* resource_find_instance(Resource *rr, const char *version) {
         Instance key = {
                 .metadata.version = (char*) version,
         }, *k = &key;
 
         Instance **found;
-        found = typesafe_bsearch(&k, rr->instances, rr->n_instances, instance_cmp);
+        found = typesafe_bsearch(&k, rr->instances, rr->n_instances, instance_version_match);
         if (!found)
                 return NULL;